get option woocommerce_free_shipping_min_amount

取得 WooCommerce 當前運送區域免運送最低金額

/**
 * 取免運費金額,由運送區域
 * 
 * @param $zone_name = "" 運送區域名稱,預設當前運送區域
 * 
 * ex: 
 * $min_amount = get_free_shipping_minimum();
 * $min_amount = get_free_shipping_minimum("台灣本島");
 * 
 * @since 1.00.2
 * 
 * @link https://v123.tw
 * @link https://stackoverflow.com/a/54870050/6784662
 * @link https://wordpress.stackexchange.com/a/334354/116304
 * 
 * @return float
 */
function v123_get_free_shipping_min_amount($zone_name = '')
{
    if (empty($zone_name)) {
        // Get cart shipping packages
        $shipping_packages =  WC()->cart->get_shipping_packages();
        // Get the WC_Shipping_Zones instance object for the first package
        $shipping_zone = wc_get_shipping_zone(reset($shipping_packages));
        $zone_name = $shipping_zone->get_zone_name();
    }

    $result = 0;
    $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 floatval($result);
}


// add the action 
add_action('woocommerce_init', 'v123_get_free_shipping_min_amount', 10, 1);

Example:

$min_amount = v123_get_free_shipping_min_amount();
$min_amount = v123_get_free_shipping_min_amount("台灣本島");

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *

這個網站採用 Akismet 服務減少垃圾留言。進一步了解 Akismet 如何處理網站訪客的留言資料