Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Empty Cart WordPress

  • SOLVED

Hi,

I would like to create a kind of link structure that empty cart and add a product to cart.
I would like to replace the add to card button for specific product. The snipped should not change how all the product are added to cart.

Thank you

Answers (2)

2017-09-26

Rempty answers:

Hello
You need something like

global $woocommerce;
$productid=1234:
$woocommerce->cart->empty_cart();
$woocommerce->cart->add_to_cart($productid);


gorbatch comments:

thank you, any code to try out?


Rempty comments:

create a template file
create a page and assign the template

in the template file add this code in the begin of the file
<?php
if(isset($_GET['custom_add_cart']) && $_GET['custom_add_cart']!=''){
global $woocommerce;
$productid=$_GET['custom_add_cart'];
$woocommerce->cart->empty_cart();
$woocommerce->cart->add_to_cart($productid);
}
?>
And now when you load the page like
mypage.com/custom-page/
if you add
mypage.com/custom-page/?custom_add_cart=123456
it will clear the cart and add the product

This only works with normal products.

For variable products or attributes need furher modifications


gorbatch comments:

I get redirected to a page "Your cart is empty"

2017-09-26

mod mi answers:

Please check these updates:

This in functions.php
<?php
if (is_page('1234')){
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');
}
}
}

}
}
?>


gorbatch comments:

thank you, this look like this could be the answer. Is there a way to have a custom url structure like in rempty answer, so I can keep the normal way for other products ?

Thank you


mod mi comments:

You can add a conditional to the hook for a specific product id


gorbatch comments:

I would want this to work on every page and just change the url to activate it for example I could put: /?custom_add_cart=123456 instead of /?add_cart=123456 as the url


mod mi comments:

With a link I'm afraid the problem is the empty_cart function will always fire first and never add the product. So you will just get "Your cart is empty" message.


gorbatch comments:

could I active the gist only on specific pages?


mod mi comments:

You mean on specific product pages?


gorbatch comments:

No, on specific page, I have a custom page with multiple add to cart, and that's these link that I would have love to customize. But if I can run the script on that page only, it could be a workaround.


mod mi comments:

Try this please, replace '1234' with your page id

<?php
add_action ('woocommerce_add_to_cart', 'lenura_empty_cart_before_add', 0);

function lenura_empty_cart_before_add() {
if (is_page('1234') ){
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');
}
}
}

}
}

}
?>


mod mi comments:

Please check my updated answer


gorbatch comments:

I get an error 500 when I put <?php custom_add_to_cart_for_specific_page() ?>

in the template


mod mi comments:

Please check the updated answer where the page is checked within the function.
Delete the function from the template page.


gorbatch comments:

the cart isnt empty, it normally add the product to the cart