I need a code that with 150 subtotal add a gift in cart. I have the following code:
`
/** Gift in Cart */
// Add free gifted product for specific cart subtotal
add_action( 'woocommerce_before_calculate_totals', 'check_free_gifted_product' );
function check_free_gifted_product( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
// Settings
$free_product_id = 7912;
$targeted_subtotal = 150;
$cart_subtotal = 0; // Initializing
// Loop through cart items (first loop)
foreach ( $cart->get_cart() as $cart_item_key => $cart_item ){
// When free product is is cart
if ( $free_product_id == $cart_item['product_id'] ) {
$free_key = $cart_item_key;
$free_qty = $cart_item['quantity'];
$cart_item['data']->set_price(0); // Optionally set the price to zero
} else {
$cart_subtotal += $cart_item['line_total'] + $cart_item['line_tax'];
}
}
// If subtotal match and free product is not already in cart, add it
if ( ! isset($free_key) && $cart_subtotal >= $targeted_subtotal ) {
$cart->add_to_cart( $free_product_id );
}
// If subtotal doesn't match and free product is already in cart, remove it
elseif ( isset($free_key) && $cart_subtotal < $targeted_subtotal ) {
$cart->remove_cart_item( $free_key );
}
// Keep free product quantity to 1.
elseif ( isset($free_qty) && $free_qty > 1 ) {
$cart->set_quantity( $free_key, 1 );
}
}
`
When I apply a discount and the subtotal of order is less than subtotal targeted the gift is removed by the order in checkout but the gift should remain also if I apply a discount.
I modify the code in this way but it doesn’t work:
`
add_action( 'woocommerce_before_calculate_totals', 'check_free_gifted_product' );
function check_free_gifted_product( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
// Settings
$free_product_id = 7912;
$targeted_subtotal = 150;
$cart_subtotal = 0; // Initializing
$applied_coupons = WC()->cart->get_applied_coupons(); // Get applied coupon codes
$free_product_in_cart = false; // Flag to check if free product is in cart
// Loop through cart items (first loop)
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ){
// When free product is is cart
if ( $free_product_id == $cart_item['product_id'] ) {
$free_product_in_cart = true;
$free_qty = $cart_item['quantity'];
$cart_item['data']->set_price(0); // Optionally set the price to zero
} else {
$cart_subtotal += $cart_item['line_total'] + $cart_item['line_tax'];
}
}
// Store the original cart subtotal before applying the coupon discounts
$original_subtotal = $cart_subtotal;
// Loop through applied coupon codes and apply discounts to the cart subtotal
foreach ( $applied_coupons as $code ) {
$coupon = new WC_Coupon( $code );
$discount_amount = $coupon->get_discount_amount( $cart_subtotal );
$cart_subtotal -= $discount_amount;
}
// If original subtotal match and free product is not already in cart, add it
if ( ! $free_product_in_cart && $original_subtotal >= $targeted_subtotal ) {
$cart->add_to_cart( $free_product_id );
}
// If original subtotal doesn't match and free product is already in cart, remove it
elseif ( $free_product_in_cart && $original_subtotal < $targeted_subtotal ) {
$cart->remove_cart_item( $free_product_id );
}
// Keep free product quantity to 1.
elseif ( $free_product_in_cart && $free_qty > 1 ) {
$cart->set_quantity( $free_product_id, 1 );
}
}
`

