How to clear a Woocommerce cart

后端 未结 8 1697
-上瘾入骨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

    For triggering only on front page your function needs to look like this:

    add_action( 'init', 'woocommerce_clear_cart_url' );
    function woocommerce_clear_cart_url() {
      global $woocommerce;
    
        if ( is_front_page() && isset( $_GET['empty-cart'] ) ) { 
            $woocommerce->cart->empty_cart(); 
        }
    }
    

    function is_front_page() returns true only on front page of your wordpress site. Also, you might detect any other page with function is_page() where you can pass any page title, ID or slug

提交回复
热议问题