Skip to content
Blog Careers Google Partner 12+ Years · 5000+ Projects
All January 18, 2021 1 min read

Clear cart data before adding new

How to clear cart data before adding new item to the cart ?

Solution:

  1. Login to your wordpress backend.
  2. Go to apperance > Theme Editor > functions.php
  3. Add the following code/* Clear cart data before adding new */
    // before addto cart, only allow 1 item in a cart
    add_filter( ‘woocommerce_add_to_cart_validation’, ‘woo_custom_add_to_cart_before’ );function woo_custom_add_to_cart_before( $cart_item_data ) {global $woocommerce;
    $woocommerce->cart->empty_cart();// Do nothing with the data and return
    return true;
    } 
  4. Save it . It works fine with all versions of WordPress and woo-commerce plugin.

 

 

Keep Reading

Related Articles

View All Articles