我设法做到了这一点,但是我在两件事上苦苦挣扎;获得免费送货产品的选项,以实际获得免费送货,并为每个送货选项添加标题。现在它们分别称为“运输和运输2”。
想法是这样的:
创建两种运输类别,一种免费,另一种非免费。将产品分配给这些类别。将类别分配给flat_rate
运送选项,并给non-free
类别支付费用。然后将这些类应用于产品。
在购物车和结帐栏中,根据这些类别,装运部分将分为两部分。但是..
如果实现了免费送货(WooCommerce设置为基于最低订单成本的免费送货),则该送货仍会提供统一费率,免费送货和本地取件,而另一项则根据flat_rate
费用进行收费。 / p>
有人可以帮我解决这个问题吗?如果达到免费送货,则送货选项应仅显示免费送货和本地取件,而不是固定费用。
这是我正在使用的代码。
add_filter( 'woocommerce_cart_shipping_packages', 'wc_shipping_classes_settings' );
function wc_shipping_classes_settings( $packages ) {
$packages = array();
$non_free_shipping_items = array();
$regular_items = array();
foreach ( WC()->cart->get_cart() as $item ) {
if ( $item['data']->needs_shipping() ) {
if ( $item['data']->get_shipping_class() == 'free' ) {
$non_free_shipping_items[] = $item;
} else {
$regular_items[] = $item;
}}}
if ( $non_free_shipping_items ) {
$packages[] = array(
'ship_via' => array( 'flat_rate' ),
'contents' => $non_free_shipping_items,
'contents_cost' => array_sum( wp_list_pluck( $non_free_shipping_items, 'line_total' ) ),
'applied_coupons' => WC()->cart->applied_coupons,
'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()
));
}
if ( $regular_items ) {
$packages[] = array(
'contents' => $regular_items,
'contents_cost' => array_sum( wp_list_pluck( $regular_items, 'line_total' ) ),
'applied_coupons' => WC()->cart->applied_coupons,
'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;
}