How to clear a Woocommerce cart

后端 未结 8 1692
-上瘾入骨i
-上瘾入骨i 2020-12-17 09:45

I am wondering how you can clear the contents of your cart on page load using woocommerce.

I came accross how to add a clear cart button using by adding this in to f

8条回答
  •  半阙折子戏
    2020-12-17 09:49

    None of the codes above works on my website. I test it in the latest WordPress version 5.4.1 and the below function works perfectly!

    /**
    Clears WC Cart on Page Load
    (Only when not on cart/checkout page)
    */
    
    add_action( 'wp_head', 'wc_clear_cart' );
    function wc_clear_cart() {
        if ( wc_get_page_id( 'cart' ) == get_the_ID() || wc_get_page_id( 'checkout' ) == get_the_ID() ) {
            return;
        }
        WC()->cart->empty_cart( true );
    }
    

提交回复
热议问题