WooCommerce Coupon Issue: Deducting Shipping Costs when Coupon Exceeds Cart Total

In a recent Business Bloomer Club discussion, a WooCommerce user highlighted an issue where coupon codes greater than the cart total fail to cover shipping costs, leaving customers with an unexpected shipping fee.

This behavior can be confusing for customers and store owners alike, especially when they expect the coupon to cover the entire purchase, including shipping.

Here, we explore a workaround for this issue using custom code to deduct shipping fees when a coupon exceeds the total.

The Issue with WooCommerce Coupon Logic

By default, WooCommerce applies coupons only to product totals and does not account for additional fees like shipping unless specifically configured to do so. In cases where the coupon value is more than the cart total, WooCommerce still expects shipping to be paid separately, resulting in an incomplete discount.

Solution: Custom Code to Include Shipping Costs

The following PHP snippet helps adjust WooCommerce’s coupon logic, ensuring that shipping costs are considered when the coupon covers the full cart total.

Code Snippet to Adjust Shipping Costs with Coupons

 add_action('woocommerce_cart_calculate_fees', 'apply_coupon_to_shipping', 10, 1);
 function apply_coupon_to_shipping($cart) {
     if (is_admin() && !defined('DOING_AJAX')) {
         return;
     }
 
     // Get the applied coupon(s)
     $coupons = $cart->get_applied_coupons();
     
     // Get cart subtotal and shipping cost including tax
     $cart_subtotal = WC()->cart->get_subtotal() + WC()->cart->get_subtotal_tax();
     $shipping_cost = $cart->get_shipping_total() + $cart->get_shipping_tax(); // Shipping total including tax
 
     $cart_subtotal = number_format($cart_subtotal, 2, '.', '');
  
     // Check if there are applied coupons
     if (!empty($coupons)) {
         // Loop through the applied coupons
         foreach ($coupons as $coupon_code) {
             $coupon = new WC_Coupon($coupon_code);
             $original_discount = $coupon->get_amount(); // Get the original discount value
             $discount_type = $coupon->get_discount_type(); // Get discount type (fixed/cart percentage, etc.)
 
             // Format the original discount to 2 decimal places
             $original_discount_formatted = number_format($original_discount, 2, '.', '');
 
             // Get the total discount amount applied by this coupon
             $discount_total = WC()->cart->get_cart_discount_total(); // Total discount amount applied by coupons
 
             // Check if the discount is greater than the cart subtotal
             if ($original_discount_formatted > $cart_subtotal) {
                 $remaining_discount = $original_discount_formatted - $cart_subtotal;
                 $remaining_discount_incl_shipping = $remaining_discount - $shipping_cost;
 
                 if ($remaining_discount_incl_shipping > 0) {
                     $cart->add_fee('Resterend bedrag Coupon lalal', -$shipping_cost);
                 } elseif ($remaining_discount_incl_shipping <= 0) {
                     $need_to_pay = (($cart_subtotal + $shipping_cost) - $original_discount_formatted);
                     
                     $shipping_tax_amount = WC()->cart->get_shipping_tax();
                     $shipping_tax_percentage = (($shipping_tax_amount / ($shipping_cost - $shipping_tax_amount)) * 100);
                     $shipping_tax_percentage = number_format($shipping_tax_percentage, 0, '.', '');
                     $shipping_tax_cal = ((100 + $shipping_tax_percentage) / 100);

                     $shipping_discount_fee = $shipping_cost - $need_to_pay;
                     $shipping_discount_fee = $shipping_discount_fee / $shipping_tax_cal;
              
                     if ($shipping_discount_fee > 0) {
                        $cart->add_fee('Resterend bedrag Coupon', -$shipping_discount_fee, true, 'BTW vrij');
                    }
                 }
             }
         }
     }
 }

How the Code Works

  1. Check for Applied Coupons: The code first verifies if there are any coupons in the cart.
  2. Calculate Total Discount: It sums up the discount amount from each coupon code applied to the cart.
  3. Apply Discount to Shipping: If the discount amount is higher than the cart total, the remaining amount is applied to the shipping cost, effectively covering it.
  4. Add Shipping Discount: Finally, the code adds a “Shipping Discount” line item to the cart, reducing the shipping cost based on the remaining coupon value.

Conclusion

This custom code provides a practical solution for WooCommerce users seeking a way to apply large coupon amounts to cover both cart totals and shipping costs. It ensures that customers don’t encounter unexpected fees at checkout when the coupon should cover the entire cost.

Related content

Rodolfo Melogli

Business Bloomer Founder

Author, WooCommerce expert and WordCamp speaker, Rodolfo has worked as an independent WooCommerce freelancer since 2011. His goal is to help entrepreneurs and developers overcome their WooCommerce nightmares. Rodolfo loves travelling, chasing tennis & soccer balls and, of course, wood fired oven pizza. Follow @rmelogli

Reply

Your email address will not be published. Required fields are marked *