Support » Plugin: WooCommerce » How to clear cart after login?

Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Support maykato

    (@maykato)

    Hello,

    I’d first reach out to the Tutor LMS plugin developer to see if there is a solution for it: https://wordpress.org/support/plugin/tutor/

    Otherwise, I’m not aware of a simple solution to empty cart on user login, but this could be achievable with some custom coding. We recommend getting in touch with a web developer or one of the services listed at https://woocommerce.com/customizations/

    I would take a look at the code snippet provided.

    https://wordpress.org/support/topic/how-to-clear-cart-on-logout-or-just-closing-the-browser-simply/

    function wc_empty_cart_logout() {
        if( function_exists('WC') ){
            WC()->cart->empty_cart();
        }
    }
    add_action('wp_logout', 'wc_empty_cart_logout');
    • This reply was modified 1 month, 2 weeks ago by Luke Cavanagh. Reason: clarify wording
    Thread Starter tigzik

    (@tigzik)

    Hi @lukefiretoss and thank you for your advice.

    I have tried this snippet before. I also tried to change it from logout to login. Neither of the solutions work, unfortunatelly. The cart is not clearing at all after login/logout for the user account.

    Thread Starter tigzik

    (@tigzik)

    Thanks to the community here I have a walkaround solution that works well. It checks if a product was bought by the same user (email or id) in the past and won’t allow to proceed onto the checkout page with this product in the cart.

    function action_woocommerce_check_cart_items() {
        // Retrieve the current user object
        $current_user = wp_get_current_user();
        
        // Initialize
        $flag = false;
        
        // Loop through cart items
        foreach( WC()->cart->get_cart() as $cart_item ) {
            // Check for variantions
            $product_id = $cart_item['variation_id'] > 0 ? $cart_item['variation_id'] : $cart_item['product_id'];
            
            // Checks if a user (by email or ID or both) has bought an item
            if ( wc_customer_bought_product( $current_user->user_email, $current_user->ID, $product_id ) ) {
                // Flag becomes true
                $flag = true;
                
                // Break loop
                break;
            }
        }
        
        // True
        if ( $flag ) {
            // Clear all other notices          
            wc_clear_notices();
    
            // Avoid checkout display an error notice
            wc_add_notice( __( 'My custom error message', 'woocommerce' ), 'error' );
            
            // Optional: remove proceed to checkout button
            remove_action( 'woocommerce_proceed_to_checkout', 'woocommerce_button_proceed_to_checkout', 20 );   
        }
    }   
    add_action( 'woocommerce_check_cart_items' , 'action_woocommerce_check_cart_items', 10, 0 );
Viewing 4 replies - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.