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

How to Offer a 20% Discount on the Next Quantity of Product A Based on WooCommerce Cart Total?

The goal of this customization is to add more quantities of a particular product A by providing an additional discount once the defined subtotal threshold is met. The code implements a dynamic discount based on the quantity of a specific product (Product A) in the WooCommerce cart and the total cart subtotal.

// Hook to apply discount based on cart subtotal
add_action('woocommerce_before_calculate_totals', 'ts_apply_discount_based_on_subtotal');

function ts_apply_discount_based_on_subtotal($cart) {
    if (is_admin() && !defined('DOING_AJAX')) {
        return;
    }

    // YOUR SETTINGS:
    $targeted_product_id = 730; // Set HERE your targeted product ID
    $discount_percentage = 20;
    $subtotal_threshold = 100;

    $product_a_quantity = 0;
    $subtotal = 0;
    $discount_applied = false;

    // Loop through cart items to calculate quantities and subtotal
    foreach ($cart->get_cart() as $cart_item) {
        if ($cart_item['product_id'] == $targeted_product_id) {
            $product_a_quantity += $cart_item['quantity'];
        }

        $subtotal += $cart_item['line_total'] + $cart_item['line_tax'];
    }

    // Check if product A is in the cart, subtotal is close to the threshold, and discount has not been applied
    if ($product_a_quantity > 0 && $subtotal > ($subtotal_threshold - $discount_percentage) && !$discount_applied) {
        // Display notice on the cart and checkout pages
        wc_clear_notices(); // clear other notices on the checkout page.
        if (is_cart() || is_checkout()) {
            wc_add_notice(sprintf(__('Add the next quantity & get 20%% discount. Only %s away from the threshold!'), wc_price($subtotal_threshold - $subtotal)), 'notice');
        }
    }

    // Check if product A is in the cart, subtotal exceeds the threshold, and discount has not been applied
    if ($product_a_quantity > 0 && $subtotal > $subtotal_threshold && !$discount_applied) {
        // Apply the discount to the next quantity of product A
        foreach ($cart->get_cart() as $cart_item) {
            if ($cart_item['product_id'] == $targeted_product_id) {
                // Calculate the next quantity of product A
                $next_quantity = $cart_item['quantity'] + 1;

                // Calculate the discount amount for the next quantity of product A
                $discount_amount = $cart_item['data']->get_price() * ($discount_percentage / 100);

                // Apply the discount by reducing the price for the next quantity
                $new_price = max(0, $cart_item['data']->get_price() - $discount_amount);
                $cart_item['data']->set_price($new_price);

                // Set the flag to indicate that the discount has been applied
                $discount_applied = true;

                // Add a discount fee to the cart (optional, based on your requirement)
                $cart->add_fee(__('Discount for Product A', 'your-text-domain'), -$discount_amount, true);

                // Display notice on the cart and checkout pages
                wc_clear_notices(); // clear other notices on the checkout page.
                if (is_cart() || is_checkout()) {
                    wc_add_notice(__("Congrats! You have got a 20% discount"), 'notice');
                }

                // Break out of the loop once the discount is applied
                break;
            }
        }
    }
}

Output

If a customer adds Product A to the cart, checks if the subtotal (including all line items) exceeds the subtotal threshold, and if it does, apply a 20% discount to Product when the next quantity of product A is added to the cart. Also, display a notice on the cart page, indicating how close the customer is to getting a 20% discount on Product A.

How to Offer a 20% Discount on the Next Quantity of Product A Based on WooCommerce Cart Total? - Tyche Softwares

Wwhen the subtotal threshold is met, the discount of 20%is applied to the price of Product A as shown below.

How to Offer a 20% Discount on the Next Quantity of Product A Based on WooCommerce Cart Total? - Tyche Softwares

Alternatively, you can also add buy one get one offers based on WooCommerce cart total ranges 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