Title: viniciusrtf's Replies | WordPress.org

---

# viniciusrtf

  [  ](https://wordpress.org/support/users/viniciusrtf/)

 *   [Profile](https://wordpress.org/support/users/viniciusrtf/)
 *   [Topics Started](https://wordpress.org/support/users/viniciusrtf/topics/)
 *   [Replies Created](https://wordpress.org/support/users/viniciusrtf/replies/)
 *   [Reviews Written](https://wordpress.org/support/users/viniciusrtf/reviews/)
 *   [Topics Replied To](https://wordpress.org/support/users/viniciusrtf/replied-to/)
 *   [Engagements](https://wordpress.org/support/users/viniciusrtf/engagements/)
 *   [Favorites](https://wordpress.org/support/users/viniciusrtf/favorites/)

 Search replies:

## Forum Replies Created

Viewing 7 replies - 1 through 7 (of 7 total)

 *   Forum: [Reviews](https://wordpress.org/support/forum/reviews/)
    In reply to:
   [[WordPress Social Login] This plugin is not working along with WordPress Social Invitations – Lite Plugin](https://wordpress.org/support/topic/this-plugin-is-not-working-along-with-wordpress-social-invitations-lite-plugin/)
 *  [viniciusrtf](https://wordpress.org/support/users/viniciusrtf/)
 * (@viniciusrtf)
 * [11 years, 3 months ago](https://wordpress.org/support/topic/this-plugin-is-not-working-along-with-wordpress-social-invitations-lite-plugin/#post-7941822)
 * I actually like this type of error messages, it’s way better than technical “
   bla bla bla whatever() on line 666”, and shows that this is a known issue, so
   you don’t have to lose time opening a bug ticket.
 *   Forum: [Plugins](https://wordpress.org/support/forum/plugins-and-hacks/)
    In
   reply to: [[WooCommerce] How to empty cart before adding the new product to the cart in woocommerce](https://wordpress.org/support/topic/how-to-empty-cart-before-adding-the-new-product-to-the-cart-in-woocommerce-1/)
 *  [viniciusrtf](https://wordpress.org/support/users/viniciusrtf/)
 * (@viniciusrtf)
 * [11 years, 9 months ago](https://wordpress.org/support/topic/how-to-empty-cart-before-adding-the-new-product-to-the-cart-in-woocommerce-1/#post-4666349)
 * [@wisdmlabs](https://wordpress.org/support/users/wisdmlabs/) and [@terrytsang](https://wordpress.org/support/users/terrytsang/)
   your code seems to be the finest solution. Thanks a lot. How do I exclude the
   old snippets I’ve posted here?
 *   Forum: [Reviews](https://wordpress.org/support/forum/reviews/)
    In reply to:
   [[WP safely disable directory browsing] Doesn't block access to files](https://wordpress.org/support/topic/doesnt-block-access-to-files/)
 *  [viniciusrtf](https://wordpress.org/support/users/viniciusrtf/)
 * (@viniciusrtf)
 * [11 years, 11 months ago](https://wordpress.org/support/topic/doesnt-block-access-to-files/#post-7824225)
 * Let’s talk about not being a dick and making the world a better place. You could
   start by forking the project code into a better one, actually implementing the
   behavior you need. That would be a contribution. Placing low ratings on plugins(
   coded by a volunteer btw) because they don’t do things they don’t even intend,
   it is not.
 *   Forum: [Reviews](https://wordpress.org/support/forum/reviews/)
    In reply to:
   [[WP safely disable directory browsing] Doesn't block access to files](https://wordpress.org/support/topic/doesnt-block-access-to-files/)
 *  [viniciusrtf](https://wordpress.org/support/users/viniciusrtf/)
 * (@viniciusrtf)
 * [11 years, 11 months ago](https://wordpress.org/support/topic/doesnt-block-access-to-files/#post-7824223)
 * The plugin is called “WP safely disable directory **browsing**“. I don’t even
   have to install it to find out that this plugin does exactly what it intends 
   to do, you said yourself. The problem is that you’ve just decided that this plugin
   should do more, even if its basic documentation makes clear that **it does not**.
 * Read first, complain later.
 * Do we have a deal?
 *   Forum: [Plugins](https://wordpress.org/support/forum/plugins-and-hacks/)
    In
   reply to: [[WooCommerce] How to empty cart before adding the new product to the cart in woocommerce](https://wordpress.org/support/topic/how-to-empty-cart-before-adding-the-new-product-to-the-cart-in-woocommerce-1/)
 *  [viniciusrtf](https://wordpress.org/support/users/viniciusrtf/)
 * (@viniciusrtf)
 * [11 years, 11 months ago](https://wordpress.org/support/topic/how-to-empty-cart-before-adding-the-new-product-to-the-cart-in-woocommerce-1/#post-4666332)
 * It is important to mention that I had some issues using the code above with products
   configured to be selled individually.
 *   Forum: [Plugins](https://wordpress.org/support/forum/plugins-and-hacks/)
    In
   reply to: [[WooCommerce] How to empty cart before adding the new product to the cart in woocommerce](https://wordpress.org/support/topic/how-to-empty-cart-before-adding-the-new-product-to-the-cart-in-woocommerce-1/)
 *  [viniciusrtf](https://wordpress.org/support/users/viniciusrtf/)
 * (@viniciusrtf)
 * [11 years, 11 months ago](https://wordpress.org/support/topic/how-to-empty-cart-before-adding-the-new-product-to-the-cart-in-woocommerce-1/#post-4666331)
 * Here it is some improvements to the first code. I’m sure there are better ways
   to catch the GET and POST requests, as well better ways of using array functions
   from PHP, but this is the best I can do right now.
 *     ```
       /**
        * Hook: Empty cart before adding a new product to cart WITHOUT throwing woocommerce_cart_is_empty
        */
       add_action ('woocommerce_add_to_cart', 'woocommerce_empty_cart_before_add', 0);
   
       function woocommerce_empty_cart_before_add() {
           global $woocommerce;
   
           // Get 'product_id' and 'quantity' for the current woocommerce_add_to_cart operation
           if (isset($_GET["add-to-cart"])) {
               $prodId = (int)$_GET["add-to-cart"];
           } else if (isset($_POST["add-to-cart"])) {
               $prodId = (int)$_POST["add-to-cart"];
           } else {
               $prodId = null;
           }
           if (isset($_GET["quantity"])) {
               $prodQty = (int)$_GET["quantity"] ;
           } else if (isset($_POST["quantity"])) {
               $prodQty = (int)$_POST["quantity"];
           } else {
               $prodQty = 1;
           }
   
           // If cart is empty
           if ($woocommerce->cart->get_cart_contents_count() == 0) {
   
               // Simply add the product (nothing to do here)
   
           // If cart is NOT empty
           } else {
   
               $cartQty = $woocommerce->cart->get_cart_item_quantities();
               $cartItems = $woocommerce->cart->cart_contents;
   
               // Check if desired product is in cart already
               if (array_key_exists($prodId,$cartQty)) {
   
                   // Then first adjust its quantity
                   foreach ($cartItems as $k => $v) {
                       if ($cartItems[$k]['product_id'] == $prodId) {
                           $woocommerce->cart->set_quantity($k,$prodQty);
                       }
                   }
   
                   // And only after that, set other products to zero quantity
                   foreach ($cartItems as $k => $v) {
                       if ($cartItems[$k]['product_id'] != $prodId) {
                           $woocommerce->cart->set_quantity($k,'0');
                       }
                   }
               }
           }
       }
       ```
   
 *   Forum: [Plugins](https://wordpress.org/support/forum/plugins-and-hacks/)
    In
   reply to: [[WooCommerce] How to empty cart before adding the new product to the cart in woocommerce](https://wordpress.org/support/topic/how-to-empty-cart-before-adding-the-new-product-to-the-cart-in-woocommerce-1/)
 *  [viniciusrtf](https://wordpress.org/support/users/viniciusrtf/)
 * (@viniciusrtf)
 * [12 years, 1 month ago](https://wordpress.org/support/topic/how-to-empty-cart-before-adding-the-new-product-to-the-cart-in-woocommerce-1/#post-4666319)
 * In case you’re seeking for a PHP-based solution, put this on your theme’s functions.
   php:
 *     ```
       add_action ('woocommerce_add_to_cart', 'woocommerce_empty_cart_before_add', 0);
   
       function woocommerce_empty_cart_before_add() {
           global $woocommerce;
   
           // Get 'product_id' and 'quantity' for the current woocommerce_add_to_cart operation
           $prodId = (int)$_POST["add-to-cart"];
           $prodQty = (int)$_POST["quantity"];
   
           // Get the total of items for each product in cart
           $cartQty = $woocommerce->cart->get_cart_item_quantities();
   
           // Empty cart before adding the new product to the cart
           if ($cartQty[$prodId] != $prodQty) {
               $woocommerce->cart->empty_cart();
               $woocommerce->cart->add_to_cart($prodId,$prodQty);
           }
       }
       ```
   
 * We do not get an infinite loop, because `($cartQty[$prodId] != $prodQty)` fails
   when `$woocommerce->cart->add_to_cart()` trigger the `woocommerce_add_to_cart`
   action for the second time.

Viewing 7 replies - 1 through 7 (of 7 total)