Sunday, March 17, 2013

WooCommerce: Trigger Free Shipping min_amount Logic on discounted Final Order Amount

WooCommerce, by default, applies the "Free Shipping" logic on the Order total (not discounted) before the application of a discount coupon.

Here is a Use-Case: Free Shipping is enabled if the cart total is greater than $800, that is, when the cart total is, for example, $1000, free shipping should be available.

However, if a discount coupon of 50% discount is applied on the $1000 total, Free shipping must not be available - because now the new total is $500. WooCommerce does not do this and Free Shipping logic triggers on the Un-discounted Cart Total and not on the new Total after the discount coupon is applied. Therefore, even when the final order amount is $500 (less than $800), Free Shipping is enabled.

Here is how this behaviour can be changed:

1) Open the file class-wc-free-shipping.php present in the plugins/woocommerce/classes/shipping folder.

2) Within the function is_available($package), replace this piece of code:

if ( $woocommerce->cart->prices_include_tax )
    $total = $woocommerce->cart->tax_total + $woocommerce->cart->cart_contents_total;
else
    $total = $woocommerce->cart->cart_contents_total;

with this,


if ( $woocommerce->cart->prices_include_tax )
    $total = $woocommerce->cart->tax_total + $woocommerce->cart->cart_contents_total - $woocommerce->cart->discount_total;
else
    $total = $woocommerce->cart->cart_contents_total - $woocommerce->cart->discount_total;