admin管理员组文章数量:1302401
I am looking forward on how to hide shipping at all from the checkout page of Woocommerce when only certain product (magazine) is in the cart or much better would be, if the only product/s with certain shipping class are in the cart.
If there is a combination of products (magazine + books), then shipping module should be visible.
This is what I have tried, but this always returns false. Can you help me out on this?
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;
}
I am looking forward on how to hide shipping at all from the checkout page of Woocommerce when only certain product (magazine) is in the cart or much better would be, if the only product/s with certain shipping class are in the cart.
If there is a combination of products (magazine + books), then shipping module should be visible.
This is what I have tried, but this always returns false. Can you help me out on this?
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;
}
Share
Improve this question
asked Mar 12, 2021 at 7:35
jamjam
1732 gold badges3 silver badges15 bronze badges
1 Answer
Reset to default 0Solved it. Just remeber to add the same shipping class to all variations too.
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;
}
}
本文标签:
版权声明:本文标题:filters - Woocommerce - Hide shipping at all, if only certain product or products with certain shipping class is in the cart 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741681956a2392223.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论