Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
WooCommerce Hook: Empty cart before adding a new product to cart WITHOUT throwing woocommerce_cart_is_empty
<?php
/**
* Hook: Empty cart before adding a new product to cart WITHOUT throwing woocommerce_cart_is_empty
*/
add_action ('woocommerce_add_to_cart', 'lenura_empty_cart_before_add', 0);
function lenura_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;
}
error_log('prodID: ' . $prodId); // FIXME
error_log('prodQty: ' . $prodQty); // FIXME
// 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');
}
}
}
}
}
?>
@bartjones

This comment has been minimized.

Copy link

@bartjones bartjones commented Oct 11, 2016

Thank you for this. I've been hunting for this for the last 2 hours. Works like a charm. I offer a bonus version of a product that is only available for 6 days. The timer plugin redirects to the normal price sales page after that, but that didn't guarantee that the cart would be empty of the reduced price version.

In other words this is perfect if you use evergreen countdown timers and woocommerce.

Thank you!!

@briangelhaus

This comment has been minimized.

Copy link

@briangelhaus briangelhaus commented Jul 19, 2017

thanks dude!

@lutifyme

This comment has been minimized.

Copy link

@lutifyme lutifyme commented Aug 17, 2017

Thank you! This is perfect. We use the /checkout/?add-to-cart=123 links for one-click checkout for our clients. However, if their order fails for whatever reason or they simply decide to abandon the cart only to get back to it later, they'll usually just click once again the /checkout/?add-to-cart=123 button to start all over effectively adding additional copy to their cart. Surprisingly, many of them never notice additional product in the cart until they receive the 'Order is complete' email. This is a perfect solution for us as it prevents this from happening. Thanks a bunch! We installed this as a simple plugin and it's working flawlessly.

@sadue

This comment has been minimized.

Copy link

@sadue sadue commented Sep 24, 2017

Please how do I install this plugin, or to add this code.
I downloaded the zip file, went to my plugin page and click add new, upload, install. But I got the following error...

Unpacking the package…

Installing the plugin…

The package could not be installed. No valid plugins were found.

Plugin install failed.

please help me out.

@cezarderevlean

This comment has been minimized.

@chstappert

This comment has been minimized.

Copy link

@chstappert chstappert commented Apr 28, 2018

Thanks a lot!

@viczx

This comment has been minimized.

Copy link

@viczx viczx commented Jun 2, 2019

great post!!! thanks

@nstyleinternational

This comment has been minimized.

Copy link

@nstyleinternational nstyleinternational commented Jun 6, 2020

Excellent. Thanks alot

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment