在 WooCommerce 中隐藏和取消隐藏基于运输 class 的特定运输方式
Hide and unhide a specific shipping methods based on shipping class in WooCommerce
我的手推车有标准配送(0-10kg)、标准配送(11-20kg)和次日配送(0-20kg)三种方法,我的问题是当我的产品有冷冻食品运输时class 到购物车,送货方式只能是次日送达,如果现在有送货 class 在购物车上,它只有标准送货,我的问题是当添加带送货的产品时 class 和没有运费的产品 class 它不会达到我制作的状态。
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;
}
更新2:你的代码有很多错误和失误…
THERE IS A MISSING SHIPPING METHOD IN YOUR CODE as you only have 2:
'shipping_by_rules:16'
==> Standard delivery(0-10kg)
'shipping_by_rules:15'
==> Next day delivery
But what about standard 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 文件。
应该可以。
我的手推车有标准配送(0-10kg)、标准配送(11-20kg)和次日配送(0-20kg)三种方法,我的问题是当我的产品有冷冻食品运输时class 到购物车,送货方式只能是次日送达,如果现在有送货 class 在购物车上,它只有标准送货,我的问题是当添加带送货的产品时 class 和没有运费的产品 class 它不会达到我制作的状态。
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;
}
更新2:你的代码有很多错误和失误…
THERE IS A MISSING SHIPPING METHOD IN YOUR CODE as you only have 2:
'shipping_by_rules:16'
==> Standard delivery(0-10kg)'shipping_by_rules:15'
==> Next day deliveryBut what about standard 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 文件。
应该可以。