WooCommerce 2.6 - 通过达到特定金额触发免费送货时隐藏付费送货

Rea*_*eke 5 php wordpress shipping wordpress-plugin woocommerce

我最近在我的商店更新了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 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;
}
Run Code Online (Sandbox Code Playgroud)

虽然这不再起作用了.我需要一个新的修复程序,我不是真正的编码.

有人有解决方案吗?

以上解决方案来自此网站:
免费送货时隐藏其他送货方式

我猜测一些参数已经改变,因为他们更新了运输方法.

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

小智 3

请尝试用下面的代码片段替换您现有的代码片段。本文描述了该片段的详细信息。让我知道这是否可以改进。

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;
}
Run Code Online (Sandbox Code Playgroud)