WooCommerce: Adding Dynamic Discount Programmatically for Variations of a Product: Boost Sales with Bulk Savings

by | Jul 11, 2023 | E-commerce, Tutorials, WooCommerce, WordPress


In the competitive world of online retail, offering attractive discounts can be a powerful strategy to attract customers and boost sales. However, setting up dynamic discounts for specific variations of a product in WooCommerce might require some custom code. In this tutorial, we’ll explore how to programmatically add a discount feature called “Bulk Savings” for pairs of variations in your WooCommerce store. By offering this variation-based discount, you can encourage customers to purchase multiple pairs of variations and enjoy significant savings. Let’s dive in!

Boost Sales With Dynamic Discount In Woocommerce

Step 1: Understanding the Bulk Savings Discount

Before we delve into the code, let’s clarify how the Bulk Savings discount works. Consider a product with ID 656 that has two variations: 657 and 658. If a customer adds a pair of 657 and a pair of 658 to their cart, the code will automatically deduct $10 for each pair, resulting in a Bulk Savings discount. For instance, if the customer adds 3 of 657 and 3 of 658 to their cart, the discount will be applied to the three pairs, totaling a $30 reduction.

Step 2: Code Breakdown

 * Apply "Bulk Savings" discount in WooCommerce cart.
function apply_bulk_savings_discount() {
    if (is_admin() && !defined('DOING_AJAX')) {

    // Set the product IDs for checking the conditions
    $main_product_id = 656;
    $variation_product_ids = array(657, 658);

    // Initialize the counters for each variation
    $variation_counts = array();
    foreach ($variation_product_ids as $variation_product_id) {
        $variation_counts[$variation_product_id] = 0;

    // Calculate the quantity of each variation in the cart
    foreach (WC()->cart->get_cart() as $cart_item) {
        $product_id = $cart_item['product_id'];
        if ($product_id === $main_product_id) {
            $variation_id = $cart_item['variation_id'];
            if (in_array($variation_id, $variation_product_ids)) {
                $variation_counts[$variation_id] += $cart_item['quantity'];

    // Calculate the discount amount based on the variation counts
    $discount_amount = 0;
    $pair_count = min($variation_counts[657], $variation_counts[658]);
    $discount_amount = $pair_count * 10;

    // Apply the discount if it is greater than 0
    if ($discount_amount > 0) {
        WC()->cart->add_fee('Bulk Savings', -$discount_amount, false, '');
add_action('woocommerce_cart_calculate_fees', 'apply_bulk_savings_discount');

To achieve this dynamic discount functionality, we need to implement a custom code snippet. The code hooks into the WooCommerce cart and applies the Bulk Savings discount based on the specified conditions. Here’s a breakdown of the code:

1. Initializing the product and variation IDs

$main_product_id = 656;
$variation_product_ids = array(657, 658);

In this part, we set the main product ID (656) and the variation product IDs (657 and 658) as variables to check the conditions.

2. Counting the variations in the cart

foreach (WC()->cart->get_cart() as $cart_item) {
    // ...

This loop iterates through the cart items and counts the quantity of each variation in the cart, considering only the main product and the specified variations.

3. Calculating the discount amount

foreach ($variation_counts as $variation_product_id => $variation_count) {
    $discount_amount += floor($variation_count / 2) * 10;

Here, we iterate over the variation counts and deduct $10 for each pair of variations found. The discount amount is accumulated accordingly.

4. Applying the discount

if ($discount_amount > 0) {
    WC()->cart->add_fee('Bulk Savings', -$discount_amount, false, '');

If the discount amount is greater than 0, we apply the Bulk Savings discount to the cart by adding a fee labeled “Bulk Savings” with the calculated discount amount.

Step 3: Implementing the Code

To implement the Bulk Savings discount in your WooCommerce store, follow these steps:

  1. Open your theme’s functions.php file or create a custom plugin file.
  2. Copy the provided code snippet into the file.
  3. Save the changes and upload the modified file back to your server.

Step 4: Testing and Customization

After adding the code, it’s crucial to test the functionality thoroughly to ensure it works as expected. Add products with the specified variations to the cart and verify that the Bulk Savings discount is applied correctly. You can also customize the discount amount or the product and variation IDs to fit your specific requirements.


Offering dynamic discounts for specific variations of a product can significantly enhance your WooCommerce store’s appeal and increase customer engagement. By implementing the Bulk Savings discount programmatically, you provide an incentive for customers to purchase multiple pairs of variations, resulting in higher sales and customer satisfaction. The code snippet provided in this tutorial allows you to effortlessly integrate this powerful discount feature into your WooCommerce store.

Remember, implementing custom code should be approached with caution. Backup your files before making any changes, and if you’re not comfortable with coding, consult a developer to assist you. With the Bulk Savings discount in place, you’ll be able to drive more conversions, attract loyal customers, and stand out in the competitive e-commerce landscape.

Need Help With Your Website?

Related Posts

10 Unique Comprehensive CSS Styles for Select Inputs in 2024

10 Unique Comprehensive CSS Styles for Select Inputs in 2024

1. Dark Mode Elegance Select2. Light & Airy3. Bold & Vibrant4. Elegant Minimalist5. Nature Lover6. Neon Glow7. Ocean Breeze8. Tech Savvy9. Playful Pop10. Elegant SimplicityCommon Arrow Code (for all above) For Select Inputs: Customizing <select> inputs...

See these 6 Stylish Input CSS Design in 2024

See these 6 Stylish Input CSS Design in 2024

Web design is constantly progressing, and with it, the elements within a webpage. Among these, input fields are pivotal in engaging users and enhancing their experience. This blog post unfolds six stylish CSS input designs that are not only trendy but are also geared...

Subscribe To Our Newsletter

Join our mailing list to receive the latest news and updates from our team.

You have Successfully Subscribed!

Pin It on Pinterest

Share This