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

How to 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();
        }
    }
}

Output

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.

How to 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.

How to 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
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x