Black Friday & Cyber Monday SUPER SALE ALL WEEK:
Grab 40% OFF on plugins
Days
Hours
Minutes
Seconds

WooCommerce Buy X Get Y discount by Category

Buy X get Y by category

This customization will provide ‘Category-Based Discounts’ encouraging customers to buy in bulk quantities from one category so that you can highlight a discount in another category. This strategy not only entices customers with appealing offers but also effectively drives higher sales volumes and increases the visibility of targeted categories.

Solution: WooCommerce Buy X (in specified Quantity Range) Get Y discount by Category

The code snippet will dynamically apply the discount to products in Category Y (e.g. Electronic Accessories) when a specific quantity range of products (e.g. 5 to 10 as specified in the code) from Category X (e.g. Electronics) is added to the cart.

add_action('template_redirect', 'buy_x_get_y_discount');

function buy_x_get_y_discount() {
    if (!is_admin()) {
        global $woocommerce;

        $category_x_id = 50; // Category ID for Category X
        $category_y_id = 63; // Category ID for Category Y

        $min_qty_x = 5; // Minimum quantity for Category X
        $max_qty_x = 10; // Maximum quantity for Category X

        // Flag to check if the discount has been applied
        $discount_applied = false;

        // Count the quantity of items in Category X
        $category_x_qty = 0;

        // Loop through cart items
        foreach (WC()->cart->get_cart() as $cart_item_key => $cart_item) {
            // Check if the product is in Category X
            $product_cats_id_incart_x = wc_get_product_term_ids($cart_item['data']->get_id(), 'product_cat');

            if (!is_admin() && in_array($category_x_id, $product_cats_id_incart_x)) {
                // Increment the quantity only if the product is in Category X
                $category_x_qty += $cart_item['quantity'];
            }
        }

        // Check if the quantity condition is met for Category X
        if ($category_x_qty >= $min_qty_x && $category_x_qty <= $max_qty_x) {
            // Loop through cart items and apply the discount to Category Y
            foreach (WC()->cart->get_cart() as $inner_cart_item_key => $inner_cart_item) {
                // Check if the product is in Category Y
                $product_cats_id_incart_y = wc_get_product_term_ids($inner_cart_item['data']->get_id(), 'product_cat');

                if (!is_admin() && in_array($category_y_id, $product_cats_id_incart_y)) {
                    // Apply a discount for each item in Category Y
                    $discount_percentage = 10; // You can set the desired discount percentage
                    $original_price = floatval($inner_cart_item['data']->get_price());
                    $discount_amount = $original_price * $discount_percentage / 100;
                    $new_price = $original_price - $discount_amount;

                    // Set the new discounted price
                    $inner_cart_item['data']->set_price($new_price);

                    // Set the flag to indicate that the discount has been applied
                    $discount_applied = true;
                }
            }
        }

        // If the discount is applied, you may need to update the cart totals
        if ($discount_applied) {
            WC()->cart->calculate_totals();
        }
    }
}
flexi bogo cta banner image


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 Product

Example
Buy products from Category X (Electronics)
with a quantity range between 5 and 10
Discount on products from
Category Y (Electronic Accessories)
Buy 5 to 10 units of items like smartphones from Electronics(category X) to get a discount on accessories like chargers from Electronic Accessories(category Y).

When products from category X (Electronics) are added to the cart the code checks for whether the quantity of items present in this category meets the defined quantity range as given in the code.

  • $min_qty_x = 5; Minimum quantity for Category X.
  •  $max_qty_x = 10; Maximum quantity for Category X.

If it falls within the specified quantity range the discount gets applied to each item of category Y (Electronic Accessories).

If the quantity of the category X items doesn’t meet the specified quantity range then the specified discount amount in the code will not be applied to each item of category Y as shown below.

Move the traditional discounts aside and start offering complimentary gift automatically added to the cart when they purchase a specific quantity of products. This simple rule will be exciting and will also offer a free product based on the minimum and maximum quantity range of a specific product.

Browse more in: Code Snippets, WooCommerce How Tos, WooCommerce Tutorials

Share It:

Subscribe
Notify of
0 Comments
Newest
Oldest
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x