按 WooCommerce 拆分运输套餐上的商品数量增加运输成本

问题描述 投票:0回答:1

我使用以下代码将项目拆分为单独的 WooCommerce 运输包:

add_filter( 'woocommerce_cart_shipping_packages', 'split_shipping_packages_by_shipping_class' );
 
function split_shipping_packages_by_shipping_class( $packages ) {
       
   $destination = $packages[0]['destination'];  
   $user = $packages[0]['user']; 
   $applied_coupons = $packages[0]['applied_coupons'];
   $packages = array();
    
   foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {    
      $key = $cart_item['data']->get_shipping_class_id();
      $packages[$key]['contents'][$cart_item_key] = $cart_item;
   }
    
   foreach ( $packages as $index => $package ) {
      $total = array_sum( wp_list_pluck( $packages[$index]['contents'], 'line_total' ) );
      $packages[$index]['destination'] = $destination;
      $packages[$index]['user'] = $user;
      $packages[$index]['applied_coupons'] = $applied_coupons;
      $packages[$index]['contents_cost'] = $total;
   }
   return $packages;
}

但是,我也尝试将包含多个数量的物品的包裹拆分为单独的包裹,或将运费成本乘以数量

我尝试过:

add_filter( 'woocommerce_package_rates', 'shipping_cost_based_on_number_of_items', 10, 2 );
function shipping_cost_based_on_number_of_items( $rates, $package ) {
    $numer_of_items = (int) sizeof($package['contents']);

    // Loop through shipping rates
    foreach ( $rates as $rate_key => $rate ){
            $has_taxes = false;

            // Set the new cost
            $rates[$rate_key]->cost = $rate->cost * $numer_of_items;

            // Taxes rate cost (if enabled)
            foreach ($rates[$rate_key]->taxes as $key => $tax){
                if( $tax > 0 ){
                    // New tax calculated cost
                    $taxes[$key] = $tax * $numer_of_items;
                    $has_taxes = true;
                }
            }
            // Set new taxes cost
            if( $has_taxes )
                $rates[$rate_key]->taxes = $taxes;
       
    }
    return $rates;
}

然而,并没有成功。从我的测试来看,它似乎没有正确获取包裹中物品数量的总和,因为当我将

$number_of_items
硬编码为
2
时它起作用了。

是否可以修改上面的代码来实现这一点?

php woocommerce cart shipping-method quantities
1个回答
0
投票

要获取运输包裹内容数量总和,请将最后一个函数替换为:

add_filter( 'woocommerce_package_rates', 'shipping_cost_based_on_number_of_items', 10, 2 );
function shipping_cost_based_on_number_of_items( $rates, $package ) {
    $contents_qty = (int) array_sum( array_column($package['contents'], 'quantity') );

    // Loop through shipping rates
    foreach ( $rates as $rate_key => $rate ){
        if( $rate->cost > 0 ) {
            // Set the new cost
            $rates[$rate_key]->cost = $rate->cost * $contents_qty;

            $has_taxes = false; // Initializing

            // Taxes rate cost (if enabled)
            foreach ($rate->taxes as $key => $tax){
                if( $tax > 0 ){
                    $taxes[$key] = $tax * $contents_qty;
                    $has_taxes = true;
                }
            }
            // Set new taxes cost array
            if( $has_taxes ) {
                $rates[$rate_key]->taxes = $taxes;
            }
        }
    }
    return $rates;
}

现在应该可以了

© www.soinside.com 2019 - 2024. All rights reserved.