自定义 WooCommerce 运输方式和距离费率值问题
Custom WooCommerce shipping Method and distance rate value issue
我正在尝试通过将总距离发送到外部 API 来根据 Woocommerce 上的 KM 距离计算运费 API,因此,为此,我在自己的运输插件中使用了此功能:
public function calculate_shipping( $packages = array() ) {
wp_parse_str( $_POST['post_data'], $post_data );
$cURLConnection = curl_init();
curl_setopt($cURLConnection, CURLOPT_URL, $this->url_api.$post_data["distancia_billing"]);
curl_setopt($cURLConnection, CURLOPT_RETURNTRANSFER, true);
$datos = curl_exec($cURLConnection);
curl_close($cURLConnection);
$jsonArrayResponse = json_decode($datos, true);
$rate = array(
'id' => $this->id,
'label' => $this->title,
'cost' => isset($jsonArrayResponse["precio"]) ? $jsonArrayResponse["precio"] : '10.00',
'calc_tax' => 'per_order'
);
$this->add_rate( $rate );
}
效果很好,在结账过程中显示正确的价格,但是当用户完成结账过程并且我在我的 Woocommerce 面板上检查订单时,总是显示默认值 (10.00) 而不是 API.
我的函数遗漏了什么?
谢谢
您需要将成本设置为 WC_Session
变量以避免丢失距离计算的成本值,因此您的函数代码应为:
public function calculate_shipping( $packages = array() ) {
wp_parse_str( $_POST['post_data'], $post_data );
$cURLConnection = curl_init();
curl_setopt($cURLConnection, CURLOPT_URL, $this->url_api.$post_data["distancia_billing"]);
curl_setopt($cURLConnection, CURLOPT_RETURNTRANSFER, true);
$datos = curl_exec($cURLConnection);
curl_close($cURLConnection);
$jsonArrayResponse = json_decode($datos, true);
if( isset($jsonArrayResponse["precio"]) && ! empty($jsonArrayResponse["precio"]) && isset(WC()->session) ) {
WC()->session->set('shipping_distance_cost', floatval($jsonArrayResponse["precio"]) );
}
$this->add_rate( array(
'id' => $this->id,
'label' => $this->title,
'cost' => WC()->session->__isset('shipping_distance_cost') ? WC()->session->get('shipping_distance_cost') : '10.00',
'calc_tax' => 'per_order'
) );
}
但是您应该需要在创建订单后删除该会话变量,因此在您的运输 class 代码之外,添加以下内容:
add_action( 'woocommerce_checkout_order_created', 'remove_wc_session_shipping_distance_cost' );
function remove_wc_session_shipping_distance_cost( $order ) {
if ( WC()->session->__isset('shipping_distance_cost') ) {
WC()->session->__unset('shipping_distance_cost')
}
}
应该可以。
我正在尝试通过将总距离发送到外部 API 来根据 Woocommerce 上的 KM 距离计算运费 API,因此,为此,我在自己的运输插件中使用了此功能:
public function calculate_shipping( $packages = array() ) {
wp_parse_str( $_POST['post_data'], $post_data );
$cURLConnection = curl_init();
curl_setopt($cURLConnection, CURLOPT_URL, $this->url_api.$post_data["distancia_billing"]);
curl_setopt($cURLConnection, CURLOPT_RETURNTRANSFER, true);
$datos = curl_exec($cURLConnection);
curl_close($cURLConnection);
$jsonArrayResponse = json_decode($datos, true);
$rate = array(
'id' => $this->id,
'label' => $this->title,
'cost' => isset($jsonArrayResponse["precio"]) ? $jsonArrayResponse["precio"] : '10.00',
'calc_tax' => 'per_order'
);
$this->add_rate( $rate );
}
效果很好,在结账过程中显示正确的价格,但是当用户完成结账过程并且我在我的 Woocommerce 面板上检查订单时,总是显示默认值 (10.00) 而不是 API.
我的函数遗漏了什么?
谢谢
您需要将成本设置为 WC_Session
变量以避免丢失距离计算的成本值,因此您的函数代码应为:
public function calculate_shipping( $packages = array() ) {
wp_parse_str( $_POST['post_data'], $post_data );
$cURLConnection = curl_init();
curl_setopt($cURLConnection, CURLOPT_URL, $this->url_api.$post_data["distancia_billing"]);
curl_setopt($cURLConnection, CURLOPT_RETURNTRANSFER, true);
$datos = curl_exec($cURLConnection);
curl_close($cURLConnection);
$jsonArrayResponse = json_decode($datos, true);
if( isset($jsonArrayResponse["precio"]) && ! empty($jsonArrayResponse["precio"]) && isset(WC()->session) ) {
WC()->session->set('shipping_distance_cost', floatval($jsonArrayResponse["precio"]) );
}
$this->add_rate( array(
'id' => $this->id,
'label' => $this->title,
'cost' => WC()->session->__isset('shipping_distance_cost') ? WC()->session->get('shipping_distance_cost') : '10.00',
'calc_tax' => 'per_order'
) );
}
但是您应该需要在创建订单后删除该会话变量,因此在您的运输 class 代码之外,添加以下内容:
add_action( 'woocommerce_checkout_order_created', 'remove_wc_session_shipping_distance_cost' );
function remove_wc_session_shipping_distance_cost( $order ) {
if ( WC()->session->__isset('shipping_distance_cost') ) {
WC()->session->__unset('shipping_distance_cost')
}
}
应该可以。