所以我试图找到一个解决方案,通过他们的位置和卖家的位置之间的距离向我的客户收费,但找不到合适的东西。我发现使用Google API在仓库和运输地点之间执行类似工作的插件很少,但是他们只处理一个特定位置作为仓库。实际上我有几家供应商在不同的地方都有仓库。
我正在使用woocommerce和wc vendor插件。
仅供参考...我设置了一个使用Google Api收集供应商纬度和经度的元字段。
//示例://
供应商1 - 位置1供应商2 - 位置2
客户从供应商处下订单1.客户的位置在 位置A.API将计算购物车项目之间的距离 供应商的位置,即位置1,以及客户的位置,即 是位置A.
Check the cart item's Vendor Get the location of Vendor If Location 1 to Location A = 3KM, Shipping Charge is 50; If Location 1 to Location A <= 5 KM, Shipping Charge is 100; Else Shipping Charge is 150;
//示例结尾//
N.B。我只是没有找到任何与WC Vendor插件兼容的远程运费的插件。即使你设法给我任何执行这项工作的插件的链接,也会受到赞赏。
所以,@ luictheaztec提供了以下解决方案。我根据他的代码编写了一段代码。不幸的是,它不适合我。
add_filter( 'woocommerce_package_rates', 'distance_shipping_calculated_surcharge', 100, 2 );
function distance_shipping_calculated_surcharge( $rates, $package ) {
$shippingurl = "https://maps.googleapis.com/maps/api/distancematrix/json?origins=VENDOR_LOCATION&destinations=CUSTOMER_LOCATION&key=MY_API_KEY";
//fetch json response from googleapis.com:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $shippingurl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = json_decode(curl_exec($ch), true);
//If google responds with a status of OK
//Extract the distance text:
if($response['status'] == "OK"){
$dist = $response['rows'][0]['elements'][0]['distance']['text'];
}
// Get only number from string
$distance= filter_var ( $dist, FILTER_SANITIZE_NUMBER_INT);
// Distance example
//$distance = 3.5; //(in KM)
if( $distance > 5 )
$cost_operator = 3; // (3 * 50 = 150)
elseif( $distance > 3 && $distance <= 5 )
$cost_operator = 2; // (2 * 50 = 100)
else
$cost_operator = 1; // (1 * 50 = 50)
// Iterating through each shipping rate
foreach($rates as $rate_key => $rate_values){
$method_id = $rate_values->method_id;
$rate_id = $rate_values->id;
// Targeting "Flat Rate" shipping method
if ( 'flat_rate' === $rate_values->method_id ) {
// Set the new calculated rate cost
$rates[$rate_id]->cost = number_format($rates[$rate_id]->cost * $cost_operator, 2);
// Taxes rate cost (if enabled)
foreach ($rates[$rate_id]->taxes as $key => $tax){
if( $rates[$rate_id]->taxes[$key] > 0 ){ // set the new tax cost
$rates[$rate_id]->taxes[$key] = number_format( $rates[$rate_id]->taxes[$key] * $cost_operator, 2 );
}
}
}
}
return $rates;
}
答案 0 :(得分:2)
woocommerce_package_rates
过滤器钩子中的自定义函数。以下是代码:
add_filter( 'woocommerce_package_rates', 'distance_shipping_calculated_surcharge', 100, 2 );
function distance_shipping_calculated_surcharge( $rates, $package ) {
## HERE set your API code to get the distance ##
// Distance example
$distance = 3.5; //(in KM)
if( $distance > 5 )
$cost_operator = 3; // (3 * 50 = 150)
elseif( $distance > 3 && $distance <= 5 )
$cost_operator = 2; // (2 * 50 = 100)
else
$cost_operator = 1; // (1 * 50 = 50)
// Iterating through each shipping rate
foreach($rates as $rate_key => $rate_values){
$method_id = $rate_values->method_id;
$rate_id = $rate_values->id;
// Targeting "Flat Rate" shipping method
if ( 'flat_rate' === $rate_values->method_id ) {
// Set the new calculated rate cost
$rates[$rate_id]->cost = number_format($rates[$rate_id]->cost * $cost_operator, 2);
// Taxes rate cost (if enabled)
foreach ($rates[$rate_id]->taxes as $key => $tax){
if( $rates[$rate_id]->taxes[$key] > 0 ){ // set the new tax cost
$rates[$rate_id]->taxes[$key] = number_format( $rates[$rate_id]->taxes[$key] * $cost_operator, 2 );
}
}
}
}
return $rates;
}
此代码包含活动子主题(或主题)的function.php文件或任何插件文件。
经过测试和工作。
有时需要更新送货缓存:
1)首先你的购物车是空的。
2)代码已保存在function.php文件中。
3)进入运输区并禁用一个&#34;统一费率&#34; (例如)和&#34;保存&#34;。然后重新启用&#34;统一费率&#34;并且&#34;保存&#34;。 你已经完成了。
答案 1 :(得分:0)
假设供应商与客户之间的距离为Km。
add_action( 'woocommerce_flat_rate_shipping_add_rate','add_another_custom_flat_rate', 10, 2 );
function add_another_custom_flat_rate( $method, $rate ) {
$new_rate = $rate;
/*let the distance is = $dis; */
if($dis==3){
$new_rate['cost'] = 50; // Update the cost to 50
} else if($dis<=5){
$new_rate['cost'] = 100; // Update the cost to 100
} else {
$new_rate['cost'] = 150; // Update the cost to 150
}
$method->add_rate( $new_rate );
}