Skip shopping cart in WooCommerce

My beautiful wife put together a great WordPress blog, and installed the WooCommerce plugin in order to sell some items on her site. However, she didn’t like the conversion flow for end users; users had to add an item to the cart, confirm the quantities and subtotal, then enter payment information. Because my wife only had one SKU (product), and customers could only buy one at a time, she wanted to skip the shopping cart verification step, and allow customers to go straight from the product page to entering payment information after hitting a big red button.

Seems like a reasonable request. However, after much googling, I determined there was no out-of-the-box setting to enable users to skip the cart page after adding an item to the cart. Furthermore, although the WooCommerce plugin app does expose some AJAX endpoints, those endpoints do not provide the ability to reliably add one and only item of a given product to a cart before redirecting a user to the checkout page.

Luckily my beautiful wife is married to an ace programmer. It’s been several years since I’ve done any coding in PHP, but I went ahead and threw down a little modification for the WooCommerce plugin to enable her special use case. I’m releasing the code here into the public domain.

/** * Set cart item quantity action *
 * Only works for simple products (with integer IDs, no colors etc) *
 * @access public
 * @return void */
function woocommerce_set_cart_qty_action() { 
  global $woocommerce;
  foreach ($_REQUEST as $key => $quantity) {
    // only allow integer quantities
    if (! is_numeric($quantity)) continue;

    // attempt to extract product ID from query string key
    $update_directive_bits = preg_split('/^set-cart-qty_/', $key);
    if (count($update_directive_bits) >= 2 and is_numeric($update_directive_bits[1])) {
      $product_id = (int) $update_directive_bits[1]; 
      $cart_id = $woocommerce->cart->generate_cart_id($product_id);
      // See if this product and its options is already in the cart
      $cart_item_key = $woocommerce->cart->find_product_in_cart( $cart_id ); 
      // If cart_item_key is set, the item is already in the cart
      if ( $cart_item_key ) {
        $woocommerce->cart->set_quantity($cart_item_key, $quantity);
      } else {
        // Add the product to the cart 
        $woocommerce->cart->add_to_cart($product_id, $quantity);
      }
    }
  }
}

add_action( 'init', 'woocommerce_set_cart_qty_action' );

Please be aware this code has been tested, but is intended for hobbyist use. No warranty is implied. Your mileage may vary. Offer void where prohibited.

Installation instructions:
Copy and paste the code in your theme’s functions.php file.

Usage instructions:
Create a hyperlink with a query string argument like so:
<url>?set-cart-qty_<product ID>=<quantity>
Where <product ID> is the numerical ID of your product (something like “167”) and <quantity> is the quantity you want set in the user’s shopping cart (most likely this will just be “1”).

Example URL to send user to checkout with exactly one item of product with ID “167” in cart:

http://my.website.com/checkout?set-cart-qty_167=1

In my wife’s case the URL was set to the checkout page, so that she could set up a shiny red button that linked the user to checkout with the appropriate product already set in the cart, ready to check out.

Feel free to use it for your own site; it should be useful for any situation where you want to ensure a single product exists in the cart at a set quantity.