Search code examples
phpwordpresswoocommerceshipping-method

WooCommerce: Check if shipping of an order is free


I display an information about the needed amount for free shipping.

Like:

Add 10$ more to get free shipping

That all works fine and is based on a fixed amount for the free shipping. My problem is now, that it will also show even if a coupon sets free shipping. Is there any way to check if the order already uses any free shipping method?

I found something here: https://stackoverflow.com/a/32932933/1788961

global $woocommerce;
$shipping_methods = $woocommerce->shipping->load_shipping_methods();
if($shipping_methods['free_shipping']->enabled == "yes")

But it doesn't help.

I also found an interesting snippet here: https://www.businessbloomer.com/woocommerce-hide-shipping-options-free-shipping-available/

$all_free_rates = array();
     
foreach ( $rates as $rate_id => $rate ) {
      if ( 'free_shipping' === $rate->method_id ) {
         $all_free_rates[ $rate_id ] = $rate;
         break;
      }
}
     
if ( empty( $all_free_rates )) {
        return $rates;
} else {
        return $all_free_rates;
} 
 
}

Solution

  • I guess I solved it:

    // Loop though shipping packages
    foreach ( WC()->shipping->get_packages() as $key => $package ) {
        // Loop through Shipping rates
        foreach($package['rates'] as $rate_id => $rate ){
            $check_method_shipping = $rate->method_id;
        }
    }
    

    And after that you can use it like this:

    if ($check_method_shipping != 'free_shipping') :