Title: empty cart before add
Last modified: June 2, 2018

---

# empty cart before add

 *  Resolved [amila](https://wordpress.org/support/users/flatworld21/)
 * (@flatworld21)
 * [7 years, 11 months ago](https://wordpress.org/support/topic/empty-cart-before-add/)
 * Hi, I need to empty cart before add_to_cart,
    If I add “WC_Cart::empty_cart()”
   to function add_to_cart in includes/class-wc-cart.php it works.
 * But if I add the following code in functions.php
 *     ```
       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;
       WC_Cart::empty_cart();
       // Do nothing with the data and return
       return true;
       }
       ```
   
 * I get an error in page:
    `jquery.js?ver=1.12.4:4 POST http://www.xxxxxxxx.com/?
   wc-ajax=add_to_cart 500 (Internal Server Error)`
 * I do not want to change core source. How to solve it?
    -  This topic was modified 7 years, 11 months ago by [amila](https://wordpress.org/support/users/flatworld21/).

Viewing 1 replies (of 1 total)

 *  [Thomas Shellberg](https://wordpress.org/support/users/shellbeezy/)
 * (@shellbeezy)
 * Automattic Happiness Engineer
 * [7 years, 11 months ago](https://wordpress.org/support/topic/empty-cart-before-add/#post-10370084)
 * You got the snippet from here, I assume?
    [https://www.hungred.com/how-to/empty-woocommerce-cart-adding-item/](https://www.hungred.com/how-to/empty-woocommerce-cart-adding-item/)
 * I tested that snippet and it works fine for me, no 500 errors for the AJAX calls:
 * [http://cld.wthms.co/wYmdfb](http://cld.wthms.co/wYmdfb)
 * Perhaps try testing with a default theme and just WooCommerce enabled.

Viewing 1 replies (of 1 total)

The topic ‘empty cart before add’ is closed to new replies.

 * ![](https://ps.w.org/woocommerce/assets/icon.svg?rev=3234504)
 * [WooCommerce](https://wordpress.org/plugins/woocommerce/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/woocommerce/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/woocommerce/)
 * [Active Topics](https://wordpress.org/support/plugin/woocommerce/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/woocommerce/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/woocommerce/reviews/)

 * 1 reply
 * 2 participants
 * Last reply from: [Thomas Shellberg](https://wordpress.org/support/users/shellbeezy/)
 * Last activity: [7 years, 11 months ago](https://wordpress.org/support/topic/empty-cart-before-add/#post-10370084)
 * Status: resolved