PHPwoocommerceadvanced

WooCommerce Bulk Discounts and Quantity Pricing

Implement bulk discounts, quantity-based pricing, and tiered discounts in WooCommerce

Faisal Yaqoob
#woocommerce#pricing#discounts#bulk-pricing#promotions
Share this snippet:

Code

php
1// Apply bulk discount based on quantity
2add_action('woocommerce_before_calculate_totals', 'apply_bulk_discount');
3function apply_bulk_discount($cart) {
4 if (is_admin() && !defined('DOING_AJAX')) {
5 return;
6 }
7
8 if (did_action('woocommerce_before_calculate_totals') >= 2) {
9 return;
10 }
11
12 foreach ($cart->get_cart() as $cart_item_key => $cart_item) {
13 $product = $cart_item['data'];
14 $quantity = $cart_item['quantity'];
15 $original_price = $product->get_regular_price();
16
17 // Define discount tiers
18 $discount_percentage = 0;
19
20 if ($quantity >= 100) {
21 $discount_percentage = 25; // 25% off for 100+
22 } elseif ($quantity >= 50) {
23 $discount_percentage = 20; // 20% off for 50-99
24 } elseif ($quantity >= 20) {
25 $discount_percentage = 15; // 15% off for 20-49
26 } elseif ($quantity >= 10) {
27 $discount_percentage = 10; // 10% off for 10-19
28 } elseif ($quantity >= 5) {
29 $discount_percentage = 5; // 5% off for 5-9
30 }
31
32 if ($discount_percentage > 0) {
33 $discount_amount = ($original_price * $discount_percentage) / 100;
34 $new_price = $original_price - $discount_amount;
35
36 $product->set_price($new_price);
37
38 // Store discount info for display
39 $cart_item['bulk_discount'] = $discount_percentage;
40 }
41 }
42}

WooCommerce Bulk Discounts and Quantity Pricing

Implement quantity-based discounts, bulk pricing tiers, and wholesale pricing in WooCommerce with automatic calculation and display.

// Apply bulk discount based on quantity
add_action('woocommerce_before_calculate_totals', 'apply_bulk_discount');
function apply_bulk_discount($cart) {
    if (is_admin() && !defined('DOING_AJAX')) {
        return;
    }

    if (did_action('woocommerce_before_calculate_totals') >= 2) {
        return;
    }

    foreach ($cart->get_cart() as $cart_item_key => $cart_item) {
        $product = $cart_item['data'];
        $quantity = $cart_item['quantity'];
        $original_price = $product->get_regular_price();

        // Define discount tiers
        $discount_percentage = 0;

        if ($quantity >= 100) {
            $discount_percentage = 25; // 25% off for 100+
        } elseif ($quantity >= 50) {
            $discount_percentage = 20; // 20% off for 50-99
        } elseif ($quantity >= 20) {
            $discount_percentage = 15; // 15% off for 20-49
        } elseif ($quantity >= 10) {
            $discount_percentage = 10; // 10% off for 10-19
        } elseif ($quantity >= 5) {
            $discount_percentage = 5;  // 5% off for 5-9
        }

        if ($discount_percentage > 0) {
            $discount_amount = ($original_price * $discount_percentage) / 100;
            $new_price = $original_price - $discount_amount;

            $product->set_price($new_price);

            // Store discount info for display
            $cart_item['bulk_discount'] = $discount_percentage;
        }
    }
}

Display Discount Tiers on Product Page

