WooCommerce,在免费送货可用时隐藏运输方式



我正在使用woocommerce,发现我可以在免费送货时隐藏送货方法。

但是现在的问题是我需要隐藏两个而不是一个。每当我试图隐藏两个方法时,整个页面都会变黑。

我需要专家来帮我解决这个问题。提前万分感谢!!
// Hide standard shipping option when free shipping is available
add_filter( 'woocommerce_available_shipping_methods', 'hide_standard_shipping_when_free_is_available' , 10, 1 );
/**
 *  Hide Standard Shipping option when free shipping is available
 * 
 * @param array $available_methods
 */
function hide_standard_shipping_when_free_is_available( $available_methods ) {
    if( isset( $available_methods['free_shipping'] ) AND isset( $available_methods['local_delivery'], ['flat_rate'] ) ) {
        // remove standard shipping option
        unset( $available_methods['local_delivery'], ['flat_rate'] );
    }
    return $available_methods;
}

我做了一个插件,用于高级免费送货选项。这包括一个选项,隐藏其他航运时,免费是可用的。

你可能会喜欢:http://wordpress.org/plugins/woocommerce-advanced-free-shipping/

WooCommerce高级免费送货插件很好,但是当免费送货时,它不会隐藏其他送货方式。该插件可以在woocommerce管理设置上启用,但检查隐藏其他运输方法的选项无法检查和保存。我试着在WP选项db上覆盖它,但似乎它也不起作用。插件代码似乎有问题

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 => $rate ) {
            if ( 'free_shipping' === $rate->method_id ) {
                $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;

详情请参考此链接

最新更新