Ever needed to apply WooCommerce coupons to regular prices instead of sale prices? Maybe you want to run a special promotion that should override existing sales, or you need to ensure your percentage-based coupons are calculated from the original product price. Here’s a simple solution that lets you do just that.
In my case, I needed this for a specific coupon code, but I’ll show you how to make it work for both specific coupons and all coupons in your store.
The Solution
We’ll use the woocommerce_before_calculate_totals hook to modify product prices before WooCommerce calculates the cart total. The high priority number (9999) ensures our code runs after other price modifications.
Option 1: For a Specific Coupon
Here’s the code for handling a specific coupon. Just replace ‘your-coupon-code’ with your actual coupon code.
add_action('woocommerce_before_calculate_totals', 'fvf_adjust_cart_prices_for_coupon', 9999, 1);
function fvf_adjust_cart_prices_for_coupon($cart_object) {
if (!function_exists('WC') || !isset(WC()->cart)) {
return;
}
if (is_admin() && !defined('DOING_AJAX')) {
return;
}
// Replace with your coupon code
if (!WC()->cart->has_discount('your-coupon-code')) {
return;
}
foreach ($cart_object->get_cart() as $cart_item) {
$product = $cart_item['data'];
if ($product->is_on_sale()) {
$product->set_price($product->get_regular_price());
}
}
}
Option 2: For All Coupons
If you want this behavior for any coupon applied to the cart, here’s the modified version:
add_action('woocommerce_before_calculate_totals', 'fvf_adjust_cart_prices_all_coupons', 9999, 1);
function fvf_adjust_cart_prices_all_coupons($cart_object) {
if (!function_exists('WC') || !isset(WC()->cart)) {
return;
}
if (is_admin() && !defined('DOING_AJAX')) {
return;
}
// Check if any coupon is applied
if (empty(WC()->cart->get_applied_coupons())) {
return;
}
foreach ($cart_object->get_cart() as $cart_item) {
$product = $cart_item['data'];
if ($product->is_on_sale()) {
$product->set_price($product->get_regular_price());
}
}
}
How to Use
- Choose which version you want to use (specific coupon or all coupons)
- Copy the code and paste it into your theme’s functions.php file or a custom plugin
- If using Option 1, replace ‘your-coupon-code’ with your actual coupon code
That’s it! The code will now automatically change sale prices to regular prices when coupons are applied
Remember to test thoroughly in your development environment before deploying to production, especially if you have other plugins that modify product prices.