Woocommerce – Add free gift for every X amount spent in selected categories
Woocommerce – Add free gift for every X amount spent in selected categories
On our Woocommerce store, we want to give buyers a Free Gift for every $50 spent in selected categories - below is our code in functions.php, it works but not exactly good enough.
The problem that we face now is that when the buyer spends $50.50 or $51 or $52, Woocommerce does not add the Free Gift into the cart. Instead, Woocommerce will only add it when the amount is slightly more, like $57 or more, or when there is additional product quantities above the $50 target.
The code's current problem is that it does not kick in and add 1 Free Gift when the target spend of $50 is met
Please help
```
// Free Gift - Auto Add To Cart //
add_action('woocommerce_before_calculate_totals' , 'freegift_q12024', 10, 1);
function freegift_q12024( $cart_object ){
global $woocommerce;
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
$category_slug=array('categoryA', 'categoryB', 'categoryC', 'categoryD', 'categoryE', 'categoryF'); // Category slugs
$product_gifted_id=54657; // Gift Product ID
$gift_on_spent=50; // Every X Spend
$total_gift=0;
$total_gift_exits=0;
$total_sub_gift=0;
$cart_items_total = 0; // Initializing
foreach ($cart_object->get_cart() as $key => $cart_item) {
if ( has_term( $category_slug, 'product_cat', $cart_item['product_id'] ) ) {
if($cart_item['product_id']!=$product_gifted_id){
$total_sub_gift=$total_sub_gift+$cart_item['line_total'];
}
if($cart_item['product_id']=$product_gifted_id){
$total_gift_exits=$cart_item['quantity'];
}
}
}
if($total_sub_gift>0){
$total_gift=floor(($total_sub_gift/$gift_on_spent));
}
if($total_gift_exits!=$total_gift){
$product_gifted_cart_id = WC()->cart->generate_cart_id( $product_gifted_id );
$product_gifted_in_cart = WC()->cart->find_product_in_cart( $product_gifted_cart_id );
if ($product_gifted_in_cart ){ WC()->cart->remove_cart_item( $product_gifted_in_cart ); }
$product_cart_id = WC()->cart->generate_cart_id( $product_gifted_id );
if( ! WC()->cart->find_product_in_cart( $product_cart_id ) ){
if ($total_gift>0){ WC()->cart->add_to_cart( $product_gifted_id,$total_gift);}
}
}
if($total_gift>0){
foreach ($cart_object->get_cart() as $key => $cart_item) {
if($cart_item['product_id']==$product_gifted_id){
$cart_item['data']->set_price(0);
$cart_item['data']->set_regular_price(0);
}
}
}
}
// Free Gift - Disable remove from cart //
add_filter('woocommerce_cart_item_remove_link', 'freegift_q12024_noremovefromcart', 20, 2 );
function freegift_q12024_noremovefromcart( $button_link, $cart_item_key ){
//SET HERE your specific products IDs
$targeted_products_ids = array(54657);
// Get the current cart item
$cart_item = WC()->cart->get_cart()[$cart_item_key];
// If the targeted product is in cart we remove the button link
if( in_array($cart_item['data']->get_id(), $targeted_products_ids) )
$button_link = '';
return $button_link;
}
```
The code above works - it will auto-add the Free Gift, but the problem is that it won't auto-add at the exact $50 spend target and needs more spending or item quantity in order to initialize (which is the problem)
The issue you're encountering with your WooCommerce code for adding a gift seems to be related to the calculation of the total amount spent
here is the portion of your code you should change and try
if (is_admin() && !defined('DOING_AJAX'))
return;
$category_slug = array('categoryA', 'categoryB', 'categoryC', 'categoryD', 'categoryE', 'categoryF'); // Category slugs
$product_gifted_id = 54657; // Gift Product ID
$gift_on_spent = 50; // Every X Spend
$total_gift = 0;
$total_gift_exits = 0;
$total_sub_gift = 0;
foreach ($cart_object->get_cart() as $cart_item) {
if (has_term($category_slug, 'product_cat', $cart_item['product_id'])) {
if($cart_item['product_id'] != $product_gifted_id){
$total_sub_gift += $cart_item['line_total'];
}
if($cart_item['product_id'] == $product_gifted_id){
$total_gift_exits = $cart_item['quantity'];
}
}
}
if($total_sub_gift >= $gift_on_spent){ // Change from > to >= to include exact $50
$total_gift = floor($total_sub_gift / $gift_on_spent);
}
January 7, 2024
Turbo Multi-language Translator
Make the better internet purchasing globaly
Turbosify SEO Speed Booster
5.0 (7)•Free plan available
Get better conversions by improving store loading speed
Installed
Turbo Multi-language Chat - AI Customer service in one hand
January 7, 2024