Skipping the cart and going straight to checkout

This recipe will be useful for you if you have the type of store where most people only buy one product and thus the cart isn't very useful. In such cases, you might want to remove the cart altogether and have the customer skip the cart page and go straight to the checkout.

Note

We have covered a similar recipe, Making the Add to Cart button go straight to the checkout page, in Chapter 3, Changing the Product Organization, which uses a plugin instead of code.

Getting ready

In the WordPress admin, you need to make sure the Redirect to the cart page after successsful addition option under WooCommerce | Settings | Products | Display is checked.

How to do it…

We'll be writing a bit of code to change the page that a customer lands on after adding a product to the cart. You'll want to put this in either your theme's functions.php file or in a custom plugin. Let's take a look at the following steps:

  1. Open either your theme's functions.php file or a custom plugin file.
  2. At the bottom of the file, add the following code:
    // make the add to cart redirect go to the checkout page 
    function woocommerce_cookbook_add_to_cart_redirect() { 
        return get_permalink( wc_get_page_id( 'checkout' ) ); 
    } 
    add_filter( 'woocommerce_add_to_cart_redirect', 'woocommerce_cookbook_add_to_cart_redirect' );
  3. Save the file and upload it to your site.

How it works…

This code doesn't replace the cart page entirely. It just skips the cart page when a product is added to the cart. A user can still access the cart if it's in your menu or if they type in the direct URL. This could be ideal, because some people will want to return to their cart after leaving the site.

There's more...

We used the wc_get_page_id( 'checkout' ) line to select the checkout page. You could use that same function to get the link to any WooCommerce page and the user would be directed to that page. To see the other WooCommerce pages you can access with that function, view the documentation for that function at http://docs.woothemes.com/wc-apidocs/function-wc_get_page_id.html.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset