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

Viewed 587

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.

0 Answers
Related