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
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
- Why the downvote? Seems like a well constructed question, I explained my problem, my setup and what I've tried to fix it. – Jon Commented Jan 30 at 17:11
- 1 This isn't a redirect issue, the functionality of the button does what it does, i don' see how any of these things you tried are even relevant to what you are trying to accomplish. – Kevin B Commented Jan 30 at 17:13
- Then clearly you don't understand my problem or how WooCommerce carts works, so why are you even commenting? Let alone down voting. – Jon Commented Jan 30 at 17:37
- 1 @ADyson Yes, If I click the button after adjusting any of quantity inputs it works as expected. I believe it posts back to my custom page /store. If I dont make any changes it goes to /cart (the original cart page) – Jon Commented Jan 30 at 18:05
- 1 @LoicTheAztec This is interesting, I will look into this more. Funny because in the Woo advanced setting I tried setting the cart url and checkout url to be the same page but they dont allow that. I was going to look in the DB but maybe this is better. – Jon Commented Jan 30 at 19:15
1 Answer
Reset to default 1The 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.