// Show quantity discount table on product page
add_action('woocommerce_before_add_to_cart_button', 'display_bulk_discount_table');
function display_bulk_discount_table() {
    global $product;

    // Define discount tiers
    $discount_tiers = array(
        array('min' => 5, 'max' => 9, 'discount' => 5),
        array('min' => 10, 'max' => 19, 'discount' => 10),
        array('min' => 20, 'max' => 49, 'discount' => 15),
        array('min' => 50, 'max' => 99, 'discount' => 20),
        array('min' => 100, 'max' => null, 'discount' => 25),
    );

    echo '<div class="bulk-discount-table">';
    echo '<h4>' . __('Quantity Discounts Available', 'woocommerce') . '</h4>';
    echo '<table>';
    echo '<thead>';
    echo '<tr>';
    echo '<th>' . __('Quantity', 'woocommerce') . '</th>';
    echo '<th>' . __('Discount', 'woocommerce') . '</th>';
    echo '<th>' . __('Price Per Unit', 'woocommerce') . '</th>';
    echo '</tr>';
    echo '</thead>';
    echo '<tbody>';

    $regular_price = $product->get_regular_price();

    foreach ($discount_tiers as $tier) {
        $quantity_range = $tier['min'];

        if ($tier['max']) {
            $quantity_range .= ' - ' . $tier['max'];
        } else {
            $quantity_range .= '+';
        }

        $discounted_price = $regular_price - ($regular_price * $tier['discount'] / 100);

        echo '<tr>';
        echo '<td>' . $quantity_range . '</td>';
        echo '<td>' . $tier['discount'] . '%</td>';
        echo '<td>' . wc_price($discounted_price) . '</td>';
        echo '</tr>';
    }

    echo '</tbody>';
    echo '</table>';
    echo '</div>';
}

Product-Specific Bulk Discount Settings

// Add bulk discount settings to product admin
add_action('woocommerce_product_options_pricing', 'add_bulk_discount_fields');
function add_bulk_discount_fields() {
    global $post;

    echo '<div class="options_group bulk-discount-group">';

    echo '<p class="form-field"><strong>' . __('Bulk Discount Tiers', 'woocommerce') . '</strong></p>';

    // Enable bulk discounts
    woocommerce_wp_checkbox(array(
        'id'          => '_enable_bulk_discount',
        'label'       => __('Enable Bulk Discounts', 'woocommerce'),
        'description' => __('Enable quantity-based discounts for this product', 'woocommerce'),
    ));

    // Tier 1
    echo '<div class="bulk-discount-tier">';
    woocommerce_wp_text_input(array(
        'id'          => '_bulk_qty_1',
        'label'       => __('Tier 1 Quantity', 'woocommerce'),
        'type'        => 'number',
        'placeholder' => '5',
        'desc_tip'    => true,
        'description' => __('Minimum quantity for first discount tier', 'woocommerce'),
    ));

    woocommerce_wp_text_input(array(
        'id'          => '_bulk_discount_1',
        'label'       => __('Tier 1 Discount %', 'woocommerce'),
        'type'        => 'number',
        'placeholder' => '5',
        'desc_tip'    => true,
        'description' => __('Discount percentage for tier 1', 'woocommerce'),
    ));
    echo '</div>';

    // Tier 2
    echo '<div class="bulk-discount-tier">';
    woocommerce_wp_text_input(array(
        'id'          => '_bulk_qty_2',
        'label'       => __('Tier 2 Quantity', 'woocommerce'),
        'type'        => 'number',
        'placeholder' => '10',
    ));

    woocommerce_wp_text_input(array(
        'id'          => '_bulk_discount_2',
        'label'       => __('Tier 2 Discount %', 'woocommerce'),
        'type'        => 'number',
        'placeholder' => '10',
    ));
    echo '</div>';

    // Tier 3
    echo '<div class="bulk-discount-tier">';
    woocommerce_wp_text_input(array(
        'id'          => '_bulk_qty_3',
        'label'       => __('Tier 3 Quantity', 'woocommerce'),
        'type'        => 'number',
        'placeholder' => '20',
    ));

    woocommerce_wp_text_input(array(
        'id'          => '_bulk_discount_3',
        'label'       => __('Tier 3 Discount %', 'woocommerce'),
        'type'        => 'number',
        'placeholder' => '15',
    ));
    echo '</div>';

    echo '</div>';
}

// Save bulk discount fields
add_action('woocommerce_process_product_meta', 'save_bulk_discount_fields');
function save_bulk_discount_fields($post_id) {
    $enable_bulk = isset($_POST['_enable_bulk_discount']) ? 'yes' : 'no';
    update_post_meta($post_id, '_enable_bulk_discount', $enable_bulk);

    // Save tiers
    for ($i = 1; $i <= 3; $i++) {
        $qty = isset($_POST['_bulk_qty_' . $i]) ? intval($_POST['_bulk_qty_' . $i]) : '';
        $discount = isset($_POST['_bulk_discount_' . $i]) ? floatval($_POST['_bulk_discount_' . $i]) : '';

        update_post_meta($post_id, '_bulk_qty_' . $i, $qty);
        update_post_meta($post_id, '_bulk_discount_' . $i, $discount);
    }
}

