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

How to Apply Discount on Product B Based on Quantity of Product A in WooCommerce?

This WooCommerce customization is an excellent strategy for increasing sales by incentivizing customers to buy more of a specific product. For instance, if you offer a product bundle in your online store, this feature can encourage customers to purchase the main product, Product A, in larger quantities.

When customers add 5 units of Product A to their cart, they unlock a special offer: a related accessory, Product B, is automatically added at a 50% discount. Let’s see how with these simple WooCommerce snippets we can enhance customer satisfaction by offering great deals on complementary products.

Solution: Apply Discount on Product B Based on Quantity of Product A in WooCommerce

The code checks if the customer has added the required quantity of Product A to their cart. If this condition is met, Product B is automatically added to the cart with a 50% discount. If the conditions are not met, any previously added Product B related to the promotion is removed from the cart.

add_action('template_redirect', 'ts_add_product_to_cart');

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

        $product_a_id = 922;
        $free_product_id = 933;

        // Set the targeted quantity for the parent product
        $targeted_qty_a_id = 5; // Targeted quantity for the parent product
        $discount_percentage = 50; // Discount percentage for the free product

        // Check if the free product is already in the cart
        $free_product_cart_id = WC()->cart->generate_cart_id($free_product_id);
        $free_product_in_cart = WC()->cart->find_product_in_cart($free_product_cart_id);

        $parent_quantity = 0;

        // Loop through cart items
        foreach (WC()->cart->get_cart() as $cart_item_key => $cart_item) {
            // Check if the current product is the parent product
            if ($cart_item['product_id'] == $product_a_id) {
                $parent_quantity = $cart_item['quantity'];
                break; // No need to continue once we've found the parent product
            }
        }

        // Check if the quantity of the parent product meets the condition
        if ($parent_quantity == $targeted_qty_a_id) {
            // If the free product is not in the cart or the cart has been updated, add it with a discount
            if (!$free_product_in_cart || isset($_POST['update_cart'])) {
                // Remove the free product from the cart to ensure the discount is applied correctly
                ts_remove_free_product($free_product_id);

                // Add the free product to the cart
                WC()->cart->add_to_cart($free_product_id);

                // Apply a 50% discount on the added free product
                foreach (WC()->cart->get_cart() as $key => $cart_item) {
                    if ($cart_item['product_id'] == $free_product_id) {
                        $discount_amount = $cart_item['data']->get_price() * $discount_percentage / 100;
                        $cart_item['data']->set_price($cart_item['data']->get_price() - $discount_amount);
                    }
                }

                // Display a notice
                wc_add_notice('Complimentary gift added! Enjoy a 50% discount on the free product.', 'success');
            }
        } else {
            // If the quantity condition is not met, remove the free product
            ts_remove_free_product($free_product_id);
        }
    }
}

function ts_remove_free_product($free_product_id) {
    global $woocommerce;

    // Check if free product is in the cart
    foreach ($woocommerce->cart->get_cart() as $cart_item_key => $values) {
        $_product = $values['data'];
        if ($_product->get_id() == $free_product_id) {
            // Remove the free product
            $woocommerce->cart->remove_cart_item($cart_item_key);

            // Display a notice
            wc_add_notice('Complimentary gift removed.', 'notice');
        }
    }
}

Output

This is the original price of the complimentary product added to the cart with the discounted amount.

How to Apply Discount on Product B Based on Quantity of Product A in WooCommerce? - Tyche Softwares
Scenario
Discounted Product
Example
Buy Product A as per the targeted QuantityProduct B at 50% OffAdd 5 units of Product A to get Product B automatically added to the cart at 50% off.
Condition Not Met
Product B Removed
If Product A quantity falls below 5, Product B is removed from the cart.


Whenever the parent product with a quantity of 5 is in the cart, the free gift is added with a 50% discount, and the notice reflects this action.

How to Apply Discount on Product B Based on Quantity of Product A in WooCommerce? - Tyche Softwares

Either if the quantity is increased or decreased and when the quantity of the parent product is not within the specified range then the auto-added product B is removed from the cart.

Similarly, you can implement various tactics for WooCommerce cart discounts such as adding dynamic bulk WooCommerce discount tiers based on quantity that will tie the discounts to a quantity range.

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