私は自分の場所と売り手の場所の間の距離で顧客を請求する解決策を見つけようとしていましたが、適切なものを見つけることができませんでした。 Google APIを使用して、倉庫と配送場所の間で異なる配送料金のような類似の仕事をするプラグインはほとんど見つかりませんでしたが、倉庫として特定の場所のみを扱っています。私は実際には異なる場所に倉庫を持ついくつかのベンダーを持っています。配送コストは、売り手と顧客の距離に基づいて
私はwoocommerceとwc vendor pluginを使用しています。
FYI ... Google Apiを使用してベンダーの緯度と経度を収集するメタフィールドを設定しました。
//例://
ある場所1であるカートアイテムの ベンダーの位置、および顧客の位置、との間の距離ベンダー1 - ロケーション1ベンダー2 - ロケーション2
顧客は、ベンダーからの発注を行う1.お客様の場所が 場所A. APIである計算します が位置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