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

How to Offer a WooCommerce BOGO Deal: 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). 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();
        }
    }
}

Output

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.

How to Offer a WooCommerce BOGO Deal: Fixed Discount on Category Z Based on Quantity of Categories X and/or Y? - Tyche Softwares
  • 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 BOGO Deal: 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 BOGO Deal: 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
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x