• Resolved kristinubute

    (@kristinubute)


    Hi

    I have been googling for hours, what is the best way for the Woocommerce cart to be cleared on either Log Out or if the client just closes the browser and doesn’t log out.

    I notice that if I just close the browser as a pretend customer then the items in the cart are there the next day… Wouldn’t this affect the stock .. example if client didn’t log out, then items would stay in the cart, then they may decide to purchase it next day and stocks are gone … would rather have it cleared …

    I would prefer to have the cart CLEAR each time either logging out and/OR if they have forgotten to log out and just closed the browser.

    Doesn’t seem to be many options out there that I can find for that. Is there a simple plugin maybe ? I can’t even seem to find that ?

    Thx
    Kristin

Viewing 7 replies - 1 through 7 (of 7 total)
  • Mikey (a11n)

    (@mikeyarce)

    Thread Starter kristinubute

    (@kristinubute)

    HI,

    Yes I had already tried this and doesn’t work for some reason.
    I put it in my functions.php child theme

    Then cleared my cache and tested again as a TEST Customer and logged in and still see my $125 worth of items in the TEST cart …

    I had logged out properly beforehand also.

    Items didn’t go away or didn’t clear on log out.
    What other options do I have ?

    Thx
    Kristin

    Thread Starter kristinubute

    (@kristinubute)

    HI, Can you confirm:

    When a customer purchases from the store and it redirects to Paypal and then redirects back to the site saying “order completed etc” ..

    If the customer doesn’t log out then and there, just closes the browser, does those items still stay in the cart or completely removed as they have been paid and purchased ?

    Just wondering.

    Therefore if a customer is browsing the store, and clicks on “BUY NOW” and adds items to the cart, and then gets busy doesn’t come back, and doesn’t finish the order, and hasn’t logged out, then do the items automatically stay in the cart until next time ?

    Is this a usual way that the Abandoned cart plugin works to backtrack and upsell those items ?

    Or is it best to add a “REMOVE Cart” on logout and avoid issue entirely ?

    What is best practice and what is most popular with ecoommerce shopping carts these days ?

    Please advise best way.

    Thx
    Kristin

    Caleb Burks

    (@icaleb)

    Automattic Happiness Engineer

    Right, so let’s clear up the stock behavior first. As emptying a user’s cart isn’t a good idea and could upset them / lose sales.

    WooCommerce does not reduce stock when an item it added to the cart, at least not by default. Stock is only reduced when the customers finished checkout. If two people have a 1-stock item in their cart, whoever checks out first will get it, and the other will be notified when they try to checkout and it will be removed from their cart automatically.

    If the customer goes to PayPal but does not pay and just leaves the page, stock is reduced because an order was technically created. But if WooCommerce does not hear back from PayPal within X amount of time, it will automatically move the order to the cancelled status and return the stock. The X amount of time is configured at WooCommerce > Settings > Products > Inventory > Hold stock.

    ______

    As for how cart persistency works, a logged out guest user will have item’s saved in the browser cookies. So if they return within a certain amount of time, items will still be there.

    Logged-in users have a persistent cart, meaning once an item is added to the cart it will stay there and will always appear when they login. That is unless an external factor is involved, like you manually clear all user’s sessions on your site.

    I found a solution that worked for me at…

    https://stackoverflow.com/questions/32785328/woocommerce-clear-cart-on-user-logout

    Just add the following to your theme’s ” functions.php ” file :

    function your_function() {
        if( function_exists('WC') ){
            WC()->cart->empty_cart();
        }
    }
    add_action('wp_logout', 'your_function');

    =============

    Let me know if this helps.

    • This reply was modified 6 years, 10 months ago by Pigo3934blog.
    • This reply was modified 6 years, 10 months ago by Pigo3934blog.

    function your_function() {
    if( function_exists(‘WC’) ){
    WC()->cart->empty_cart();
    }
    }
    add_action(‘wp_logout’, ‘your_function’);

    ——————
    Not working, nothing happens.

    sekizikimedya

    (@sekizikimedya)

    here is your solution…

    /*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(); 
    }
    }
Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘How to clear cart on logout or just closing the browser simply’ is closed to new replies.