Php WooCommerce 2.6-当达到特定金额触发免费配送时隐藏付费配送
我最近在我的店铺上更新到WooCommerce 2.6,他们已经更新了他们的配送系统。在我使用此选项隐藏达到特定订单值并触发免费配送时的付费配送选项之前:Php WooCommerce 2.6-当达到特定金额触发免费配送时隐藏付费配送,php,wordpress,woocommerce,shipping,Php,Wordpress,Woocommerce,Shipping,我最近在我的店铺上更新到WooCommerce 2.6,他们已经更新了他们的配送系统。在我使用此选项隐藏达到特定订单值并触发免费配送时的付费配送选项之前: /** * woocommerce_package_rates is a 2.1+ hook */ add_filter( 'woocommerce_package_rates', 'hide_shipping_when_free_is_available', 10, 2 ); /** * Hide shipping rates wh
/**
* 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;
}
虽然这已经不起作用了。我需要一个新的补丁,我不太喜欢编码
有人能解决这个问题吗
上述解决方案来自此网站:我猜自从他们更新了运输方法后,一些参数已经改变了
我希望有人知道如何解决这个问题。请尝试用下面的代码片段替换现有的代码片段。中介绍了此代码段的详细信息。让我知道这是否可以改进
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 - HIDE OTHER SHIPPING METHODS WHEN FREE SHIPPING IS AVAILABLE #####
add_filter('woocommerce_package_rates', 'hide_shipping_when_free_is_available', 10, 2);
function hide_shipping_when_free_is_available($rates, $package) {
$free_yn = 0;
$pickup_yn = 0;
foreach($rates as $key => $value) {
$key_part = explode(":", $key);
$method_title = $key_part[0];
if ('free_shipping' == $method_title) {
// check if free shipping rate exists
$free_yn = 1;
$free_shipping = $rates[$key];
$free_key = $key;
}
if ('local_pickup' == $method_title) {
// check if local pickup rate exists
$pickup_yn = 1;
$local_pickup = $rates[$key];
$pickup_key = $key;
}
}
if ($free_yn == 1) {
// Unset all rates.
$rates = array();
// Restore free shipping rate.
$rates[$free_key] = $free_shipping;
if ($pickup_yn == 1) {
// Restore local pickup rate.
$rates[$pickup_key] = $local_pickup;
}
return $rates;
}
return $rates;
}
如果您已经删除了旧的装运方法(装运方法必须使用新的装运区域进行设置),则可以使用以下代码段在免费装运可用时删除所有其他装运方法。(2.6+):
从中,此代码是否正常工作?因为我看不到我的woo 2.6.7的变化
/**
* Hide shipping rates when free shipping is available.
* Updated to support WooCommerce 2.6 Shipping Zones.
*
* @param array $rates Array of rates found for the package.
* @return array
*/
function my_hide_shipping_when_free_is_available( $rates ) {
$free = array();
foreach ( $rates as $rate_id => $rate ) {
if ( 'free_shipping' === $rate->method_id ) {
$free[ $rate_id ] = $rate;
break;
}
}
return ! empty( $free ) ? $free : $rates;
}
add_filter( 'woocommerce_package_rates', 'my_hide_shipping_when_free_is_available', 100 );