2014-10-27 78 views
7

我如何获取所需的最低订单金额获得免费送货(woocommerce_free_shipping_min_amount这是设置在管理面板woocommerce - >设置 - >运费 - >免费送货 - >最低订单金额)在woocommerce?如何获得最低订单金额在woocommerce免费送货

我想显示此价位在前端页面

回答

8

此值存储在一个option下的关键woocommerce_free_shipping_settings。它是由WC_Settings_API->init_settings()加载的数组。

如果要访问它直接就可以使用get_option()

$free_shipping_settings = get_option('woocommerce_free_shipping_settings'); 
$min_amount = $free_shipping_settings['min_amount']; 
+0

谢谢。它的工作:) – Vidhi 2014-10-27 06:59:23

+1

我有投票您的答案,但此代码不再适用于WooCommerce版本2.6+ ...我有一个WooCommerce实际版本的功能答案在这里:http://stackoverflow.com/a/42201311/ 3730754 – LoicTheAztec 2017-02-13 15:40:42

2

接受的答案不再工作作为WooCommerce 2.6版本。它仍然会给出一个输出,但是这个输出是错误的,因为它没有使用新引入的运输区域。

为了获得特定区域中的免费送货的最低消费金额,尽量U该功能我放在一起:

/** 
* 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; 
} 

把上述功能的functions.php和喜欢的模板使用所以:

$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; 

希望这可以帮助别人。

+0

This Works。谢谢! – Moe 2017-12-12 08:21:22

相关问题