所以我试图找到一个解决方案,通过他们的位置和卖家的位置之间的距离为我的客户收费,但找不到合适的东西。我发现很少有使用Google API在仓库和货运地点之间做类似工作的插件,但它们只处理一个仓库。我实际上有几个供应商在不同的地点有仓库。根据卖家和客户之间的距离的运费
我使用woocommerce和wc供应商插件。
仅供参考...我已经设置了一个meta字段,使用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供应商插件很好。即使你设法给我任何做这项工作的任何插件的链接,这也是值得赞赏的。
所以,@loictheaztec提供了一个解决方案如下。我已经根据他的代码写了一段代码。不幸的是,它不适合我。
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;
}
嗨@loictheaztec,谢谢您的配合。不幸的是,它不适合我。请看下面的答案,因为这里写的太长。 – MAD
你能再看看这个问题吗?我在问题的底部添加了几行,谢谢 – MAD