更新时间:2023-11-30 11:45:40
已更新2: 您的代码中有很多错误和错误……
Updated 2: There are many errors and mistakes in your code…
您的代码中有一种错误的运输方式,因为您只有2:
THERE IS A MISSING SHIPPING METHOD IN YOUR CODE as you only have 2:
'shipping_by_rules:16'
==>标准递送(0-10公斤)’shipping_by_rules:15’
==>第二天交付'shipping_by_rules:16'
==> Standard delivery(0-10kg)'shipping_by_rules:15'
==> Next day delivery但是标准交付(11-20kg)
您应该尝试以下操作:
add_filter( 'woocommerce_package_rates', 'custom_hide_shipping_methods', 20, 2 );
function custom_hide_shipping_methods( $rates, $package ) {
$targeted_class_id = 149; // <== ID OF MY SHIPPING_CLASS
$has_class = $has_no_class = false;
// Loop through cart items
foreach( WC()->cart->get_cart() as $cart_item ) {
$shipping_class = $cart_item['data']->get_shipping_class_id();
# $weight = $cart_item['data']->get_weight();
if( $shipping_class == $targeted_class_id )
$has_class = true;
elseif( empty( $shipping_class ) )
$has_no_class = true;
}
// Unseting shipping methods
if( $has_class )
{ // CASE 1 and 3
unset( $rates['shipping_by_rules:16'] ); // standard delivery
# wc_add_notice( sprintf( __( '1 | %s', 'woocommerce' ), $weight ), 'error' );
}
elseif( ! $has_class && $has_no_class )
{ // CASE 2
unset( $rates['shipping_by_rules:15'] ); // next day delivery
# wc_add_notice( sprintf( __( '2 | %s', 'woocommerce' ), $weight ), 'error' );
}
elseif( $has_class && $has_no_class ) // ==> Optional (You may not neeed it)
{ // CASE 3
unset( $rates['shipping_by_rules:16'] ); // standard delivery
# wc_add_notice( sprintf( __( '3 | %s', 'woocommerce' ), $weight ), 'error' );
}
return $rates;
}
代码位于活动子主题(或活动主题)的function.php文件中
应该可以。