在结帐页面中获取 'Free Shipping' 方法的最小订单量

Getting minimum order amount for 'Free Shipping' method in checkout page

我曾尝试使用此答案中的代码:
How to get minimum order amount for free shipping in woocommerce

但它 return 一个 NULL 结果,直到现在我都找不到修复此代码的方法。

如何在结帐页面上获得正确的最低订单金额

谢谢

您需要通过获取 ,

来获取选项
get_option( 'woocommerce_free_shipping_1_settings' ); 

然后反序列化数据,

maybe_unserialize();

The code of this answer: How to get minimum order amount for free shipping in woocommerce
is obsolete with WooCommerce version 2.6+, but it was helpful for this functional answer…

经过一些搜索和尝试,我找到了获取特定区域(区域)在免费送货方法中设置的最低订单金额的方法:

这是经过测试的工作代码(解释在里面注释):

// Here you get (as you already know) the used shipping method reference
$chosen_methods = WC()->session->get( 'chosen_shipping_methods' );

// Replacing inside the string ':' by '_'
$option_value = str_replace(':', '_', $chosen_methods[0]);

// We concatenate the string with additional sub-strings
$option_value = 'woocommerce_'.$option_value.'_settings';

// Just a test => outputting the string to see what we get
echo $option_value; echo '<br>';

// Now we can get the options values with that formatted string
$free_shipping_settings = get_option( $option_value );

// Just for test => we output the (pre-formatted) array of values to check
echo '<pre>'; print_r($free_shipping_settings); echo '</pre><br>'; 

// Here we get the value of the order min amount (Finally!)
$order_min_amount = $free_shipping_settings['min_amount'];

// We output the value (to check)
echo 'Order min amount: '.$order_min_amount;

宾果!你明白了。

也许它会对某人有所帮助。您可以创建新的免费送货对象并从中获取 min_amount。我认为它比@LoicTheAztec 的回答更简单。

if ( class_exists( 'WC_Shipping_Free_Shipping' ) ) {
    $free_shipping = new WC_Shipping_Free_Shipping();
    $min_amount = $free_shipping->min_amount;
    echo $min_amount;
}

得到这个的正确方法..

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

这 2 个过滤器允许您获取最小订单金额甚至更改其值:

  • woocommerce_shipping_free_shipping_instance_option
  • woocommerce_shipping_free_shipping_option

您可以像这样使用它,例如:

add_filter( 'woocommerce_shipping_free_shipping_instance_option', 'get_free_shipping_min_amount', 10, 3 );
add_filter( 'woocommerce_shipping_free_shipping_option', 'get_free_shipping_min_amount', 10, 3 );
function get_free_shipping_min_amount( $option, $key, $method ){
    if (
        'min_amount' !== $key ||
        ! is_numeric( $option )     
    ) {
        return $option;
    }
    // Minimum amount
    $min_amount = $option;
    return $option;
}

如果您愿意,您可以使用 $method 参数(例如 instance_id 或类似的参数)从运输方式中获取更多信息。

您还可以从挂钩中替换“free_shipping”以获取其他运输方式的信息。钩子是这样工作的: 'woocommerce_shipping_' . $shipping_method_id . '_option'.

你可以在文档上看一下: