2017-08-28 79 views
1

所以我试图找到一个解决方案,通过他们的位置和卖家的位置之间的距离为我的客户收费,但找不到合适的东西。我发现很少有使用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; 
} 

回答

0

假设您拥有供应商与客户之间的公里距离。

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); 
} 
1
  1. 首先你会为每个航运区一个“固定费率”航运法的50成本(您可以启用税或没有)。
  2. 您应该添加一个自定义函数,该函数挂钩了woocommerce_package_rates过滤器挂钩。
  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文件或也以任何插件文件。

经过测试和工作。

有时是必要的刷新航运缓存:
1)首先你的车是空的。
2)代码已经保存在你的function.php文件中。
3)进入运输区域,并禁用一个“统一费率”(例如)和“保存”。然后重新启用“统一费率”和“保存”。 你完成了。

+0

嗨@loictheaztec,谢谢您的配合。不幸的是,它不适合我。请看下面的答案,因为这里写的太长。 – MAD

+0

你能再看看这个问题吗?我在问题的底部添加了几行,谢谢 – MAD