- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
因此,我试图找到一种根据客户位置与卖家位置之间的距离向客户收费的解决方案,但找不到任何合适的方法。我发现很少有插件使用 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;
}
最佳答案
woocommerce_package_rates
过滤器 Hook 中的自定义函数。这是代码:
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/
我正在尝试获取运费。这是我的要求: 2017-10-20T11:28:56.000-08:00 pukka30193422993470
我有一个澳大利亚 PayPal 帐户,我正在使用该帐户通过基本网站付款按钮在我的网站上处理付款。送货区域似乎只适用于美国帐户。有什么办法可以为澳大利亚帐户设置不同的运输区域价格。 有点像 Domest
我想根据添加到购物车中的产品数量来计算运费,例如, 如果我购买一部手机,则运费为 2.5,如果我购买了两两部以上手机,则运费为 5.0 那么有什么想法或建议如何根据产品数量计算运费吗? 最佳答案
我们是否可以通过 API 调用或脚本在结账时添加自定义运费? 我想根据一些规则修改价格,但我不知道如何在 Shopify 中进行修改。任何帮助将不胜感激。 最佳答案 您可以通过实现自己的 Carrie
在托管的 PayPal 添加到购物车按钮上,我看到您可以在哪里使用: 是否有可能,如果: 数量为 1 运费总额为 '$x' 如果数量为 2 - 5,则运费总额为 '$xx' 任何超过 6 的总运费
我一直在寻找解决方案...我正在尝试根据添加到 Woocommerce 结帐页面的单选按钮选项来显示或隐藏特定运费。但我对 Ajax 和 JQuery 一无所知,我认为这是它所需要的。 基本上,如果用
我正在努力在产品页面上显示运费,例如在亚马逊和 eBay。 但是,因为我会比通常的购物车页面更多地调用 API,我想知道 USPS API 是否有任何限制可以阻止我在产品页面上使用 API? 你认为我
我正在用 NodeJS 和 MongoDB 开发一个公共(public)电子商务网站,我使用的是 forever 而不是 nginx 或类似的东西。 (请让我知道我是否因为这样做而完全疯了,以及为什么
我正在用 PHP 编写自定义商务 CMS。 我一直在集成 UPS XML API 以获取运费,目前它正在运行。我正在使用 Mark Sandborn 的 PHP 类 http://code.googl
我正在尝试在输入地址后更新运费。当客户开始输入地址时,自动完成功能会帮助客户找到街道名称、建筑物、楼层和侧面。 输入地址后,JavaScript 会计算距离和价格。 然后价格通过 AJAX 发送到 s
出于某种原因,当我尝试使用 SDK 授权 PayPal 付款时,我的日志中不断出现以下错误: ERROR: Got Http response code 400 when accessing http
我想如果订单高于 x 金额,则免费送货,否则需要收取 x 金额。 在免费送货中,我已经设置了 X 数量。 在统一费率中,我设置了 X 量。 但是在前端,如果订单高于 x 数量,则它会同时显示免费送货和
我是一名优秀的程序员,十分优秀!