WP WooCommerce 从钩子的结帐下拉菜单中获取国家/地区

Bad*_*ddy 2 wordpress woocommerce hook-woocommerce

我正在尝试通过他们自己的 API 获得第 3 方运费。他们需要国家、重量和服务。我使用硬编码值发送 HTTP 请求。但是当我试图获得实际值时,我似乎在谈到 Country 时碰壁了。

在此处输入图片说明

当用户更改国家/地区时,需要重新发送价格,目前我正在寻找默认值,在这种情况下是英国。

但是,我无法使用以下挂钩获得该值:

woocommerce_shipping_fields

woocommerce_checkout_get_value

这是当前的代码,在这里它动态获取权重:

add_action( 'woocommerce_cart_calculate_fees', 'shipping_weight_fee', 30, 1 );
function shipping_weight_fee( $cart ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    $url  = 'http://********/shipping/read.php';
    $args =  array(
        'body' => array(
            'weight' => $cart->get_cart_contents_weight(),
            'location' => 'United Kingdom',
            'service' => 1
        )
    );
    $data = wp_remote_post( $url, $args );
    $p = json_decode($data['body']);
    //print_r($p);

    $fee = $p->Data->rate;
    // Setting the calculated fee based on weight
    $cart->add_fee( __( 'Shipping Rate' ), $fee, false );
}
Run Code Online (Sandbox Code Playgroud)

作为开始,需要进行的是获取预加载(默认)的当前国家/地区。然后,如果用户更改了此设置,则它会再次使用新国家/地区询问 API,并应用该新价格。

我上面尝试过的所有钩子都没有为我提供任何实际价值,而正确的过滤器是什么?

谢谢阿迪

Loi*_*tec 5

您应该先尝试使用:

WC()->customer->get_shipping_country()
Run Code Online (Sandbox Code Playgroud)

......因为它将随着客户的变化而更新,并且由 woocommerce 的自动检测位置功能填充。

您也可以尝试使用其中之一:

$shipping_counrtry = WC()->session->get('customer')['shipping_country'];
Run Code Online (Sandbox Code Playgroud)

或者

$package = WC()->shipping->get_packages()[0];
$custome_shipping_country = $package['destination']['country'];
Run Code Online (Sandbox Code Playgroud)

您还可以使用以下组合:

$customer_shipping_country = WC()->customer->get_shipping_country();

if( empty($custome_shipping_country) ){
    $package = WC()->shipping->get_packages()[0];
    if( ! isset($package['destination']['country']) ) return $passed;
    $customer_shipping_country = $package['destination']['country'];
}
Run Code Online (Sandbox Code Playgroud)

所以在你的代码中:

add_action( 'woocommerce_cart_calculate_fees', 'shipping_weight_fee', 30, 1 );
function shipping_weight_fee( $cart ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    $custome_shipping_country = WC()->customer->get_shipping_country();

    if( empty($custome_shipping_country) ){
        $package = WC()->shipping->get_packages()[0];
        if( ! isset($package['destination']['country']) ) return $passed;
        $customer_shipping_country = $package['destination']['country'];
    }

    $url  = 'http://********/shipping/read.php';
    $args =  array(
        'body' => array(
            'weight' => $cart->get_cart_contents_weight(),
            'location' => $custome_shipping_country,
            'service' => 1
        )
    );
    $data = wp_remote_post( $url, $args );
    $p = json_decode($data['body']);
    //print_r($p);

    $fee = $p->Data->rate;
    // Setting the calculated fee based on weight
    $cart->add_fee( __( 'Shipping Rate' ), $fee, false );
}
Run Code Online (Sandbox Code Playgroud)

此代码位于活动子主题(或主题)的 function.php 文件中

它应该有效。

我使用运输而不是计费,因为如果客户不提供运输数据,运输国家将填充计费数据。