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

相關問題