Apply Product-Specific Bulk Discounts

// Apply product-specific bulk discounts
add_action('woocommerce_before_calculate_totals', 'apply_product_bulk_discount', 10, 1);
function apply_product_bulk_discount($cart) {
    if (is_admin() && !defined('DOING_AJAX')) {
        return;
    }

    if (did_action('woocommerce_before_calculate_totals') >= 2) {
        return;
    }

    foreach ($cart->get_cart() as $cart_item_key => $cart_item) {
        $product_id = $cart_item['product_id'];
        $quantity = $cart_item['quantity'];
        $product = $cart_item['data'];

        // Check if bulk discounts are enabled for this product
        if (get_post_meta($product_id, '_enable_bulk_discount', true) !== 'yes') {
            continue;
        }

        $original_price = $product->get_regular_price();
        $discount_percentage = 0;

        // Check tiers (from highest to lowest)
        for ($i = 3; $i >= 1; $i--) {
            $tier_qty = get_post_meta($product_id, '_bulk_qty_' . $i, true);
            $tier_discount = get_post_meta($product_id, '_bulk_discount_' . $i, true);

            if ($tier_qty && $tier_discount && $quantity >= $tier_qty) {
                $discount_percentage = $tier_discount;
                break;
            }
        }

        if ($discount_percentage > 0) {
            $discount_amount = ($original_price * $discount_percentage) / 100;
            $new_price = $original_price - $discount_amount;
            $product->set_price($new_price);
        }
    }
}

Display Discount in Cart

// Show applied discount in cart
add_filter('woocommerce_cart_item_price', 'display_bulk_discount_in_cart', 10, 3);
function display_bulk_discount_in_cart($price, $cart_item, $cart_item_key) {
    if (isset($cart_item['bulk_discount']) && $cart_item['bulk_discount'] > 0) {
        $product = $cart_item['data'];
        $original_price = $product->get_regular_price();

        $price = '<del>' . wc_price($original_price) . '</del> ' . wc_price($product->get_price());
        $price .= '<br><small class="bulk-discount-notice">' .
                  sprintf(__('Bulk Discount: %s%% off', 'woocommerce'), $cart_item['bulk_discount']) .
                  '</small>';
    }

    return $price;
}

Category-Wide Bulk Discounts

// Apply bulk discounts to entire categories
add_action('woocommerce_before_calculate_totals', 'apply_category_bulk_discount');
function apply_category_bulk_discount($cart) {
    if (is_admin() && !defined('DOING_AJAX')) {
        return;
    }

    if (did_action('woocommerce_before_calculate_totals') >= 2) {
        return;
    }

    // Define category discount rules
    $category_rules = array(
        'wholesale' => array(
            array('min' => 10, 'discount' => 15),
            array('min' => 50, 'discount' => 25),
            array('min' => 100, 'discount' => 35),
        ),
        'bulk-items' => array(
            array('min' => 5, 'discount' => 10),
            array('min' => 20, 'discount' => 20),
        ),
    );

    foreach ($cart->get_cart() as $cart_item_key => $cart_item) {
        $product = $cart_item['data'];
        $product_id = $cart_item['product_id'];
        $quantity = $cart_item['quantity'];

        // Get product categories
        $categories = wp_get_post_terms($product_id, 'product_cat', array('fields' => 'slugs'));

        foreach ($categories as $category_slug) {
            if (isset($category_rules[$category_slug])) {
                $rules = $category_rules[$category_slug];
                $discount_percentage = 0;

                // Find applicable discount tier
                foreach (array_reverse($rules) as $rule) {
                    if ($quantity >= $rule['min']) {
                        $discount_percentage = $rule['discount'];
                        break;
                    }
                }

                if ($discount_percentage > 0) {
                    $original_price = $product->get_regular_price();
                    $discount_amount = ($original_price * $discount_percentage) / 100;
                    $new_price = $original_price - $discount_amount;

                    $product->set_price($new_price);
                    break; // Apply only first matching category
                }
            }
        }
    }
}

Role-Based Bulk Discounts

