You have a (brick and mortar) shop for quite some time. Your neighbors recognize you, great you every day and buy from your shop. You know, as a salesman, that a satisfied customer is a repeat customer. What do you do? You offer special prices for your best customers, and discounts for big orders. This earns you happy, repeat customers, who will recommend you to more of their friends and, ultimately, increase your sales.
The exact same logic can apply to your e-commerce shop. You can entice your customers to buy more, by creating discounts for bulk orders and reward your loyal customers.
In this article, we will examine various scenarios in which we reward a customer with a discount and how to apply them in our WordPress/WooCommerce e-shop.
Table of contents |
---|
Getting ready |
Price-based bulk discount |
Exlude certain product categories from discount |
Number of items based bulk discount |
Wrapping up |
Getting ready
Before doing any code modifications, it’s always a good idea to take a full backup of our site and make sure that we have FTP access or access to cPanel or Plesk or other control panel with access to a file manager.
If you wonder what is the best way to add code snippets to your site, have a look at our previous article about safely adding PHP code.
We assume that you already have WordPress with WooCommerce installed, and your store is displaying at least a few products.
Bulk discount based on total price
Let’s find out how we can apply a bulk discount when the total exceeds a certain amount. In the following example, if the order total is $50, we will offer a discount of 5%. If the order total is over $100, we will offer a discount of 10%.
The following snippet will check the order total and calculate the discount percentage, if applicable:
function tl_apply_bulk_discount() {
global $woocommerce;
$excluded_amount = $discount_percent = 0;
$working_total = $woocommerce->cart->cart_contents_total;
// Only apply manual discount if no coupons are applied
if (!$woocommerce->cart->applied_coupons) {
// Logic to determine WHICH discount to apply based on subtotal
if ($working_total >= 50 && $working_total < 100) { $discount_percent = 5; } elseif ($working_total >= 100) {
$discount_percent = 10;
}
else {
$discount_percent = 0;
}
// Make sure cart total is eligible for discount
if ($discount_percent > 0) {
$discount_amount = ( ( ($discount_percent/100) * $working_total ) * -1 );
$woocommerce->cart->add_fee('Bulk Discount', $discount_amount);
}
}
}
add_action('woocommerce_cart_calculate_fees', 'tl_apply_bulk_discount');
The snippet above prevents the bulk discount to be applied when other coupons have been used. To change the minimum amounts and the discount percentage applied, edit the numbers in lines 10-15:
if ($working_total >= 50 && $working_total < 100) { $discount_percent = 5; } elseif ($working_total >= 100) {
$discount_percent = 10;
}
If you only want to apply one tier for discount, simply remove completely the elseif
statement.
Here is the result of this snippet:
My cart total here is $51, so I get a discount of 5% (=2.55, rounded up to 3):
While here, after I added a few products and got the total over $100, the discount is increased to 10%:
Exlude certain product categories from discount
The above code snippet will apply a general discount to every product of your shop, as long as the order total exceeds a predetermined amount. This, however, might not be desirable. We may be selling some products that are already in a discounted price as a promotion, or we might not want to offer discounts for some luxury items.
Let’s fine-tune the previous snippet, in order to be able to exclude certain product categories from being eligible for the discounted price.
In the following example, my order reaches a total of $81 and I get the 5% discount.
However, I want all products in the category “Hoodies” to not be eligible for discount. The category ID of “Hoodies”, in my installation, is 23
, as I found visiting Products > Categories on my dashboard and clicking on “Hoodies”. So, let’s expand the snippet of the previous section, to exclude all products in the category with ID 23
from being eligible for discount:
function tl_apply_bulk_discount() {
global $woocommerce;
$excluded_amount = $discount_percent = 0;
$working_total = $woocommerce->cart->cart_contents_total;
$excluded_categories = array(
23, // Hoodies
);
// Only apply manual discount if no coupons are applied
if (!$woocommerce->cart->applied_coupons) {
// Find any items in cart that belong to the restricted categories
foreach ($woocommerce->cart->cart_contents as $item) {
$product_categories = get_the_terms($item['product_id'], 'product_cat');
if (empty($product_categories) || is_wp_error($product_categories) || !$product_categories) {
if (is_wp_error($product_categories)) {
wp_die($product_categories->get_error_message());
}
else {
$product_categories = new WP_Error('no_product_categories', "The product \"".$item->post_title."\" doesn't have any categories attached, thus no discounts can be calculated.", "Fatal Error");
wp_die($product_categories);
}
}
foreach ($excluded_categories as $excluded_category) {
foreach ($product_categories as $category) {
if ($excluded_category == $category->term_id) {
$excluded_amount += $item['line_subtotal']; // Increase our discounted amount
$working_total -= $item['line_subtotal']; // Decrease our discounted amount
}
}
}
}
// Logic to determine WHICH discount to apply based on subtotal
if ($working_total >= 50 && $working_total < 100) { $discount_percent = 5; } elseif ($working_total >= 100) {
$discount_percent = 10;
}
else {
$discount_percent = 0;
}
// Make sure cart total is eligible for discount
if ($discount_percent > 0) {
$discount_amount = ( ( ($discount_percent/100) * $working_total ) * -1 );
$woocommerce->cart->add_fee('Bulk Discount', $discount_amount);
}
}
}
add_action('woocommerce_cart_calculate_fees', 'tl_apply_bulk_discount');
I added the category ID in the code above in line 6. Now, the same checkout page looks quite different:
As the hoodie was the product that raised my price to over $50, the discount has disappeared. Let’s see the same cart if I add a few more beanies, that are still eligible for discount:
Here I do get my bulk discount of 5%, as the total price of the beanies exceeds $50.
If you want to exclude more categories, you can simply add a new line after line 6:
$excluded_categories = array(
23, // Hoodies
26, // Clothing
8, // Hair products
);
Number of items based bulk discount
Here is another scenario: You are selling wholesales, your products don’t vary too much on prices (or you only sell one product) and it makes more sense to both you and your customer to offer a discount based on number of products ordered.
For this, we will use the WooCommerce Bulk Discount, that does exactly this.
Setting up WooCommerce Bulk Discount
Upon installing and activating the plugin, a new tab called Bulk Discount appears on WooCommerce > Settings, that allows you to create your own discount policy:
The most important decision here is the “Discount type”:
- Percentage Discount: Much like the previous use-case we discussed, on an order of more of N items, a percentage discount will be applied on the total price of the product.
- Flat discount: A fixed amount of money is reduced from the total when you buy more than N items. For example, if I buy more than 20 hoodies, I will get a $10 discount, no matter if I buy 21 or 40 hoodies.
- Fixed Discount: An amount of money is reduced from each item, when you buy more than N items. For example, if the flat discount is 2$ for more than 10 items, on a product that costs $50. So if I buy over 10 items, I will get each item at the price of $8 per item.
In each of the above choices, you can still determine tiers, for example different discount for over 10 and over 100 items.
Examples using WooCommerce Bulk Discount
Let’s see how this works in action. Here I want to apply a flat discount of $15 if someone buys more than 20 of my product Polo”, that costs $20 per item. First, we need to select “Flat discount” from the dropdown at WooCommerce > Settings > Bulk Discount. Then at the product edit screen, we can see a new tab named “Bulk Discount”. The new panel allows you to add a special offer text if you so wish, and then you can add a rule for the discount, by pressing on the button: “Add discount line”.
And here we can see that the flat discount applied on my order of 22 x “Polo”:
Let’s test this for a fixed discount of $5 for each item if the order is more than 20 items. We need to select “Fixed discount” from the dropdown at WooCommerce > Settings > Bulk Discount and then enter the appropriate line at the product edit screen:
At the checkout screen after buying, again, 22 x “Polo”, we can see that now the discount is 5 dollars for each item, that is, I actually bought the 22 polos at $15 each, instead of $20 each:
Wrapping up
In this article we explored the various ways to reward your customers for bulk orders, by offering discounted prices. We saw how to:
- Offer a discount when the total price exceeds a specific amount.
- Limit which product categories can be eligible for a total price discount.
- Offer a flat, fixed, or by percentage discount to customers that buy more than a certain amount of items per product.
We hope this article was useful. Please leave a comment if you have used any of these methods to offer discounted prices to your customers. Let us know if you have any questions.