![]()
Are you tired of paying monthly or annual fees for a WooCommerce dynamic pricing plugin just to run a simple, yet highly effective, “Buy More, Save More” promotion? You’ve come to the right place.
The goal of offering a bulk discount – like “Buy 5 items and get 10% off the total” or “Purchase 10 units for 20% off each product” – is to dramatically increase your Average Order Value (AOV). For most store owners, this is a mission-critical feature. Luckily, you don’t need a pricey premium plugin to implement a functional, powerful, and scalable solution.
This comprehensive guide will show you exactly how to apply a WooCommerce bulk discount programmatically using a single, robust PHP code snippet. We’ll bypass the slow, bulky plugins and integrate a custom solution directly into your WordPress site.
The entire process is clean, lightweight, and focuses on extending WooCommerce’s core functionality, ensuring future compatibility and peak performance. By the end of this tutorial, you’ll have a professional, zero-cost bulk discount system running on your store.
Prerequisite: How to Add Your Custom Code
Before we dive into the PHP, it’s crucial to understand where this code needs to go. NEVER add custom PHP directly to your theme’s functions.php file, as an theme update will wipe out your work.
The best and safest way to add this WooCommerce bulk discount programmatically snippet is using a dedicated code snippets plugin (like Code Snippets) or by placing it in your Child Theme’s functions.php file.
Pro Tip: Always test code on a staging or development site first. Back up your site before making any changes to your production environment!
The Core WooCommerce Bulk Discount Programmatically Snippet
WooCommerce provides a powerful hook, woocommerce_cart_calculate_fees, which allows us to add or subtract costs from the cart total before final checkout. We will use this to inject our bulk discount.
The following snippet is structured to apply a tiered percentage discount based on the total quantity of all items in the cart. This is a common and effective WooCommerce bulk discount programmatically method for increasing overall order size.
Copy and paste the entire block of code below.
/**
* pnet_apply_bulk_discount_programmatically
*
* Apply a tiered percentage bulk discount based on the total quantity of items in the cart.
* Hooked into woocommerce_cart_calculate_fees with a low priority to ensure it runs last.
*
* @param \WC_Cart $cart The WooCommerce Cart object.
*/
function pnet_apply_bulk_discount_programmatically( $cart ) {
// Check if in admin and not doing AJAX, or if discount is already applied.
if ( is_admin() && ! defined( 'DOING_AJAX' ) ) {
return;
}
// --- Configuration: Define Your Discount Tiers Here ---
$discount_tiers = array(
// Quantity Threshold => Discount Percentage (as a decimal)
5 => 0.10, // 5 items or more gets 10% off
10 => 0.15, // 10 items or more gets 15% off
20 => 0.20, // 20 items or more gets 20% off
);
// --------------------------------------------------------
// 1. Calculate the total quantity of ALL items in the cart
$total_quantity = $cart->get_cart_contents_count();
// 2. Determine the highest applicable discount percentage
$applicable_discount_rate = 0.0;
// Sort tiers by quantity in descending order to find the highest applicable tier first
krsort( $discount_tiers );
foreach ( $discount_tiers as $quantity_threshold => $discount_rate ) {
if ( $total_quantity >= $quantity_threshold ) {
$applicable_discount_rate = $discount_rate;
// Once the highest applicable tier is found, stop the loop
break;
}
}
// 3. Apply the discount if a rate is found (i.e., greater than 0)
if ( $applicable_discount_rate > 0.0 ) {
// Calculate the subtotal (excl. tax) that the discount will be applied to
$cart_subtotal = $cart->get_subtotal();
// The total discount amount is the rate multiplied by the subtotal
$discount_amount = $cart_subtotal * $applicable_discount_rate;
// Format the discount text for the cart
$discount_percentage_display = $applicable_discount_rate * 100;
$discount_name = sprintf( __( 'Bulk Discount (%s%% Off)', 'your-text-domain' ), $discount_percentage_display );
// Add the discount as a negative fee to the cart.
// We use -1 * $discount_amount because add_fee expects a positive number for a fee.
$cart->add_fee( $discount_name, -1 * $discount_amount );
}
}
add_action( 'woocommerce_cart_calculate_fees', 'pnet_apply_bulk_discount_programmatically', 10, 1 );
Customizing Your Bulk Discount Tiers
The real power of this snippet lies in the configuration array near the top of the function: $discount_tiers.
// --- Configuration: Define Your Discount Tiers Here ---
$discount_tiers = array(
// Quantity Threshold => Discount Percentage (as a decimal)
5 => 0.10, // 5 items or more gets 10% off
10 => 0.15, // 10 items or more gets 15% off
20 => 0.20, // 20 items or more gets 20% off
);
// --------------------------------------------------------
You can change these values to perfectly match your store’s strategy for a WooCommerce bulk discount programmatically.
- Quantity Threshold: This is the minimum total number of items in the cart needed to qualify for that specific discount tier.
- Discount Percentage (as a decimal): This must be entered as a decimal. For example, 10% is written as
0.10, 15% as0.15, and 20% as0.20.
How the Tiers Work:
The code automatically checks for the highest applicable discount. With the default configuration:
- A cart with 4 items gets 0% off.
- A cart with 5 items gets 10% off.
- A cart with 15 items gets 15% off (because 15 is > 10).
- A cart with 25 items gets 20% off (because 25 is > 20).
This ensures that customers are always rewarded with the best discount for reaching a higher quantity milestone—a key motivational factor for any successful bulk sales program.
Expanding the Snippet: Discount Based on Specific Product Category
What if you only want the bulk discount to apply to items in a specific category, like “T-Shirts”?
We can modify the quantity calculation part of the code to only count the items belonging to one or more defined categories. This is an advanced application of the WooCommerce bulk discount programmatically method.
Replace the lines that calculate $total_quantity and $cart_subtotal in the original snippet with the following code.
Note: The rest of the pnet_apply_bulk_discount_programmatically function remains the same.
// --- Customization: Target Specific Categories ---
// Define the slug(s) of the categories you want to target
$target_categories = array( 't-shirts', 'hoodies' );
// 1. Calculate the total quantity and subtotal of ONLY TARGETED items in the cart
$total_quantity = 0;
$cart_subtotal_for_discount = 0;
foreach ( $cart->get_cart() as $cart_item_key => $cart_item ) {
$product = $cart_item['data'];
// Check if the product belongs to any of the target categories
if ( has_term( $target_categories, 'product_cat', $product->get_id() ) ) {
$total_quantity += $cart_item['quantity'];
// Calculate the subtotal for only the items in the target categories
$cart_subtotal_for_discount += $cart_item['line_total'];
}
}
// If no target products are found, exit the function early
if ( $total_quantity < min( array_keys( $discount_tiers ) ) ) {
return;
}
// ... Rest of the function continues from step 2 (Determine the highest applicable discount percentage)
// ... $cart_subtotal will now be $cart_subtotal_for_discount
What changed?
- We defined an array
$target_categoriesusing the category slugs. - We looped through all items in the cart and used the
has_term()function to check if the product is in our target category. - Only the quantity and subtotal of the qualifying items are accumulated, ensuring that non-category products don’t count towards the threshold or receive the discount.
Testing and Next Steps
Once you’ve added the code, clear any caching on your site and visit your store’s front end.
- Test the Base Case: Add fewer items than the lowest threshold (e.g., 4 items). The discount should not appear.
- Test the First Tier: Add the minimum quantity for the first tier (e.g., 5 items). A new line item for the Bulk Discount should appear in the cart/checkout totals, showing the correct percentage off.
- Test a Higher Tier: Increase the quantity to hit a higher threshold (e.g., 12 items). The discount line should automatically update to the higher percentage (e.g., 15% off).
Congratulations! You’ve successfully leveraged the power of WooCommerce’s developer hooks to implement a sophisticated WooCommerce bulk discount programmatically solution. This is not only a free alternative to paid plugins but is also a faster, more secure, and fully customized way to run your promotions and boost your sales.
You can learn more about how to set up bulk discounts using a plugin, which can be useful for those who aren’t comfortable with code, by watching this video. Bulk Discounting by Manufacturers in WooCommerce on YouTube.