Woocommerce 按产品重量计算的商业运输成本

Woocommerce 按产品重量计算的商业运输成本,woocommerce,Woocommerce,我需要组织一个系统来计算一个商业项目的运费。下面是一个例子: 如果产品重量小于19公斤,运输成本:36$ 如果产品重量超过19公斤,运输成本:300$ 另外,我需要创建一个额外的航运类(免费航运)。以便 商店可以决定免费运送哪种产品 我是如何解决这个问题的: 首先,在WooCommerce->Settings->Shipping->Shipping zones中,我创建了新的装运区(以色列-按重量装运),在此区域中,我创建了三种不同的装运方法: 19公斤以下订单(统一费率:21) 20公斤

我需要组织一个系统来计算一个商业项目的运费。下面是一个例子:

  • 如果产品重量小于19公斤,运输成本:36$

  • 如果产品重量超过19公斤,运输成本:300$

  • 另外,我需要创建一个额外的航运类(免费航运)。以便 商店可以决定免费运送哪种产品

  • 我是如何解决这个问题的:

    首先,在WooCommerce->Settings->Shipping->Shipping zones中,我创建了新的装运区(以色列-按重量装运),在此区域中,我创建了三种不同的装运方法:

  • 19公斤以下订单(统一费率:21)
  • 20公斤以上订单(统一费率:22)
  • 免费送货(统一费率:24)
  • 然后,我在functions.php文件中放置了这段代码:

    add_filter( 'woocommerce_package_rates', 'custom_tiered_shipping_rates', 9999, 2 );
    
    function custom_tiered_shipping_rates( $rates, $package ) {
    
    if ( WC()->cart->get_cart_contents_weight() < 19 ) {
        if ( isset( $rates['flat_rate:21'] ) ) unset( $rates['flat_rate:22'], $rates['flat_rate:24'] );
    } elseif ( WC()->cart->get_cart_contents_weight() > 20 ) {
        if ( isset( $rates['flat_rate:21'] ) ) unset( $rates['flat_rate:21'], $rates['flat_rate:24'] );
    } else {
        if ( isset( $rates['flat_rate:21'] ) ) unset( $rates['flat_rate:21'], $rates['flat_rate:22'] );
    }
    return $rates;
    }
    
    add_filter('woocommerce_package_rates'、'custom_tiered_shipping_rates',9999,2);
    功能自定义\分层\装运\费率($rates,$package){
    如果(WC()->购物车->获取购物车内容重量()<19){
    如果(isset($rates['flat_rate:21'])未设置($rates['flat_rate:22']),则$rates['flat_rate:24']);
    }其他(WC()->购物车->获取购物车内容重量()>20){
    如果(isset($rates['flat_rate:21'])未设置($rates['flat_rate:21']),则$rates['flat_rate:24']);
    }否则{
    如果(isset($rates['flat_rate:21'])未设置($rates['flat_rate:21']),则$rates['flat_rate:22']);
    }
    返回美元汇率;
    }
    
    我从这里获得的详细描述的源代码:

    一切似乎都正常。只有免费送货方式不起作用。当我为产品提供免费送货服务时。运输成本不是根据此类货物的可用性计算的,而是根据产品的重量计算的。请帮助解决此问题。我知道我把情况弄糊涂了。但是我做的实验越多,我现在就越困惑


    PS:如果购物车中至少有一个商品是付费配送的,那么即使有免费配送的商品,配送成本也应该以付费配送为导向。

    尝试以下操作,如果只有“免费配送”配送类别的商品,则会检查购物车商品。如果是这种情况,并且启用了“免费配送”配送方式,则将设置免费配送方式
    在其他情况下,将采用基于重量的统一费率
    确保在下面的代码中为“免费运输”运输类别设置正确的运输类别slug

    add_filter( 'woocommerce_package_rates', 'custom_tiered_shipping_rates', 9999, 2 );
    function custom_tiered_shipping_rates( $rates, $package ) {
        // HERE below set the correct shipping class slug for "Free shipping" items.
        $free_shipping_class   = 'free-shipping';
        
        $free_shipping_only    = true;
        $non_free_items_weight = 0;
        
        // Check items shipping class for the current shipping package
        foreach( $package['contents'] as $cart_item ) {
            // For non "Free shipping items" flag them and get the calculated weight.
            if( $cart_item['data']->get_shipping_class() !== $free_shipping_class ) {
                $free_shipping_only = false;
                $non_free_items_weight += $cart_item['data']->get_weight() * $cart_item['quantity'];
            }
        }
        
        // Free shipping
        if ( $free_shipping_only && isset($rates['flat_rate:24']) ) {
            unset($rates['flat_rate:21'], $rates['flat_rate:22']);
        }
        // Other rates (Flat rates)
        else {
            if ( $non_free_items_weight < 20 && isset($rates['flat_rate:21']) ) 
                unset( $rates['flat_rate:22'], $rates['flat_rate:24'] );
            } elseif ( $non_free_items_weight >= 20 && isset($rates['flat_rate:22']) ) 
                unset( $rates['flat_rate:21'], $rates['flat_rate:24'] );
            }
        }
        return $rates;
    }
    
    add_filter('woocommerce_package_rates'、'custom_tiered_shipping_rates',9999,2);
    功能自定义\分层\装运\费率($rates,$package){
    //下面为“免费配送”项目设置正确的配送等级slug。
    $free_shipping_class=‘free shipping’;
    $free\u shipping\u only=真;
    $non_free_items_weight=0;
    //检查当前装运包的项目装运类别
    foreach($package['contents']作为$cart\u项目){
    //对于非“免费送货项目”,标记它们并获取计算的重量。
    如果($cart\u item['data']->get\u shipping\u class()!==$free\u shipping\u class){
    $free\u shipping\u only=错误;
    $non_free_items_weight+=$cart_item['data']->get_weight()*$cart_item['quantity'];
    }
    }
    //免费送货
    如果($free_shipping_only&&isset($rates['flat_rate:24'])){
    未结算($rates['flat_rate:21'],$rates['flat_rate:22');
    }
    //其他收费(统一收费)
    否则{
    if($non_free_items_weight<20&&isset($rates['flat_rate:21']))
    未结算($rates['flat_rate:22'],$rates['flat_rate:24']);
    }elseif($non_free_items_weight>=20&&isset($rates['flat_rate:22']))
    未结算($rates['flat_rate:21'],$rates['flat_rate:24');
    }
    }
    返回美元汇率;
    }
    
    代码进入活动子主题(或活动主题)的functions.php文件。测试和工作

    不要忘记清空购物车以刷新配送缓存


    现在,您最好使用WooCommerce的“免费送货”方法(无限制)代替免费送货,并用正确的“免费送货”方法费率ID替换代码中的
    Flat_rate:24


    尝试以下操作,如果只有来自“Free shipping”装运类别的项目,则将检查购物车项目。如果是这种情况,并且启用了“免费配送”配送方式,则将设置免费配送方式
    在其他情况下,将采用基于重量的统一费率
    确保在下面的代码中为“免费运输”运输类别设置正确的运输类别slug

    add_filter( 'woocommerce_package_rates', 'custom_tiered_shipping_rates', 9999, 2 );
    function custom_tiered_shipping_rates( $rates, $package ) {
        // HERE below set the correct shipping class slug for "Free shipping" items.
        $free_shipping_class   = 'free-shipping';
        
        $free_shipping_only    = true;
        $non_free_items_weight = 0;
        
        // Check items shipping class for the current shipping package
        foreach( $package['contents'] as $cart_item ) {
            // For non "Free shipping items" flag them and get the calculated weight.
            if( $cart_item['data']->get_shipping_class() !== $free_shipping_class ) {
                $free_shipping_only = false;
                $non_free_items_weight += $cart_item['data']->get_weight() * $cart_item['quantity'];
            }
        }
        
        // Free shipping
        if ( $free_shipping_only && isset($rates['flat_rate:24']) ) {
            unset($rates['flat_rate:21'], $rates['flat_rate:22']);
        }
        // Other rates (Flat rates)
        else {
            if ( $non_free_items_weight < 20 && isset($rates['flat_rate:21']) ) 
                unset( $rates['flat_rate:22'], $rates['flat_rate:24'] );
            } elseif ( $non_free_items_weight >= 20 && isset($rates['flat_rate:22']) ) 
                unset( $rates['flat_rate:21'], $rates['flat_rate:24'] );
            }
        }
        return $rates;
    }
    
    add_filter('woocommerce_package_rates'、'custom_tiered_shipping_rates',9999,2);
    功能自定义\分层\装运\费率($rates,$package){
    //下面为“免费配送”项目设置正确的配送等级slug。
    $free_shipping_class=‘free shipping’;
    $free\u shipping\u only=真;
    $non_free_items_weight=0;
    //检查当前装运包的项目装运类别
    foreach($package['contents']作为$cart\u项目){
    //对于非“免费送货项目”,标记它们并获取计算的重量。
    如果($cart\u item['data']->get\u shipping\u class()!==$free\u shipping\u class){
    $free\u shipping\u only=错误;
    $non_free_items_weight+=$cart_item['data']->get_weight()*$cart_item['quantity'];
    }
    }
    //免费送货
    如果($free_shipping_only&&isset($rates['flat_rate:24'])){
    未结算($rates['flat_rate:21'],$rates['flat_rate:22');
    }
    //其他收费(统一收费)
    否则{
    if($non_free_items_weight<20&&isset($rates['flat_rate:21']))
    未结算($rates['flat_rate:22'],$rates['flat_rate:24']);
    }elseif($non_free_items_weight>=20&&isset($rates['flat_rate:22']))
    未结算($rates['flat_rate:21'],$rat