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

Apply a Discount on Category Y Based on a Fixed Quantity of Items from Category X in WooCommerce

In our previous guide, we explored how to apply discounts to products from Category Y when customers purchase items from Category X within a specified quantity range in WooCommerce. In this post, we’re diving into a similar discount strategy but with a focus on offering discounts based on a fixed quantity of items from Category X.

Solution: Apply a Discount on Category Y Based on a Fixed Quantity of Items from Category X in WooCommerce

Store owners can encourage customers to purchase products from a particular category by simply setting quantity thresholds for products from specific categories that are bought. For example, “Buy 4 items from Category X, get 20% off Category Y”.

add_action('template_redirect', 'ts_add_product_to_cart');

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

        $category_id_y = 16; // Category ID for the specified category to be discounted
        $category_id_x = 60; // Category ID for the category to check quantity

        $targeted_qty_cat_id_x = 4; // Targeted quantity for Category ID x

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

        // Count the quantity of items in Category ID x
        $category_qty_x = 0;

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

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

      

        // Check if the quantity condition is met for Category ID x
        if ($category_qty_x >= $targeted_qty_cat_id_x) {
            // Loop through cart items and apply the discount to Category ID y
            foreach (WC()->cart->get_cart() as $inner_cart_item_key => $inner_cart_item) {
                // Check if the product is in Category ID 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_id_y, $product_cats_id_incart_y)) {
                    // Apply a 20% discount for each item in Category ID y
                    $discount_percentage = 20;
                    $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

ScenarioDiscounted ProductExample
Add Specified Quantity from Category X20% off on Category YAdd 5 units of Product A (Category X) to get 20% off on Product B (Category Y) in the cart.

Once the customer adds the specified quantity of products from Category X to their cart, a 20% discount is automatically applied to the line items of Category Y products present in the cart.

Apply a Discount on Category Y Based on a Fixed Quantity of Items from Category X in WooCommerce - Tyche Softwares

If the quantity threshold of category X items are not met, then the discount is not applied to the items of category Y.

Apply a Discount on Category Y Based on a Fixed Quantity of Items from Category X in WooCommerce - Tyche Softwares


Instead of discounts, you can also add a bogo offer for a fixed quantity in WoCommerce that helps to add a complimentary gift automatically to the cart based on the specific product quantity.

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