我试图隐藏所有其他运输方法有条件的,如果一个特定的存在.
以下是我的代码:
function hide_shipping_when_alg_wc_shipping_31_is_present( $rates, $package ) {
// Check if alg_wc_shipping:31 is present
$alg_wc_shipping_31_present = false;
foreach ( $package->get_shipping_items() as $item ) {
if ( $item->needs_shipping() && $item->get_shipping_class() === 'alg_wc_shipping:31' ) {
$alg_wc_shipping_31_present = true;
break;
}
}
// If alg_wc_shipping:31 is present, hide alg_wc_shipping:30 and alg_wc_shipping:29
if ( $alg_wc_shipping_31_present ) {
foreach ( $rates as $rate_key => $rate ) {
if ( in_array( $rate->get_method_id(), array( 'alg_wc_shipping:30', 'alg_wc_shipping:29' ) ) ) {
unset( $rates[ $rate_key ] );
}
}
}
return $rates;
}
add_filter( 'woocommerce_package_rates', 'hide_shipping_when_alg_wc_shipping_31_is_present', 10, 2 );
但这并不管用. 有人能告诉我这个代码出了什么问题吗?
我试了几种方法,但都不管用.