John Davidson

php - How to clear cart contents only after visiting and leaving Checkout page in Woocommerce?

0 comments
Message:


I've been dealing with this issue quite a while, and i hope someone can help me, i came across with multiple solutions but they are not the right ones.... the nearest solution i've found is this one (posted here in stack overflow):


/*empty cart if user come to homepage*/
add_action( 'init', 'woocommerce_clear_cart_url' );
function woocommerce_clear_cart_url() {
global $woocommerce;

if ($_SERVER['REQUEST_URI'] === '/') {
$woocommerce->cart->empty_cart();
}
}

The only problem with this solution is that every time a costumer hits the homepage, his cart will be deleted, even if he closes the window, the cart also empties, so he has to choose his items again, and woocommerce's abandoned cart retention function is no longer useful. What i want to achieve is that if the costumer chooses an item, and he navigates my site to look for something else, even if he goes back to the homepage for whatever reason or leaves my site and return after to complete his order, the cart keeps the items, but.... once he goes to the checkout page, and if he leaves that page for any reason, in that moment the cart empties, so he has to go thru the whole process again.


I'am a high level noob, i quite understand snippets, used them and tweaked them in order to achieve certain functions but this one is impossible to me, and i don't even know if this is possible. So any help would be awesome.



My Answer:
To achieve this functionality, you can use a combination of session variables and WooCommerce hooks. Here's a step-by-step guide on how to clear the cart contents only after visiting and leaving the Checkout page in WooCommerce:

1. Add the following code to your theme's functions.php file to set a session variable when the user visits the Checkout page:

php
add_action( 'template_redirect', 'set_checkout_page_session' );
function set_checkout_page_session() {
if ( is_checkout() ) {
WC()->session->set( 'checkout_page_visited', true );
}
}


2. Add the following code to your theme's functions.php file to clear the cart contents if the user leaves the Checkout page:

php
add_action( 'template_redirect', 'clear_cart_on_checkout_page_exit' );
function clear_cart_on_checkout_page_exit() {
if ( WC()->session->get( 'checkout_page_visited' ) && ! is_checkout() ) {
WC()->cart->empty_cart();
WC()->session->__unset( 'checkout_page_visited' );
}
}


With these code snippets in place, the cart contents will only be cleared if the user visits the Checkout page and then leaves it without completing the order. If the user navigates away from the Checkout page and then returns, the cart contents will remain intact.

I hope this helps! Let me know if you have any questions.

Rate this post

4 of 5 based on 1706 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us