根据WooCommerce中的运输类别隐藏和取消隐藏特定的运输方法 [英] Hide and unhide a specific shipping methods based on shipping class in WooCommerce

查看:127
本文介绍了根据WooCommerce中的运输类别隐藏和取消隐藏特定的运输方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个推车,该推车具有标准交付方式(0-10kg),标准交付方式(11-20kg)和第二天交付方式(0-20kg)的三种方法,我的问题是当ia产品具有冷冻食品运输类时,购物车的运送方式应该仅是第二天的运送,如果购物车上现在有运送类别,则它只能进行标准运送,我的问题是,当添加具有运送类别的产品和没有运送类别的产品时,它不会

i have a cart that has three method standard delivery(0-10kg), standard delivery(11-20kg) and next day delivery(0-20kg), my problem is when i a product with a frozen food shipping class to the cart the shipping method should only be the next day delivery and if there is now shipping class on the cart it only has the standard delivery, my problem is when a add the product with a shipping class and a products with no shipping class it will not go to the condition that i make.

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: 您的代码中有很多错误和错误……

Updated 2: There are many errors and mistakes in your code…


您的代码中有一种错误的运输方式,因为您只有2:

THERE IS A MISSING SHIPPING METHOD IN YOUR CODE as you only have 2:


  • 'shipping_by_rules:16' ==>标准递送(0-10公斤)

  • ’shipping_by_rules:15’ ==>第二天交付

  • 'shipping_by_rules:16' ==> Standard delivery(0-10kg)
  • 'shipping_by_rules:15' ==> Next day 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文件中

应该可以。

这篇关于根据WooCommerce中的运输类别隐藏和取消隐藏特定的运输方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