我的推车有三种方法标准配送(0-10kg),标准配送(11-20kg)和次日送货(0-20kg),我的问题是当ia产品与冷冻食品运输类到购物车的运输方式应该只是第二天发货,如果现在有购物车上的运输类,它只有标准的发货,我的问题是当添加产品与运输类和没有运输类的产品,它不会转到我所做的条件。
add_filter( 'woocommerce_package_rates', 'custom_hide_shipping_methods', 10, 2 );
function custom_hide_shipping_methods( $rates, $package ) {
foreach( WC()->cart->get_cart() as $cart_item ) {
$product = $cart_item[ 'data' ]; // The WC_Product object
if( $product->get_shipping_class_id() == 149 ) { // <== ID OF MY SHIPPING_CLASS
unset( $rates['shipping_by_rules:16'] ); // standard delivery
wc_add_notice( sprintf( __( '1', 'woocommerce' ), $weight ), 'error' );
}else if($product->get_shipping_class_id() == NULL){
unset( $rates['shipping_by_rules:15'] ); // next day delivery
wc_add_notice( sprintf( __( '2', 'woocommerce' ), $weight ), 'error' );
}else if($product->get_shipping_class_id() != || $product->get_shipping_class_id() == 149){
unset( $rates['shipping_by_rules:16'] ); // standard delivery
wc_add_notice( sprintf( __( '3', 'woocommerce' ), $weight ), 'error' );
}
break; // we stop the loop
}
return $rates;
}
答案 0 :(得分:1)
更新2:您的代码中存在许多错误和错误......
您的代码中存在丢失的运输方法,因为您只有2:
'shipping_by_rules:16'
==&gt;标准配送(0-10kg)'shipping_by_rules:15'
==&gt;第二天发货但标准配送(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文件。
它应该有用。