Search code examples
phpwordpresswoocommercecartcheckout

WooCommerce Combined Cart & Checkout Page (Redirect Issue)


I have a single page site that has a product listing, cart & checkout all on one page. It's been a challenge to get this setup working but I have it 99% of the way there :/

My issue is if I click on the Update Cart button without any of the Qty fields being changed is redirects me to the actual cart page.

Here is my setup

  • I created a new page called Store
  • I set this page to be the "Checkout Page" in WooCommerce settings
  • In the block editor I added the Checkout block to the page
  • To view the page with an empty cart I added add_filter('woocommerce_checkout_redirect_empty_cart', '__return_false');
  • Then in my page template I added the cart shortcode echo do_shortcode('[woocommerce_cart]');

This works great, I can add products to the cart, adjust the qty in the cart and checkout without issue.

However, if I click the "Update Cart" without any changes to any Qty fields it redirects to /cart. This is a huge problem as I need to stay on this page.

Things I've tried...

  • I've tried only displaying the Update Cart button if changes have been made. This is complicated because I've added some custom buttons to adjust the qty and with multiple items in the cart it gets hard to work out.
  • I tried an .htaccess redirect of /cart to /store but that breaks the cart functionality
  • I tried messing with the nonce by adding wp_nonce_field( 'woocommerce-cart', '_wpnonce', false, true ); but that didn't seem to do anything

Solution

  • The only way to get this solved is by keeping the update button disabled until a quantity has been changed. It requires custom JavaScript (jQuery).

    Now for that redirection problem to the cart page, you could try to change the cart URL used in/with wc_get_cart_url() function to the checkout URL, using the following:

    add_filter( 'woocommerce_get_cart_url', function( $url ) { 
        return wc_get_checkout_url();
    } );
    

    or simply:

    add_filter( 'woocommerce_get_cart_url', 'wc_get_checkout_url' );
    

    This could improve/solve the redirection behavior issue.