The customization of adding a BOGO (Buy One Get One) offer based on cart weight in WooCommerce can be particularly beneficial for stores that sell products where quantity and weight are significant factors. Customers are more likely to return to the store if they know they can earn discounts based on the cumulative weight of their purchases.
The code will calculate the total cart weight and if the cart weight reaches the threshold weight as specified in the code, the complimentary gift is offered.
add_action('woocommerce_cart_calculate_fees', 'ts_add_free_gift_based_on_cart_weight', 10, 1); function ts_add_free_gift_based_on_cart_weight($cart) { // Set the weight threshold for the free gift $weight_threshold = 10; // Adjust this value to your specific requirement // Get the total cart weight $cart_weight = $cart->cart_contents_weight; // Check if the cart weight is greater than or equal to the threshold if ($cart_weight >= $weight_threshold) { // Set the free gift product ID $free_gift_product_id = 470; // Change this to your actual free gift product ID // Check if the free gift is not already in the cart if (!in_array($free_gift_product_id, array_column($cart->get_cart(), 'product_id'))) { // Add the free gift to the cart $cart->add_to_cart($free_gift_product_id, 1, 0, array(), array('free_gift' => true)); // Displaying a custom notice (optional) wc_clear_notices(); wc_add_notice(__('Free gift added to your cart based on weight!'), 'notice'); } } }
Output
When the customer adds the product to the cart, the cart loops through each product in the cart and adds the product weight to the total cart weight if available. When the total weight of the cart reaches the specified threshold amount, the free product is added in such a scenario.
The following output shows that even if there is only 1 item in the cart and when the product count is incremented, it calculates the total cart weight based on the quantity and adds the free product accordingly.
Similarly, you can also add bogo buy one get one offer based on individual product weight in WooCommerce.