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

How to Offer a WooCommerce Fixed Discount on Category Z Based on Quantity of Categories X and/or Y?

This customization will be useful for online stores that sell different categories of products such as smartphones (Category X), laptops (Category Y), and accessories (Category Z). Such deals across different categories will incentivize customers to buy more, which leads to an increase in your store’s average order value.

Solution: Fixed Discount on Category Z Based on Quantity of Categories X and/or Y

The code snippet helps to offer a discount to products of category Z when customers purchase any combination of three smartphones and/or laptops from categories X and/or Y.

add_action('template_redirect', 'ts_add_product_to_cart');

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

        $category_id_x = 50; // Category ID for category X
        $category_id_y = 65; // Category ID for category Y
        $category_id_z = 63; // Category ID for category Z

        $targeted_qty_combined = 3; // Targeted quantity for combined categories X and Y

        $discount_percentage = 20; // Discount percentage for category Z

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

        // Count the quantity of items in Category X and Category Y
        $category_qty_combined = 0;

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

            if (!is_admin() && (in_array($category_id_x, $product_cats_id_incart) || in_array($category_id_y, $product_cats_id_incart))) {
                // Increment the quantity if the product is in Category X or Category Y
                $category_qty_combined += $cart_item['quantity'];
            }
        }

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

                if (!is_admin() && in_array($category_id_z, $product_cats_id_incart_z)) {
                    // Apply the discount percentage to each item in Category Z
                    $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/Free ProductExample
3 products total from Category X and/or YApply a discount to products from Category ZAdd any combination of 3 products from Category X and/or Y to receive a discount on products from Category Z

Let’s assume that the products from the categories of X, Y, and Z are added to the cart. Now we will look into the different possible scenarios and the outputs when these categories are added to the cart.

  • The customer adds three smartphones to the cart (Category X)

It’s important to note that the code defines a targeted combined quantity ($targeted_qty_combined = 3) for categories X and Y collectively, as well as individually.
If the customer adds 3 smartphones( Category X) to the cart, the code applies a discount amount of 20% (specified in the code) to any accessories (Category Z) in the cart.

WooCommerce Fixed Discount
  • The customer adds three laptops to the cart (Category Y)

If the customer adds products only from category Y but in the quantity of 3, then the discount gets applied to the products of category Z (laptops).

How to Offer a WooCommerce Fixed Discount on Category Z Based on Quantity of Categories X and/or Y? - Tyche Softwares

  • The customer adds two laptops and one smartphone to the cart (Category Y and X)

If the combined quantity of laptops and smartphones added to the cart is 3, then the discount gets applied to any product available from the category Z in the cart.

How to Offer a WooCommerce Fixed Discount on Category Z Based on Quantity of Categories X and/or Y? - Tyche Softwares

To explore similar BOGO discounts you can also check on how to apply a discount on category Y based on a fixed quantity of items from category X in WooCommerce.

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