强制免费送货而不隐藏 WooCommerce 中的所有其他送货选项
Force free shipping without hiding all other Shipping options in WooCommerce
在 Woocommerce 中,我们使用以下代码隐藏除免费送货外的所有送货方式:
function my_hide_shipping_when_free_is_available( $rates ) {
$free = array();
foreach ( $rates as $rate_id => $rate ) {
if ( 'free_shipping' === $rate->method_id ) {
$free[ $rate_id ] = $rate;
break;
}
}
return ! empty( $free ) ? $free : $rates;
}
add_filter( 'woocommerce_package_rates', 'my_hide_shipping_when_free_is_available', 100 );
现在我们想保留快递和当地取货。但是,应预先选择免费送货。
有人知道我们如何自定义代码吗?
我们的送货方式费率 ID 是:
- 正常发货 (Versandkosten):
legacy_flat_rate
- 快递 (Expressversand):
legacy_flat_rateexpress
- 免运费 (kostenloser Versand):
legacy_free_shipping
- 本地取件(Abholung vor Ort):
legacy_local_pickup
要仅隐藏正常交付,当 "Free shipping" 可用时,您将需要一些不同的东西:
add_filter( 'woocommerce_package_rates', 'show_hide_shipping_methods', 100 );
function show_hide_shipping_methods( $rates ) {
// When "Free shipping" is available
if( isset($rates['legacy_free_shipping']) && isset($rates['legacy_flat_rate']) ) {
// Hide normal flat rate
unset($rates['legacy_flat_rate']);
}
return $rates;
}
以下将 "Free shipping" 设置为 默认 选择的送货方式:
add_filter( 'woocommerce_shipping_chosen_method', 'set_default_chosen_shipping_method', 10, 3 );
function set_default_chosen_shipping_method( $default, $rates, $chosen_method ) {
if( isset($rates['legacy_free_shipping']) ) {
$default = 'legacy_free_shipping';
}
return $default;
}
代码进入您的活动子主题(或活动主题)的 functions.php 文件。测试和工作。
Refresh the shipping caches: (required)
- This code is already saved on your active theme's function.php file.
- The cart is empty
- In a shipping zone settings, disable / save any shipping method, then enable back / save.
在 Woocommerce 中,我们使用以下代码隐藏除免费送货外的所有送货方式:
function my_hide_shipping_when_free_is_available( $rates ) {
$free = array();
foreach ( $rates as $rate_id => $rate ) {
if ( 'free_shipping' === $rate->method_id ) {
$free[ $rate_id ] = $rate;
break;
}
}
return ! empty( $free ) ? $free : $rates;
}
add_filter( 'woocommerce_package_rates', 'my_hide_shipping_when_free_is_available', 100 );
现在我们想保留快递和当地取货。但是,应预先选择免费送货。
有人知道我们如何自定义代码吗?
我们的送货方式费率 ID 是:
- 正常发货 (Versandkosten):
legacy_flat_rate
- 快递 (Expressversand):
legacy_flat_rateexpress
- 免运费 (kostenloser Versand):
legacy_free_shipping
- 本地取件(Abholung vor Ort):
legacy_local_pickup
要仅隐藏正常交付,当 "Free shipping" 可用时,您将需要一些不同的东西:
add_filter( 'woocommerce_package_rates', 'show_hide_shipping_methods', 100 );
function show_hide_shipping_methods( $rates ) {
// When "Free shipping" is available
if( isset($rates['legacy_free_shipping']) && isset($rates['legacy_flat_rate']) ) {
// Hide normal flat rate
unset($rates['legacy_flat_rate']);
}
return $rates;
}
以下将 "Free shipping" 设置为 默认 选择的送货方式:
add_filter( 'woocommerce_shipping_chosen_method', 'set_default_chosen_shipping_method', 10, 3 );
function set_default_chosen_shipping_method( $default, $rates, $chosen_method ) {
if( isset($rates['legacy_free_shipping']) ) {
$default = 'legacy_free_shipping';
}
return $default;
}
代码进入您的活动子主题(或活动主题)的 functions.php 文件。测试和工作。
Refresh the shipping caches: (required)
- This code is already saved on your active theme's function.php file.
- The cart is empty
- In a shipping zone settings, disable / save any shipping method, then enable back / save.