Php 根据WooCommerce中的shipping类隐藏和取消隐藏特定的shipping方法

Php 根据WooCommerce中的shipping类隐藏和取消隐藏特定的shipping方法,php,wordpress,woocommerce,cart,shipping,Php,Wordpress,Woocommerce,Cart,Shipping,我的购物车有三种方式:标准配送(0-10kg)、标准配送(11-20kg)和次日配送(0-20kg),我的问题是,当我将冷冻食品配送类别的产品运送到购物车时,配送方式应该是次日配送,如果购物车上现在有配送类别,则只有标准配送,我的问题是,当一个添加了装运类别的产品和一个没有装运类别的产品时,它将不符合我所做的条件 add_filter( 'woocommerce_package_rates', 'custom_hide_shipping_methods', 10, 2 ); function c

我的购物车有三种方式:标准配送(0-10kg)、标准配送(11-20kg)和次日配送(0-20kg),我的问题是,当我将冷冻食品配送类别的产品运送到购物车时,配送方式应该是次日配送,如果购物车上现在有配送类别,则只有标准配送,我的问题是,当一个添加了装运类别的产品和一个没有装运类别的产品时,它将不符合我所做的条件

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;
}
add_filter('woocommerce_package_rates','custom_hide_shipping_methods',10,2);
函数自定义\隐藏\装运\方法($rates,$package){
foreach(WC()->cart->get_cart()作为$cart_项目){
$product=$cart_项目['data'];//WC_产品对象
如果($product->get\u shipping\u class\u id()=149){//get\u shipping\u class\u id()=NULL){
未结算($rates['shipping_by_rules:15']);//第二天交货
wc添加通知(sprintf('2','woocommerce'),$weight),'error');
}如果($product->get|shipping_class_id()!=||$product->get|shipping_class_id()==149){
未结算($rates['shipping_by_rules:16']);//标准交货
wc_add_通知(sprintf('3','woocommerce'),$weight),'error');
}
break;//我们停止循环
}
返回美元汇率;
}

更新2:您的代码中有许多错误和错误

您的代码中缺少装运方法,因为您只有2个:

  • “按规则发货:16”
    ==>标准交货(0-10kg)
  • “按规则发货:15”
    ==>次日发货
但是标准交付(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;
}
add_filter('woocommerce_package_rates','custom_hide_shipping_methods',20,2);
函数自定义\隐藏\装运\方法($rates,$package){
$targeted\u class\u id=149;//cart->get\u cart()作为$cart\u项目){
$shipping_class=$cart_item['data']->get_shipping_class_id();
#$weight=$cart_item['data']->get_weight();
如果($shipping\u class==$targeted\u class\u id)
$has_class=true;
elseif(空($shipping_class))
$has\u no\u class=true;
}
//不确定的运输方式
如果($has_类)
{//案例1和3
未结算($rates['shipping_by_rules:16']);//标准交货
#wc|u添加_通知(sprintf(|%s,'woocommerce'),$weight,'error');
}
其他(!$has_class&$has_no_class)
{//案例2
未结算($rates['shipping_by_rules:15']);//第二天交货
#wc|u添加_通知(sprintf(|uuuu('2|%s','woocmerce'),$weight),'error');
}
elseif($has_class&&$has_no_class)/=>可选(您可能不需要它)
{//案例3
未结算($rates['shipping_by_rules:16']);//标准交货
#wc|u添加_通知(sprintf(| 3 |%s,'woocommerce'),$weight,'error');
}
返回美元汇率;
}
代码进入活动子主题(或活动主题)的function.php文件


它应该可以工作。

很抱歉,第三个屏幕上的代码是else if($product->get|u shipping_class_id()==NULL | |$product->get|u shipping_class_id()==149)这第三个条件永远不会起作用…因为当我添加一个具有装运类别且没有装运类别的商品时,您将接受第一个和第二个原因,它不会进入第三个条件我该怎么办?请帮助如果我的购物车中有一个具有装运类别且没有装运的商品,那么我应该设置什么条件,这将在第二天交货时不受欢迎?t非常感谢,回复感谢,代码运行完美为什么我要删除此代码wc|u add_通知(sprintf('2|%s','woocmerce'),$weight),'error'));它不起作用。我可以删除这个代码吗?因为我刚刚添加了这个代码,这样我就可以看到它发生了什么情况,我如何删除这个代码,但仍然可以工作?@MarvynSue谢谢……你甚至可以得到购物车的总重量,并在运输方式上进行交互……想想你的两种标准配送方式(0-10kg)和(11-20kg)…