【问题标题】:Hide and unhide a specific shipping methods based on shipping class in WooCommerce根据 WooCommerce 中的运输类别隐藏和取消隐藏特定的运输方式
【发布时间】:2018-07-10 04:33:58
【问题描述】:

我有一个购物车,它有三种方法标准交付(0-10 公斤)、标准交付(11-20 公斤)和次日交付(0-20 公斤),我的问题是当我的产品具有冷冻食品运输类别时购物车运输方式应该只是第二天交货,如果现在购物车上有运输类别,它只有标准交货,我的问题是当添加具有运输类别的产品和没有运输类别的产品时,它不会转到我提出的条件。

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;
}

【问题讨论】:

  • 很抱歉第三个代码是这个 else if($product->get_shipping_class_id() == NULL || $product->get_shipping_class_id() == 149)
  • 这第三种情况永远不会奏效......因为你正在服用第一种和第二种
  • 因为当我添加一个有运输等级但没有运输等级的项目时,它不会进入第三个条件
  • 我该怎么办?请帮忙
  • 如果我的购物车中有一个有运输等级但没有运输的产品,我应该设置什么条件,这将导致第二天交货?

标签: php wordpress woocommerce cart shipping


【解决方案1】:

更新2:你的代码有很多错误和错误……

您的代码中缺少一种运输方式,因为您只有 2 个:

  • 'shipping_by_rules:16' ==> 标准配送(0-10kg)
  • 'shipping_by_rules: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;
}

代码进入活动子主题(或活动主题)的 function.php 文件中。

它应该可以工作。

【讨论】:

  • 非常感谢,回复感谢,代码完美运行
  • 为什么如果我删除此代码 wc_add_notice( sprintf( __( '2 | %s', 'woocommerce' ), $weight ), 'error' );它不起作用我可以删除此代码吗?
  • 因为我刚刚添加了它,所以我可以看到它发生了什么情况,我怎样才能删除该代码但仍然使它工作?
  • @MarvynSue 谢谢……您甚至可以获取购物车的总重量并就运输方式进行互动……考虑一下您的两种标准交付方式(0-10kg)和(11-20kg)……
猜你喜欢
  • 2014-07-05
  • 2019-10-17
  • 2020-08-25
  • 1970-01-01
  • 2019-09-04
  • 2021-02-04
  • 2019-08-10
相关资源
最近更新 更多