WooCommerce 教程:当有免费送货时隐藏其他送货选项

WooCommerce 教程:当有免费送货时隐藏其他送货选项

如果您想要为特定国家/地区仅显示一种运送方式,则需要一些额外的编码。在此示例中,我们将禁用所有运送方式,但 “免费送货” 。这里是添加到您的 functions.php 的 PHP 代码

代码段#1:在 1 个运送区域中取消设置 1 个免费版本

要找到新的 “运送方式名称”,例如 “ free_shipping:8

/**
 * @snippet       Hide one shipping option in one zone when Free Shipping is available
 * @sourcecode    https://businessbloomer.com/?p=260
 * @author        Rodolfo Melogli
 * @compatible    WooCommerce 3.1.1
 */

add_filter( 'woocommerce_package_rates', 'bbloomer_unset_shipping_when_free_is_available_in_zone', 10, 2 );

function bbloomer_unset_shipping_when_free_is_available_in_zone( $rates, $package ) {

 	// Only unset rates if free_shipping is available
  	if ( isset( $rates['free_shipping:8'] ) ) {
  	unset( $rates['flat_rate:1'] );
}

return $rates;

}

代码段#1:在可用空闲时,在所有区域中取消设置所有费率


/**
 * @snippet       Hide ALL shipping rates in ALL zones when Free Shipping is available
 * @sourcecode    https://businessbloomer.com/?p=260
 * @author        Rodolfo Melogli
 * @compatible    WooCommerce 3.1.1
 */

add_filter( 'woocommerce_package_rates', 'bbloomer_unset_shipping_when_free_is_available_all_zones', 10, 2 );

function bbloomer_unset_shipping_when_free_is_available_all_zones( $rates, $package ) {

 	$all_free_rates = array();

        foreach ( $rates as $rate_id => $rate ) {
		if ( 'free_shipping' === $rate->method_id ) {
			$all_free_rates[ $rate_id ] = $rate;
			break;
		}
	}

	if ( empty( $all_free_rates )) {
        return $rates;
        } else {
        return $all_free_rates;
        }
}

对于旧版本的 WooCommerce [2.1-2.5](当 Free 可用时,取消设置一个速率)


/**
 * @snippet       Hide one shipping option when Free Shipping is available
 * @sourcecode    https://businessbloomer.com/?p=260
 * @author        Rodolfo Melogli
 * @compatible    WooCommerce 2.4.10
 */

add_filter( 'woocommerce_package_rates', 'bbloomer_unset_shipping_when_free_is_available', 10, 2 );

function bbloomer_unset_shipping_when_free_is_available( $rates, $package ) {

 	// Only unset rates if free_shipping is available
  	if ( isset( $rates['free_shipping'] ) ) {
  		unset( $rates['flat_rate'] );
  		return $rates;
}

}

对于旧版本的 WooCommerce [2.1-2.5](免费提供时,设置所有费率)


/**
 * @snippet       Hide ALL shipping options when Free Shipping is available
 * @sourcecode    https://businessbloomer.com/?p=260
 * @author        Rodolfo Melogli
 * @compatible    WooCommerce 2.4.10
 */

add_filter( 'woocommerce_package_rates', 'bbloomer_unset_shipping_when_free_is_available', 10, 2 );

function bbloomer_unset_shipping_when_free_is_available( $rates, $package ) {

 	// Only unset rates if free_shipping is available
  	if ( isset( $rates['free_shipping'] ) ) {

  	$free_shipping = $rates['free_shipping'];
	$rates = array();
	$rates['free_shipping'] = $free_shipping;
	}

	return $rates;
}

请注意:如果您无法使其工作,您必须清除 WooCommerce 缓存(WooCommerce> 系统状态)。见下图。

达到 WooCommerce 2.1


// 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['flat_rate'] ) ) {

        // remove standard shipping option
        unset( $available_methods['flat_rate'] );
    }

    return $available_methods;
}

如何添加此代码?

1 、您可以将 PHP 代码片段放置在主题或子主题的 functions.php 文件的底部(如果是 CSS 代码,请添加到子主题的 style.css 文件底部)修改之前建议先备份原始文件,若出现错误请先删除此代码。

2 、 WordPress 4.9 后改进了主题编辑器,对于 CSS 代码也可打开网站前台编辑器的【自定义】,复制代码添加到自定义 css 中。

此代码是否可用?

如需帮助或是您有更好的方案想分享?请到薇晓朵 WooCommerce 中文论坛留言告知,我们希望可以帮到更多国内的 WooCommerce 用户也希望您的参与。

文章没看懂?代码不会用?需要帮助您可以去论坛提问自助服务台

作者园长

分享关于您的一些信息。可能会被公开。