How to Apply a Discount Based on Cart Item Count (Excluding Sale Items) in WooCommerce
As an e-commerce store owner, one of your top priorities is to optimize the conversion rates of your online shop. A crucial aspect of this is ensuring that your customers are provided with an exceptional shopping experience, which includes offering relevant discounts and promotions.
In this blog post, we'll explore a custom WooCommerce function that applies a 10% discount to your cart when there are 5 or more items, excluding any products that are currently on sale. This technique can be a powerful tool for boosting your store's conversion rates and increasing customer satisfaction.
Understanding the Problem
Imagine you're running an e-commerce store powered by WooCommerce. You want to offer your customers a discount when they add a significant number of items to their cart, as this can be an indicator of a high-intent buyer. However, you don't want this discount to apply to products that are already on sale, as that could potentially erode your profit margins.
To address this challenge, we'll create a custom hooked function that will calculate the discount and apply it to the cart, but only when the following conditions are met:
- There are 5 or more items in the cart.
- None of the items in the cart are currently on sale.
By implementing this functionality, you can effectively reward your customers for adding multiple items to their cart, while also maintaining control over your pricing and profit margins.
Implementing the Custom Discount Function
To implement the custom discount function, you'll need to add the following code to your theme's functions.php
file or a custom plugin:
add_action('woocommerce_cart_calculate_fees' , 'custom_discount', 10, 1);
function custom_discount( $cart ){
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
// Only when there is 5 or more items in cart
if( $cart->get_cart_contents_count() >= 5):
// Initialising variable
$is_on_sale = false;
// Iterating through each item in cart
foreach( $cart->get_cart() as $cart_item ){
// Getting an instance of the product object
$product = $cart_item['data'];
// If a cart item is on sale, $is_on_sale is true and we stop the loop
if($product->is_on_sale()){
$is_on_sale = true;
break;
}
}
## Discount calculation ##
$discount = $cart->subtotal * -0.1;
## Applied discount (no products on sale) ##
if(!$is_on_sale )
$cart->add_fee( '10% discount', $discount);
endif;
}
Let's break down how this code works:
- The
add_action()
function hooks the custom_discount()
function to the woocommerce_cart_calculate_fees
action, which is triggered when the cart is calculated.
- The
custom_discount()
function first checks if the current request is coming from the admin area or an AJAX request, and if so, it returns without applying any discount.
- Next, the function checks if the cart contains 5 or more items. If this condition is met, it proceeds to the next step.
- Inside the if-statement, the function initializes a variable called
$is_on_sale
to false
. This variable will be used to track whether any of the items in the cart are currently on sale.
- The function then iterates through each item in the cart, getting an instance of the product object for each item. If any of the products are on sale, the
$is_on_sale
variable is set to true
and the loop is broken.
- After the loop, the function calculates the discount amount, which is 10% of the cart subtotal.
- Finally, if no products in the cart are on sale (i.e.,
$is_on_sale
is false
), the function adds the discount as a fee to the cart using the $cart->add_fee()
method.
By implementing this custom function, you can ensure that your customers receive a 10% discount on their cart when they add 5 or more items, as long as none of those items are currently on sale.
Verifying the Functionality
To test the functionality of the custom discount function, you can follow these steps:
- Add the code snippet to your theme's
functions.php
file or a custom plugin.
- Add 5 or more items to your cart, making sure that none of the items are on sale.
- Proceed to the checkout page and verify that the 10% discount is applied to the cart subtotal.
- Repeat the process, but this time add at least one item that is on sale. Verify that the discount is not applied in this scenario.
By thoroughly testing the functionality, you can ensure that the custom discount is being applied correctly and meeting your desired requirements.
Optimizing Conversion Rates with Targeted Discounts
Implementing a custom discount based on cart item count (excluding sale items) is a powerful way to optimize the conversion rates of your WooCommerce store. By rewarding customers who add multiple items to their cart, you can encourage larger order sizes and increase the overall value of each transaction.
Additionally, by excluding sale items from the discount, you can maintain control over your pricing and profit margins, ensuring that your promotional efforts don't negatively impact your bottom line.
When used as part of a comprehensive conversion rate optimization (CRO) strategy, this custom discount function can be a valuable tool in your e-commerce arsenal. By analyzing customer behavior, identifying pain points, and testing different discount strategies, you can continually refine and improve the shopping experience for your customers, ultimately driving more sales and revenue for your business.
If you're looking to take your WooCommerce store's conversion rates to the next level, consider implementing this custom discount function and exploring other data-driven CRO techniques. With the right approach, you can unlock the full potential of your online store and provide your customers with an exceptional shopping experience.
Flowpoint.ai can help you identify all the technical errors that are impacting conversion rates on your website and directly generate recommendations to fix them, including issues with your custom WooCommerce discount function.
Get a Free AI Website Audit
Automatically identify UX and content issues affecting your conversion rates with Flowpoint's comprehensive AI-driven website audit.