gpt4 book ai didi

php - 运费取决于卖家和客户之间的距离

转载 作者:行者123 更新时间:2023-12-04 16:09:14 24 4
gpt4 key购买 nike

因此,我试图找到一种根据客户位置与卖家位置之间的距离向客户收费的解决方案,但找不到任何合适的方法。我发现很少有插件使用 Google API 做类似的工作,比如在仓库和发货地点之间收取不同的运费,但它们只处理一个特定的地点作为仓库。实际上,我有几家供应商,他们在不同地点设有仓库。

我正在使用 woocommerce 和 wc 供应商插件。

仅供引用...,我设置了一个元字段,使用 Google Api 收集供应商的纬度和经度。

//一个例子://

Vendor 1 – Location 1 Vendor 2 – Location 2

A customer places order from vendor 1. Customer’s location is in Location A. API will calculate the distance between the cart item’s vendor’s location, which is Location 1, and customer’s location, which is Location 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;

//示例结束//

注意我只是找不到任何适用于 WC Vendor 插件的远程运费插件。即使您设法给我任何执行此工作的插件的任何链接,我也将不胜感激。


所以,@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;
}

最佳答案

  1. 首先,您将为每个运输区域设置一个“统一费率”运输方式,费用为 50(您可以启用或不启用税收)。
  2. 您应该添加一个 Hook 在 woocommerce_package_rates 过滤器 Hook 中的自定义函数。
  3. 在此自定义函数中,您必须设置 API 代码以获取客户和供应商之间的距离。

这是代码:

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 文件中或任何插件文件中。

经过测试并有效。

Sometimes is necessary to refresh the shipping caches:
1) First your cart is empty.
2) The code is already saved on your function.php file.
3) Go in a shipping zone and disable one "flat rate" (for example) and "save". Then re-enable that "flat rate" and "save". You are done.

关于php - 运费取决于卖家和客户之间的距离,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45917984/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com