
In a recent Business Bloomer Club discussion, a WooCommerce user inquired about calculating shipping fees based on the subtotal before applying coupon discounts.
As you may know, you can use a shortcode to make the shipping cost dynamic e.g. [fee percent="15"]
, but in this case the developer wanted to maintain a consistent shipping fee of 15% of the subtotal, unaffected by any coupon discounts.
Typically, WooCommerce applies the shipping rate after discounts, which can cause shipping fees to decrease if a discount is applied to the subtotal.
Here’s a step-by-step guide to implementing this customization using a custom code snippet.
Why Adjust the Shipping Fee Calculation?
For businesses that calculate shipping as a percentage of the subtotal, it may be important to maintain that percentage regardless of any discounts applied. By making this adjustment, the store owner can ensure shipping fees remain based on the original subtotal, providing consistency and helping to maintain revenue from shipping charges.
Custom Code Solution
The solution requires a custom filter to adjust the shipping fee calculation so that it calculates based on the subtotal before any discounts. Place this code in your theme’s functions.php
file or a custom plugin:
add_filter( 'woocommerce_evaluate_shipping_cost_args', function( $args, $sum, $shipping ) {
// Check if there is a discount applied
if ( WC()->cart && WC()->cart->get_discount_total() ) {
// Set shipping cost calculation based on the original subtotal
$args['cost'] = WC()->cart->get_subtotal();
}
return $args;
}, 9999, 3 );
Explanation of the Code
- Filter Hook:
woocommerce_evaluate_shipping_cost_args
allows customization of shipping cost calculations. - Discount Check:
WC()->cart->get_discount_total()
checks if a discount has been applied. If a discount exists, the code adjusts the shipping calculation to use the original subtotal rather than the discounted total. - Cost Calculation: By setting
$args['cost']
toWC()->cart->get_subtotal()
, the code ensures the shipping rate of 15% is applied based on the pre-discount subtotal.
Testing the Customization
- Add the Code: Place the code snippet in the
functions.php
file or a custom plugin. - Go to Checkout: Add products to the cart and apply a coupon.
- Verify Shipping Calculation: Ensure the shipping rate remains at 15% of the original subtotal, not the discounted total.
Considerations for this Solution
- Customer Communication: It may be helpful to inform customers that shipping fees are calculated before applying any discounts.
- Discounted Subtotal Visibility: Customers will still see the final discounted subtotal, but the shipping rate will remain consistent based on the original amount.
Conclusion
This custom code provides WooCommerce store owners with greater control over shipping fee calculations, ensuring consistency in percentage-based shipping rates. By calculating shipping costs from the original subtotal, this customization can help avoid reduced shipping fees due to coupon discounts, thereby maintaining a predictable shipping revenue.
For additional WooCommerce insights and solutions, consider joining the Business Bloomer Club for more expert discussions and resources.