Skip to content

Instantly share code, notes, and snippets.

@hereswhatidid
Created October 18, 2016 13:48
Show Gist options
  • Save hereswhatidid/a568b41ed8570a0d4c2ac2ddb8514d1b to your computer and use it in GitHub Desktop.
Save hereswhatidid/a568b41ed8570a0d4c2ac2ddb8514d1b to your computer and use it in GitHub Desktop.
Create custom shipping packages and manually set their display names in WooCommerce
<?php
add_filter( 'woocommerce_shipping_package_name', 'rmg_package_names', 10, 3 );
function rmg_package_names( $package_name, $i, $package ) {
if ( ! empty( $package['name'] ) ) {
$package_name = $package['name'];
}
return $package_name;
}
add_filter( 'woocommerce_cart_shipping_packages', 'rmg_split_packages' );
function rmg_split_packages( $packages ) {
$packages = array();
$first_package = array();
$second_package = array();
$counter = 0;
$cart_items = WC()->cart->get_cart();
foreach( $cart_items as $key => $cart_item ) {
if ( $counter < 5 ) {
$first_package[] = $cart_item;
} else {
$second_package[] = $cart_item;
}
$counter++;
}
$packages[] = array(
'name' => __( 'First Method', 'rmg' ), // custom value used to control the display name
'contents' => $first_package,
'contents_cost' => array_sum( wp_list_pluck( $first_package, '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()
)
);
$packages[] = array(
'name' => __( 'Second Method', 'rmg' ), // custom value used to control the display name
'contents' => $second_package,
'contents_cost' => array_sum( wp_list_pluck( $second_package, '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;
}
@cameronmscott
Copy link

Brilliant! Just what I was looking for.

I am also using "ship_via" in the $packages array to force free shipping, but it seems as though array('free_shipping') no longer works.

Any idea on this?

@cameronmscott
Copy link

Nevermind, I got it working!

@john-pereira
Copy link

Can I add a custom attribute in array of destination? I want add this and then use in a field to calculate shipping on it, is this possible?

@Malaiac
Copy link

Malaiac commented Jan 28, 2021

Okay a reup :)

@cameronmscott how did you manage to get it working ?
I'm trying to assign pre selected shipping classes to splitted packages and can't find a way

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment