我期待着在购物车中只有特定产品(杂志)的情况下,如何在Woocommerce的结账页面中隐藏发货信息,或者如果购物车中只有特定发货类别的产品,则更好。
如果存在产品组合(杂志+书籍),则应显示装运模块。
这是我尝试过的,但它总是返回false。你能帮我解决这个问题吗?
add_filter( \'woocommerce_package_rates\', \'woocommerce_cart_shipping_total_filter_callback\', 11, 2 );
function woocommerce_cart_shipping_total_filter_callback( $show_abon_shipping) {
$product_id = 27733;
$in_cart = false;
foreach( WC()->cart->get_cart() as $cart_item ) {
$product_in_cart = $cart_item[\'product_id\'];
if ( $product_in_cart === $product_id && count($product_in_cart) == 1) {
$in_cart = true;
}
}
if ( $in_cart ) {
return false;
}
return $show_abon_shipping;
}
最合适的回答,由SO网友:jam 整理而成
解决了它。只需记住在所有变体中添加相同的装运类别即可。
add_filter( \'woocommerce_package_rates\', \'custom_shipping_rates\', 100, 2 );
function custom_shipping_rates( $rates, $package ) {
$shipping_class = 506; // HERE set the shipping class ID
$found = false;
$productsincart = count($package[\'contents\']);
foreach( $package[\'contents\'] as $cart_item ) {
if ($productsincart == 1 && $cart_item[\'data\'];->get_shipping_class_id() == $shipping_class){
$found = true;
}
}
if ( $found ) {
return false; // If not found we exit
}else {
return $rates;
}
}