The following code snippet will help WooCommerce store owners to provide BOGO discounts/ free products based on the combination of categories present in the cart. The code will implement a 100% discount on products from category Z when products from both categories X and Y are added to the cart.
add_action('woocommerce_cart_calculate_fees', 'category_bogo_discount', 20, 1); add_action('woocommerce_checkout_update_order_review', 'remove_category_bogo_discount'); function category_bogo_discount($cart_object) { if (is_admin() && !defined('DOING_AJAX')) return; // Define the category IDs for products to buy (X and Y) $category_ids_to_buy = array(50, 65); // Categories X and Y // Define the category ID for the free product (Z) $free_product_category_id = 63; // Category Z // Check if both categories X and Y are present in the cart $x_category_present = false; $y_category_present = false; foreach (WC()->cart->get_cart() as $cart_item) { $cart_product_cats_ids = wc_get_product_term_ids($cart_item['product_id'], 'product_cat'); if (in_array(50, $cart_product_cats_ids)) { $x_category_present = true; } if (in_array(63, $cart_product_cats_ids)) { $y_category_present = true; } } // If both categories X and Y are present, apply discount if ($x_category_present && $y_category_present) { $discount_percentage = 100; // 100% discount $discount_amount = 0; // Initialize discount amount // Calculate the discount amount foreach ($cart_object->get_cart() as $cart_item_key => $cart_item) { $cart_product_cats_ids = wc_get_product_term_ids($cart_item['product_id'], 'product_cat'); if (in_array($free_product_category_id, $cart_product_cats_ids)) { $discount_amount += $cart_item['data']->get_price() * ($discount_percentage / 100); } } // Apply discount to cart if ($discount_amount > 0) { $cart_object->add_fee(__('Category Z Discount'), -$discount_amount); } } }
This to the shop owners who are running or planning to run BOGO offers on their WooCommerce store…
BOGO deals are great for increasing your sales, but have you thought about which offers are bringing you more revenue and which offers are not performing that great?
Don’t just set a BOGO deal, track the revenue generated by your deals in real-time with the Flexi BOGO for WooCommerce plugin.
Output
Scenario | Discounted/Free Product | Example |
But products from Categories X and Y | 100% off on Category Z | Add products from Category X and Y to get 100% off on a product from Category Z. |
When customers add products from categories X,Y, and Z to the cart, the code checks for the presence of products from both categories of X and Y. If both categories are present, a 100% discount is applied for the product present in category Z.
Similarly, you can also offer a tiered discount based on some rules applied. Check out this post on how to apply a recursive BOGO rule for the same product in WwooCommerce.