1
votes

I know there will be this type of questions but i have tried almost all options without luck so i'm asking here.

I have issue that cart is not empty in woocommerce after order completed and also some users just add items to cart then not go to checkout and after somedays come back again they still see old cart.

I think it may be caching issue as i'm using W3-total-cache plugin.

I want to clear cart data after checkout or if user add items to cart and not checkout and after a while come back again for shopping then cart must be empty and should not store old session to cart.

I tried below code but no luck :

function nom_empty_cart_init_login(){
    $opt = get_option('nom_empty_cart_browser_close_do_login');
    $opt = $opt != 1 ? false : true;

    if( $opt ){
        session_destroy();
    }
}

//  destroy session on user logout
function nom_empty_cart_init_logout(){
    $opt = get_option('nom_empty_cart_browser_close_do_logout');
    $opt = $opt != 1 ? false : true;

    if( $opt ){
        session_destroy();
    }
}

add_action('admin_menu','nom_empty_cart_init_admin_init');
function nom_empty_cart_init_admin_init(){
    add_options_page( 'Woocommerce Clear Cart on Browser Closing', 'WC Clear Cart on Browser Close', 'manage_options', 'wc-clear-cart-on-browser-close', 'wc_clear_cart_on_browser_close' );    
}

function wc_clear_cart_on_browser_close(){

    if( isset( $_REQUEST['save_accconc'] ) and wp_verify_nonce($_REQUEST['wc-clear-cart-on-browser-close-name'],'wc-clear-cart-on-browser-close-action')):

        //  SAVING THE FORM DATA

            //  enable wcccobc
            if( isset($_REQUEST['enable_wcccobc']) )
                update_option('nom_empty_cart_browser_close_enable',1);

            //  enable wcccobc on login
            if( isset($_REQUEST['enable_wcccobc_on_login']) )
                update_option('nom_empty_cart_browser_close_do_login',1);

            //  enable wcccobc on logout
            if( isset($_REQUEST['enable_wcccobc_on_logout']) )
                update_option('nom_empty_cart_browser_close_do_logout',1);


        //  SAVING ;) ENDS

    endif;

    ?>
    <div class="wrap">
        <div class="inside">
            <h2>Woocommerce Clear Cart on Browser Closing</h2>
            <p>Note: the cart will be empty if the visitor close the whole browser, not just the widow. (will be updated soon)</p>

            <form action="<?php admin_url('options-general.php?page=wc-clear-cart-on-browser-close');?>" method="post">
                <?php wp_nonce_field('wc-clear-cart-on-browser-close-action','wc-clear-cart-on-browser-close-name')?>
                <p>
                    <input id="enable_wcccobc" type="checkbox" class="checkbox" name="enable_wcccobc" value="1" <?php checked(get_option('nom_empty_cart_browser_close_enable'),'1');?>>
                    <label for="enable_wcccobc" >Enable clear cart on browser closing</label>                   
                </p>
                <p>
                    <input id="enable_wcccobc_on_login" type="checkbox" class="checkbox" name="enable_wcccobc_on_login" value="1" <?php checked(get_option('nom_empty_cart_browser_close_do_login'),1);?>>
                    <label for="enable_wcccobc_on_login" >Enable clear cart on on user login</label>                    
                </p>
                <p>
                    <input id="enable_wcccobc_on_logout" type="checkbox" class="checkbox" name="enable_wcccobc_on_logout" value="1" <?php checked(get_option('nom_empty_cart_browser_close_do_logout'),1);?>>
                    <label for="enable_wcccobc_on_logout">Enable clear cart on user logout</label>                  
                </p>
                <p>
                    <input type="submit" class="button-primary" value="Save" name="save_accconc">
                </p>
            </form>
        </div>
    </div>
    <?php 
}
1
Regarding the cart not clearing after checkout, have you tried disabling all plugins except woocommerce and using a default theme to ensure it's not a plugin/theme conflict?scottdurban
yes have tried that and also disabled w3 total cache too.raju_odi

1 Answers

3
votes

I'm using method from @shoelaced and it work. edit on theme's functions.php

add_action( 'woocommerce_payment_complete', 'order_received_empty_cart_action', 10, 1 );
function order_received_empty_cart_action( $order_id ){
    WC()->cart->empty_cart();
}