php - Woocommerce Remove all Items from cart on New Session -


i'm want clear items in cart when new session started. i've tried

add_action( 'init', 'clear_cart_on_it' ); function clear_cart_on_it() {     global $woocommerce;     $woocommerce->cart->empty_cart();  } 

it throwing error:

fatal error: call member function empty_cart() on null in /home/shuggapa/public_html/wp-content/plugins/candy-scoops/scoops.php on line 53 

i have no idea why. please how can implement this.

in code, init hook triggered before woocommerce object instantiated. can use hook:

add_action( 'template_redirect', 'clear_cart_on_it' );  

or

add_action( 'wp_loaded', 'clear_cart_on_it' ); 

Comments