首页 > 解决方案 > 在 woocoomerce 中添加更多订单运输项目

问题描述

购物车中的每件商品都有自己的运输包裹,但我计划将具有快递类别的商品放在单独的包裹中。我已经设法使用woocommerce_cart_shipping_packages钩子做到这一点,如下所示:

function custom_split_shipping_packages_shipping_class( $packages ) {

// Reset all packages
$packages            = array();
$cb_item_product_ids = array();
$frozen_package_items = array();
$aggregate_frozen_package_name = array();

foreach ( WC()->cart->get_cart() as $item_key => $item ) {

//hide delivery time picker for coupons
$isCouponProduct = has_term( array( 'coupon' ), 'product_cat', $item['product_id'] );

if ( $item['data']->needs_shipping() && ! $isCouponProduct ) {
    $package_category_result = get_fulfillment_package_category( $item['product_id'] ); //get category_slug from fulfillment_categories
    $is_frozen =  strpos( $package_category_result['slug'], 'frozen' ) !== false;

    if($is_frozen) {
        $frozen_package_items[ $item_key ] = $item;

        $package_content_result = cb_get_package_content_details( $item['variation'], $item['product_id'] );
        $package_name =  current( $package_content_result )['package_name'];
        $aggregate_frozen_package_name[] = "{$package_name} × {$item['quantity']} ";
    }
    else {
        $array_name = "split_package_items_".$item['product_id'];
        ${$array_name}[ $item_key ] = $item;

        if ( !in_array( $item['product_id'], $cb_item_product_ids ) ) {
            $cb_item_product_ids[] = $item['product_id'];
        }
    }
}
    }

// Create shipping packages
if ( ! empty( $frozen_package_items ) ) {
    $packages[] = array(
        'contents'        => $frozen_package_items,
        'aggregate_package_name' =>  implode( ", ", $aggregate_frozen_package_name ),
        'contents_cost'   => array_sum( wp_list_pluck( $frozen_package_items, 'line_total' ) ),
        'applied_coupons' => WC()->cart->get_applied_coupons(),
        'user'            => array(
            'ID' => get_current_user_id(),
        ),
        'destination'    => array(
            'country'    => WC()->customer->get_shipping_country(),
            'state'      => WC()->customer->get_shipping_state(),
            'postcode'   => WC()->customer->get_shipping_postcode(),
            'city'       => WC()->customer->get_shipping_city(),
            'address'    => WC()->customer->get_shipping_address(),
            'address_2'  => WC()->customer->get_shipping_address_2()
        )
    );
}

foreach($cb_item_product_ids as $key => $product_id ) {
$array_name = "split_package_items_".$product_id;

$packages[] = array(
  'contents'        => $$array_name,
  'contents_cost'   => array_sum( wp_list_pluck( $$array_name, 'line_total' ) ),
  'applied_coupons' => WC()->cart->get_applied_coupons(),
  'user'            => array(
     'ID' => get_current_user_id(),
  ),
  'destination'    => array(
    'country'    => WC()->customer->get_shipping_country(),
    'state'      => WC()->customer->get_shipping_state(),
    'postcode'   => WC()->customer->get_shipping_postcode(),
    'city'       => WC()->customer->get_shipping_city(),
    'address'    => WC()->customer->get_shipping_address(),
    'address_2'  => WC()->customer->get_shipping_address_2()
  )
);

}

return $packages;
}
add_filter( 'woocommerce_cart_shipping_packages',  'custom_split_shipping_packages_shipping_class' );

下订单时,订单运输项目记录的数量等于运输包裹的数量,但我希望这些等于运输项目的数量。

如何创建额外的运输项目?我认为有一个钩子woocommerce_checkout_create_order_shipping_item但我可以找到一个很好的示例来了解如何使用它。

在此处输入图像描述

标签: phpwordpresswoocommercehook-woocommerce

解决方案


推荐阅读