如果您想要为特定国家/地区仅显示一种运送方式,则需要一些额外的编码。在此示例中,我们将禁用所有运送方式,但 “免费送货” 。这里是添加到您的 functions.php 的 PHP 代码
代码段#1:在 1 个运送区域中取消设置 1 个免费版本
/** * @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 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; }
可以在哪里添加此代码?
您可以将 PHP 代码片段放置在主题或子主题的 functions.php 文件的底部(如果是 CSS 代码,请添加到主题的 style.css 文件底部),修改之前建议先备份原始文件,若出现错误请先删除此代码。
这段代码是否正常可用?
或者是您有更好的解决方案想要分享?请到薇晓朵 WooCommerce 中文论坛留言告知,我们希望可以帮到更多国内的 WooCommerce 用户也希望您的参与。
需要关于 WooCommerce 的帮助?
请观看我们提供的免费视频教程或到薇晓朵 WooCommerce 中文论坛提问,会有专业技术团队提供相关帮助。