Skip to content

Instantly share code, notes, and snippets.

@WooForce
Last active April 30, 2021 09:38
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save WooForce/d61409f64bf3d0a55458c9cdad2a5eca to your computer and use it in GitHub Desktop.
Save WooForce/d61409f64bf3d0a55458c9cdad2a5eca to your computer and use it in GitHub Desktop.
Split cart into multiple packages based on shipping classes
add_filter( 'woocommerce_cart_shipping_packages', 'wf_split_cart_by_shipping_class_group' );
function wf_split_cart_by_shipping_class_group($packages){
//Reset packages
$packages = array();
//Init splitted package
$splitted_packages = array();
// Group of shipping class ids
$class_groups = array(
'group1' => array('class-a'),
'group2' => array('class-b'),
// 'group3' => array(11,15,17),
);
foreach ( WC()->cart->get_cart() as $item_key => $item ) {
if ( $item['data']->needs_shipping() ) {
$belongs_to_class_group = 'none';
$item_ship_class_id = $item['data']->get_shipping_class();
if($item_ship_class_id){
foreach($class_groups as $class_group_key => $class_group){
if(in_array($item_ship_class_id, $class_group)){
$belongs_to_class_group = $class_group_key;
continue;
}
}
}
$splitted_packages[$belongs_to_class_group][$item_key] = $item;
}
}
// Add grouped items as packages
if(is_array($splitted_packages)){
foreach($splitted_packages as $splitted_package_items){
$packages[] = array(
'contents' => $splitted_package_items,
'contents_cost' => array_sum( wp_list_pluck( $splitted_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()
)
);
}
}
return $packages;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment