0
votes

After the upgrade to Woocommerce 2.6 , I decided to use the new Shipping Zones functionality I have broken a function of mine calculating the difference from cart value in order to get free shipping.

I used to get the free shipping value like :

$shipping_methods = $woocommerce->shipping->load_shipping_methods();
$free_shipping = $shipping_methods['free_shipping'];
$cart_total = $woocommerce->cart->cart_contents_total;
$minimum_amount_required = floatval($free_shipping->min_amount);
$diff = $minimum_amount_required - $cart_total;

But now as things have changes , i want to use shipping zones. So how can I get the min amount required for free shipping? I've already set the values from the options and I have tried

$shipping = new WC_Shipping_Free_Shipping();

Which returns

object(WC_Shipping_Free_Shipping)#9235 (22) {
      ["min_amount"]=>
      string(1) "0"
      ["requires"]=>
      string(0) ""
      ["supports"]=>
      array(3) {
        [0]=>
        string(14) "shipping-zones"
        [1]=>
        string(17) "instance-settings"
        [2]=>
        string(23) "instance-settings-modal"
      }......

Although I have set an amount at settings and it returns 0.

2

2 Answers

1
votes

After searching for a while (I had the same error) I realised that you need to give your package while your are trying to get your available shipping methods.

Package parameter in load_shipping_method is optional, but WC is not able to get your min_amount quantity so it set this value to "0". This is due to the fact that you can set several min_amount (one by each zone).

You can get your packages like this:

$packages = WC()->cart->get_shipping_packages();

For multiple packaging support, you must iterate for every package and you can get your methods with:

//In $available_methods you must have initialised your free_shipping min_amount value.
$available_methods = WC()->shipping()->load_shipping_methods($packages[i]);

I hope this would help you!

0
votes

you can use :

$free_shipping_settings = get_option( 'advanced_free_shipping_settings' );
$min_amount = $free_shipping_settings['min_amount'];

I have used to it but it return NULL result but you can try it. Maybe work with you.