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.
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.
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.
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:
functions.php
file or a custom plugin file.// 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' );
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.
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.