Your question should be specific to WordPress. Generic PHP/JS/SQL/HTML/CSS questions might be better asked at Stack Overflow or another appropriate Stack Exchange network site. Third-party plugins and themes are off-topic for this site; they are better asked about at their developers' support routes.
Closed 4 years ago.
Improve this questionOn my site, I am using WooCommerce in a bit different way. I am selling services on my site and the services page has pricing table. When people click on the respective button of the pricing table, they directly get transferred to checkout. I have unchecked the following option in Woocommer settings:
- Redirect to the cart page after successful addition
- Enable AJAX add to cart buttons on archives
- All my products are set as variable products with virtual items and can only be purchased one quantity on a single order.
I have also added the following snippet to ensure the users get directly transferred to the checkout page instead of the cart page in the middle.
add_filter('add_to_cart_redirect', function() {
// Remove the default `Added to cart` message
wc_clear_notices();
return wc_get_checkout_url();
});
This approach works fine and takes the user to the checkout page as intended but creates two issues:
Situation 1
The user visit to the services page, click on the respective pricing table, gets transferred to the checkout page. Now the user clicks the browser back button and rethinks about which plan to choose. And then thinks that the plan he chooses was right all along.
Then he again clicks on the pricing table button but this time sees:
You cannot add another "Product X" to your cart.
Situtation 2
The visitor first selects one plan that goes back and after rethinking select another plan. In this case on the checkout page the user getting charged for both the product, the one he first selected by mistakes and then the one he actually selected. Now as there is no cart page, it's hard for the user to go and update the cart.
What I am Looking For
I am just looking to disable the cart functionality all together so that the site doesn't remember what the user has added in the cart. So, if the user select a product goes to the checkout page and then goes back and select another product this problem won't happen was the cart won't remember anything and the cart functionality is disable.
Closed. This question is off-topic. It is not currently accepting answers.Your question should be specific to WordPress. Generic PHP/JS/SQL/HTML/CSS questions might be better asked at Stack Overflow or another appropriate Stack Exchange network site. Third-party plugins and themes are off-topic for this site; they are better asked about at their developers' support routes.
Closed 4 years ago.
Improve this questionOn my site, I am using WooCommerce in a bit different way. I am selling services on my site and the services page has pricing table. When people click on the respective button of the pricing table, they directly get transferred to checkout. I have unchecked the following option in Woocommer settings:
- Redirect to the cart page after successful addition
- Enable AJAX add to cart buttons on archives
- All my products are set as variable products with virtual items and can only be purchased one quantity on a single order.
I have also added the following snippet to ensure the users get directly transferred to the checkout page instead of the cart page in the middle.
add_filter('add_to_cart_redirect', function() {
// Remove the default `Added to cart` message
wc_clear_notices();
return wc_get_checkout_url();
});
This approach works fine and takes the user to the checkout page as intended but creates two issues:
Situation 1
The user visit to the services page, click on the respective pricing table, gets transferred to the checkout page. Now the user clicks the browser back button and rethinks about which plan to choose. And then thinks that the plan he chooses was right all along.
Then he again clicks on the pricing table button but this time sees:
You cannot add another "Product X" to your cart.
Situtation 2
The visitor first selects one plan that goes back and after rethinking select another plan. In this case on the checkout page the user getting charged for both the product, the one he first selected by mistakes and then the one he actually selected. Now as there is no cart page, it's hard for the user to go and update the cart.
What I am Looking For
I am just looking to disable the cart functionality all together so that the site doesn't remember what the user has added in the cart. So, if the user select a product goes to the checkout page and then goes back and select another product this problem won't happen was the cart won't remember anything and the cart functionality is disable.
Share Improve this question asked Apr 28, 2020 at 8:08 iSaumyaiSaumya 93619 silver badges36 bronze badges1 Answer
Reset to default 0Not sure if this is the right way to do it but I finally found an answer here: https://stackoverflow/a/49382476/2308992
and did the following:
add_action( 'woocommerce_add_to_cart_validation', function( $passed, $product_id, $quantity ) {
if( ! WC()->cart->is_empty() ) {
wc_empty_cart();
}
return $passed;
}, 20, 3);
What this does basically before WooCommerce tries to add anything in the cart, it simply clears the cart so the cart basically doesn't remember anything. Implementing this has resolved the issue I was facing as mentioned above.
But again if you know a better way of doing this, please share it. I would love to know.