WP WooCommerce 从结帐下拉列表中获取国家/地区
WP WooCommerce get country from checkout dropdown for hook
我正在尝试通过他们自己的 API 获得第 3 方运费。他们需要国家、重量和服务。我发送了带有硬编码值的 HTTP 请求。但是,当我尝试获取实际值时,我似乎在国家/地区方面遇到了困难。
当用户更改国家/地区时需要重新发送价格,目前我正在寻找默认值,在本例中为英国。
但是,我无法使用以下挂钩获取该值:
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 );
}
首先需要做的是获取预加载(默认)的当前国家/地区。然后,如果用户更改此设置,它会再次询问 API 新国家/地区,并应用新价格。
None 我在上面尝试过的钩子可以让我得到任何实际值,我想知道正确的过滤器是什么?
谢谢阿迪
您应该先尝试使用:
WC()->customer->get_shipping_country()
… 因为它将根据客户的变化进行更新,并且由 woocommerce 的 auto-detect 位置功能填充。
您也可以尝试使用其中之一:
$shipping_counrtry = WC()->session->get('customer')['shipping_country'];
或
$package = WC()->shipping->get_packages()[0];
$custome_shipping_country = $package['destination']['country'];
您还可以使用如下组合:
$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'];
}
所以在你的代码中:
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 );
}
此代码位于您的活动子主题(或主题)的 function.php 文件中。
应该可以。
我使用 Shipping 而不是 billing,因为如果客户不提供发货数据,发货国家/地区将填充账单数据。
我正在尝试通过他们自己的 API 获得第 3 方运费。他们需要国家、重量和服务。我发送了带有硬编码值的 HTTP 请求。但是,当我尝试获取实际值时,我似乎在国家/地区方面遇到了困难。
当用户更改国家/地区时需要重新发送价格,目前我正在寻找默认值,在本例中为英国。
但是,我无法使用以下挂钩获取该值:
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 );
}
首先需要做的是获取预加载(默认)的当前国家/地区。然后,如果用户更改此设置,它会再次询问 API 新国家/地区,并应用新价格。
None 我在上面尝试过的钩子可以让我得到任何实际值,我想知道正确的过滤器是什么?
谢谢阿迪
您应该先尝试使用:
WC()->customer->get_shipping_country()
… 因为它将根据客户的变化进行更新,并且由 woocommerce 的 auto-detect 位置功能填充。
您也可以尝试使用其中之一:
$shipping_counrtry = WC()->session->get('customer')['shipping_country'];
或
$package = WC()->shipping->get_packages()[0];
$custome_shipping_country = $package['destination']['country'];
您还可以使用如下组合:
$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'];
}
所以在你的代码中:
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 );
}
此代码位于您的活动子主题(或主题)的 function.php 文件中。
应该可以。
我使用 Shipping 而不是 billing,因为如果客户不提供发货数据,发货国家/地区将填充账单数据。