I want to add specifics products in the cart if certain conditions matches. I was able to update cart using woocommerce_before_calculate_totals but it does not update on the mini cart. Only after removing removing products which I am guessing refreshes the mini cart update cart is shown
I am using ACF to get the products IDs
Below is the code I have used to add products to cart:
add_action('woocommerce_before_calculate_totals', 'check_free_gift_product', 10, 1);
function check_free_gift_product($cart)
{
if (is_admin() && !defined('DOING_AJAX'))
return;
if (did_action('woocommerce_before_calculate_totals') >= 2)
return;
$checkout_steps_gift = get_field('checkut_steps_gift', 'option');
if (!empty($checkout_steps_gift)) {
$cart_subtotal = 0;
foreach ($cart->get_cart() as $cart_item) {
$cart_subtotal += $cart_item['line_tax'] + $cart_item['line_total'];
}
$valid_gift_products = [];
$gift_products_in_cart = [];
for ($i = 0; $i < count($checkout_steps_gift); $i++) {
$free_product_ids = $checkout_steps_gift[$i]['cart_gifts'];
$min_price_for_free_product = $checkout_steps_gift[$i]['cart_total'];
$max_price_for_free_product = ($i + 1 < count($checkout_steps_gift)) ? $checkout_steps_gift[$i + 1]['cart_total'] : PHP_INT_MAX;
if ($cart_subtotal >= $min_price_for_free_product && $cart_subtotal < $max_price_for_free_product) {
$valid_gift_products = array_merge($valid_gift_products, $free_product_ids);
}
}
foreach ($valid_gift_products as $free_product_id) {
$product_in_cart = false;
foreach ($cart->get_cart() as $cart_item_key => $cart_item) {
if ($cart_item['product_id'] == $free_product_id) {
$cart_item['data']->set_price(0);
$cart->set_quantity($cart_item_key, 1);
$product_in_cart = true;
}
}
if (!$product_in_cart) {
$cart_item_key = $cart->add_to_cart($free_product_id, 1);
$cart_item = $cart->get_cart_item($cart_item_key);
$cart_item['data']->set_price(0);
}
$gift_products_in_cart[] = $free_product_id;
}
foreach ($cart->get_cart() as $cart_item_key => $cart_item) {
if (!in_array($cart_item['product_id'], $gift_products_in_cart)) {
foreach ($checkout_steps_gift as $free_products) {
$product_ids = $free_products['cart_gifts'];
if (in_array($cart_item['product_id'], $product_ids)) {
$cart->remove_cart_item($cart_item_key);
}
}
}
}
}
}
//Remove All Products if cart subtotal is empty
add_action('woocommerce_after_calculate_totals', 'remove_product_if_subtotal_is_empty', 10, 1);
function remove_product_if_subtotal_is_empty($cart)
{
if (is_admin() && !defined('DOING_AJAX'))
return;
if (did_action('woocommerce_after_calculate_totals') >= 2)
return;
$cart_subtotal = 0;
foreach ($cart->get_cart() as $cart_item) {
$cart_subtotal += $cart_item['line_tax'] + $cart_item['line_total'];
}
if ($cart_subtotal == 0 && $cart->get_cart_contents_count() != 0) {
foreach ($cart->get_cart() as $cart_item_key => $cart_item) {
$cart->remove_cart_item($cart_item_key);
}
}
New contributor
Saugat Karki is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.