How can i fetch minimum order amount required to get free shipping ( woocommerce_free_shipping_min_amount
which is set in admin panel woocommerce -> settings -> shipping -> free shipping -> minimum order amount ) in woocommerce?
I want to display this price in front-end page
This value is stored in an option
under the key woocommerce_free_shipping_settings
. It is an array that is loaded by the WC_Settings_API->init_settings()
.
If you want to get access to it directly you can use get_option()
:
$free_shipping_settings = get_option( 'woocommerce_free_shipping_settings' );
$min_amount = $free_shipping_settings['min_amount'];
The accepted answer no longer works as of WooCommerce version 2.6. It still gives an output, but that output is wrong, since it doesn't make use of the newly introduced Shipping Zones.
In order to get the minimum spending amount for free shipping in a specific zone, try u this function I put together:
/**
* Accepts a zone name and returns its threshold for free shipping.
*
* @param $zone_name The name of the zone to get the threshold of. Case-sensitive.
* @return int The threshold corresponding to the zone, if there is any. If there is no such zone, or no free shipping method, null will be returned.
*/
function get_free_shipping_minimum($zone_name = 'England') {
if ( ! isset( $zone_name ) ) return null;
$result = null;
$zone = null;
$zones = WC_Shipping_Zones::get_zones();
foreach ( $zones as $z ) {
if ( $z['zone_name'] == $zone_name ) {
$zone = $z;
}
}
if ( $zone ) {
$shipping_methods_nl = $zone['shipping_methods'];
$free_shipping_method = null;
foreach ( $shipping_methods_nl as $method ) {
if ( $method->id == 'free_shipping' ) {
$free_shipping_method = $method;
break;
}
}
if ( $free_shipping_method ) {
$result = $free_shipping_method->min_amount;
}
}
return $result;
}
Put the above function in functions.php and use it in a template like so:
$free_shipping_min = '45';
$free_shipping_en = get_free_shipping_minimum( 'England' );
if ( $free_shipping_en ) {
$free_shipping_min = $free_shipping_en;
}
echo $free_shipping_min;
Hope this helps someone out.