php - 在 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但我可以找到一个很好的示例来了解如何使用它。
解决方案
推荐阅读
- javascript - 在文件上传器 Javascript 上检测损坏的文件
- python-3.x - 是否可以在 Python (Spyder) 中更新内联图?
- java - 使用自定义比较器会导致排序不一致
- android - Main Activity toolbar not showing after hiding when attaching fragment
- echarts - 如何使用 Echarts 在散点图上绘制圆形图层
- hibernate - 如何选择部分实体,然后使用实体更新数据库中的实体?
- java - Android 应用程序延迟执行不起作用
- django - Django 使用 AJAX 加载更多评论
- sql - 检查查询中的多个空值
- vue.js - 使用 vuex-map-fields 映射计算域