// Apply bulk discounts based on user role (wholesale customers)
add_action('woocommerce_before_calculate_totals', 'apply_role_based_bulk_discount');
function apply_role_based_bulk_discount($cart) {
    if (is_admin() && !defined('DOING_AJAX')) {
        return;
    }

    if (did_action('woocommerce_before_calculate_totals') >= 2) {
        return;
    }

    // Check if user is wholesale customer
    if (!is_user_logged_in()) {
        return;
    }

    $user = wp_get_current_user();

    if (!in_array('wholesale_customer', $user->roles)) {
        return;
    }

    // Wholesale discount tiers
    $wholesale_tiers = array(
        array('min' => 1, 'discount' => 10),   // 10% off everything
        array('min' => 10, 'discount' => 15),  // 15% off for 10+
        array('min' => 50, 'discount' => 25),  // 25% off for 50+
        array('min' => 100, 'discount' => 35), // 35% off for 100+
    );

    foreach ($cart->get_cart() as $cart_item_key => $cart_item) {
        $product = $cart_item['data'];
        $quantity = $cart_item['quantity'];
        $original_price = $product->get_regular_price();

        $discount_percentage = 0;

        foreach (array_reverse($wholesale_tiers) as $tier) {
            if ($quantity >= $tier['min']) {
                $discount_percentage = $tier['discount'];
                break;
            }
        }

        if ($discount_percentage > 0) {
            $discount_amount = ($original_price * $discount_percentage) / 100;
            $new_price = $original_price - $discount_amount;

            $product->set_price($new_price);
        }
    }
}

Cart Total Based Discount

// Apply discount based on cart total
add_action('woocommerce_cart_calculate_fees', 'add_cart_total_discount');
function add_cart_total_discount($cart) {
    if (is_admin() && !defined('DOING_AJAX')) {
        return;
    }

    $cart_total = $cart->get_subtotal();

    $discount_amount = 0;

    if ($cart_total >= 1000) {
        $discount_amount = $cart_total * 0.15; // 15% off
    } elseif ($cart_total >= 500) {
        $discount_amount = $cart_total * 0.10; // 10% off
    } elseif ($cart_total >= 200) {
        $discount_amount = $cart_total * 0.05; // 5% off
    }

    if ($discount_amount > 0) {
        $cart->add_fee(__('Bulk Order Discount', 'woocommerce'), -$discount_amount);
    }
}

Styling

/* Bulk discount table */
.bulk-discount-table {
    margin: 20px 0;
    padding: 20px;
    background: #f9f9f9;
    border-radius: 5px;
    border: 2px solid #0073aa;
}

.bulk-discount-table h4 {
    margin-top: 0;
    color: #0073aa;
    font-size: 18px;
}

.bulk-discount-table table {
    width: 100%;
    border-collapse: collapse;
}

.bulk-discount-table th,
.bulk-discount-table td {
    padding: 10px;
    text-align: left;
    border-bottom: 1px solid #e0e0e0;
}

.bulk-discount-table th {
    background: #0073aa;
    color: white;
    font-weight: 600;
}

.bulk-discount-table tr:hover {
    background: #f0f0f0;
}

/* Discount notice in cart */
.bulk-discount-notice {
    color: #0073aa;
    font-weight: 600;
}

/* Admin bulk discount fields */
.bulk-discount-tier {
    display: flex;
    gap: 15px;
    margin-bottom: 10px;
}

.bulk-discount-tier .form-field {
    flex: 1;
}

Features

  • Quantity-Based Discounts: Automatic discounts based on quantity purchased
  • Tiered Pricing: Multiple discount tiers (5%, 10%, 15%, 20%, 25%)
  • Visual Pricing Table: Shows discount tiers on product pages
  • Product-Specific Rules: Set custom discount tiers per product
  • Category-Wide Discounts: Apply discounts to entire product categories
  • Role-Based Pricing: Wholesale pricing for specific user roles
  • Cart Total Discounts: Discounts based on total cart value
  • Dynamic Calculation: Real-time price updates in cart
  • Discount Display: Shows applied discounts in cart and checkout
  • Admin Interface: Easy-to-use fields for setting discount rules
  • Fully Responsive: Mobile-friendly pricing tables
  • Flexible Configuration: Supports multiple discount strategies

Dependencies

  • WooCommerce

Related Snippets