在结帐页面获取“免费送货”方法的最低订单金额

时间:2017-02-13 09:48:53

标签: php wordpress woocommerce shipping orders

我确实试过使用这个答案中的代码:
How to get minimum order amount for free shipping in woocommerce

但它返回 NULL 结果,直到现在我找不到修复此代码的方法。

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

由于

5 个答案:

答案 0 :(得分:4)

  

这个答案的代码:How to get minimum order amount for free shipping in woocommerce
已经过时与WooCommerce版本2.6+,但它对这个功能性答案很有帮助...

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

enter image description here

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

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

宾果!你懂了。

答案 1 :(得分:0)

你需要通过获取,

获得选项
get_option( 'woocommerce_free_shipping_1_settings' ); 

然后通过执行

来反序列化数据
maybe_unserialize();

答案 2 :(得分:0)

正确的方法。

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

答案 3 :(得分:0)

这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'

您可以在文档中查看它:

答案 4 :(得分:-1)

也许它会帮助某人。您可以创建新的免费送货对象并从中获取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;
}