gpt4 book ai didi

php - WooCommerce 2.6 - 在达到特定数量触发免费送货时隐藏付费送货

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:37:50 24 4
gpt4 key购买 nike

我最近在我的商店更新到 WooCommerce 2.6,他们也更新了他们的运输系统。在达到特定订单值(value)并触发免费送货时,我使用它来隐藏付费送货选项之前:

/**
* 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;
}

虽然这不再起作用了。我需要一个新的修复程序,但我并不真正喜欢编码。

有没有人能解决这个问题?

以上解决方案来自这个网站:
Hide other shipping methods when FREE SHIPPING is available

我猜自从他们更新了运输方式后,一些参数发生了变化。

我希望有人知道如何解决这个问题。

最佳答案

请尝试用以下代码替换您现有的代码段。 this article 中描述了此片段的详细信息.让我知道这是否可以改进。

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;
}

关于php - WooCommerce 2.6 - 在达到特定数量触发免费送货时隐藏付费送货,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37830965/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com