【发布时间】:2018-07-10 04:33:58
【问题描述】:
我有一个购物车,它有三种方法标准交付(0-10 公斤)、标准交付(11-20 公斤)和次日交付(0-20 公斤),我的问题是当我的产品具有冷冻食品运输类别时购物车运输方式应该只是第二天交货,如果现在购物车上有运输类别,它只有标准交货,我的问题是当添加具有运输类别的产品和没有运输类别的产品时,它不会转到我提出的条件。
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;
}
【问题讨论】:
-
很抱歉第三个代码是这个 else if($product->get_shipping_class_id() == NULL || $product->get_shipping_class_id() == 149)
-
这第三种情况永远不会奏效......因为你正在服用第一种和第二种
-
因为当我添加一个有运输等级但没有运输等级的项目时,它不会进入第三个条件
-
我该怎么办?请帮忙
-
如果我的购物车中有一个有运输等级但没有运输的产品,我应该设置什么条件,这将导致第二天交货?
标签: php wordpress woocommerce cart shipping