By default, Woocommerce’s checkout process is broken into two parts. The shopping cart page and the actual checkout page. With a few tweaks, we can combine these two and make ordering on your site even easier.
If you want all default functionality across the board, getting single/one-page checkout is super simple. All you need to do is call the two Woocommerce shortcodes in your page content and you’re good to go.
[woocommerce_cart]
[woocommerce_checkout]
If we’re doing some more complex things with our templates, we’re going to need to do a little bit more work. Having those two shortcodes in our content area causes Woocommerce to always return true when figuring out if the current page we are on is a checkout page or if the current page is the cart page. In our case, we want it to be both.
Having [woocommerce_cart]
in our content returns true for the is_cart()
function.
Having [woocommerce_checkout]
in our content returns true for the is_checkout()
function.
When those two shortcodes aren’t present in our content we need to manually set some variables to make those functions return true.
For is_cart()
to return true, we can set a global constant of WOOCOMMERCE_CART to true.
For is_checkout()
to return true, we can add a filter for the hook woocommerce_is_checkout to return true.
As some plugins and other Woocommerce add-ons enqueue scripts in the wp_head
hook, we need to set those constants are early as we can. Through trial and error, I’ve found that the best place to do it is on the wp
hook. To determine what pages to set the constants on, I’m doing some checks on the template name.
// sets the correct variables when we're on the checkout pages add_action('wp', 'pk_custom_checkout_wp'); function pk_custom_checkout_wp() { if(in_array(basename(get_page_template()), array('tpl-cart.php', 'tpl-other-checkout-page.php'))) { if(!defined('WOOCOMMERCE_CART')) { define('WOOCOMMERCE_CART', true); } add_filter('woocommerce_is_checkout', '__return_true'); } }
Add that to your functions.php file and update the template names to your templates and you’re all set to go!