我使用以下代码将物品拆分为单独的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;
}

然而,我也试图将包含多个数量物品的包裹拆分为单独的包裹,即OR Multiply the shipping rate cost by the quantity个.

我try 过:

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_items2时,它可以工作.

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

推荐答案

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

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     = array(); // 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;
}

代码保存在您的子主题的functions.php文件中(或插件中).它现在应该起作用了.

Important:您必须清空购物车才能刷新送货方式缓存.

Php相关问答推荐

如何在PHP中将浮点数转换为其IEEE754十六进制字符串?

从产品中获取WooCommerce产品属性单选按钮的列表WP_Query

模型中的Laravel自定义查询字段

Yahoo Finance API文件_GET_CONTENTS 429请求太多

WooCommerce在store 页面上显示兼容多语言的产品属性

使用WordPress wp_mail()的邮箱标题无效

让所有具有相同名称的产品在WooCommerce中更改价格

如何确保所有语言文件都使用Laravel中的新密钥进行更新?

登录后重定向在WooCommerce中购买产品的用户

Woocommerce 仅在前端为登录用户提供价格折扣

用户注册表单中的 HTTP 错误 405

启用 WooCommerce 我的帐户付款方式,允许客户添加付款方式

在 WooCommerce 中应用优惠券时显示购物车商品折扣金额

Laravel 10 中的自定义类未找到

在 WooCommerce 我的帐户自定义部分显示未购买的产品

避免在 WooCommerce 中多次触发挂钩函数

PHP 创建 CSV 文件并用波斯语写入

在 Laravel、Vuejs 和 Inertia 中配置 TypeScript 的问题

Laravel 查询关系是否为 bool

PHP Loop 在迭代中使用修改后的数据