问题描述
我最近在我的商店更新到了 WooCommerce 2.6,他们也更新了他们的运输系统.在达到特定订单值并触发免费送货时,我使用它来隐藏付费送货选项之前:
I recently updated to WooCommerce 2.6 on my shop and they have updated their shipping system. Before I used this to hide the paid shipping option when an specific order value was reached and free shipping was triggered:
/**
* woocommerce_package_rates is a 2.1+ hook
*/
add_filter( 'woocommerce_package_rates', 'hide_shipping_when_free_is_available', 10, 2 );
/**
* Hide shipping rates when free shipping is available
*
* @param array $rates Array of rates found for the package
* @param array $package The package array/object being shipped
* @return array of modified rates
*/
function hide_shipping_when_free_is_available( $rates, $package ) {
// Only modify rates if free_shipping is present
if ( isset( $rates['free_shipping'] ) ) {
// To unset a single rate/method, do the following. This example unsets flat_rate shipping
unset( $rates['flat_rate'] );
// To unset all methods except for free_shipping, do the following
$free_shipping = $rates['free_shipping'];
$rates = array();
$rates['free_shipping'] = $free_shipping;
}
return $rates;
}
虽然这不再起作用了.我需要一个新的修复程序,但我并不真正喜欢编码.
Although this does not work anymore. I need a new fix and im not really into coding.
有没有人有办法解决这个问题?
Does anyone have a solution to this?
以上解决方案来自此站点:
免费送货时隐藏其他送货方式
The above solution was from this site:
Hide other shipping methods when FREE SHIPPING is available
我猜自从更新了运输方式后,某些参数发生了变化.
I'm guessing that some parameters have changed since they updated the shipping methods.
我希望有人知道如何解决这个问题.
I hope some one out there knows how to fix this.
推荐答案
请尝试用以下代码段替换现有代码段.此代码段的详细信息在 这篇文章.让我知道这是否可以改进.
Please try replacing your existing snippet with the below one. Details of this snippet is described in this article. Let me know if this can be improved.
add_filter('woocommerce_package_rates', 'xa_hide_shipping_rates_when_free_is_available', 10, 2);
function xa_hide_shipping_rates_when_free_is_available($rates, $package)
{
global $woocommerce;
$version = "2.6";
if (version_compare($woocommerce->version, $version, ">=")) {
foreach($rates as $key => $value) {
$key_part = explode(":", $key);
$method_title = $key_part[0];
if ('free_shipping' == $method_title) {
$free_shipping = $rates[$key];
// Unset all rates.
$rates = array();
// Restore free shipping rate.
$rates[$key] = $free_shipping;
return $rates;
}
}
}
else {
if (isset($rates['free_shipping'])) {
// Below code is for unsetting single shipping method/option.
// unset($rates['flat_rate']);
$free_shipping = $rates['free_shipping'];
// Unset all rates.
$rates = array();
// Restore free shipping rate.
$rates['free_shipping'] = $free_shipping;
}
}
return $rates;
}
这篇关于WooCommerce 2.6 - 当达到特定金额触发免费送货时隐藏付费送货的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!