Created
July 27, 2017 13:51
-
-
Save dianewallace/26a14049804282dca529223a75e0f85b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
add_filter( 'woocommerce_cart_shipping_packages', 'split_shipping_packages_shipping_class' ); | |
function split_shipping_packages_shipping_class( $packages ) { | |
// Reset the packages. | |
$packages = array(); | |
// Special items. | |
$special_items = array(); | |
$regular_items = array(); | |
// Sort free from regular | |
foreach ( WC()->cart->get_cart() as $item ) { | |
if ( $item['data']->needs_shipping() ) { | |
if ( $item['data']->get_shipping_class() === 'free' ) { | |
$special_items[] = $item; | |
} else { | |
$regular_items[] = $item; | |
} | |
} | |
} | |
// Put inside packages | |
if ( $special_items ) { | |
$packages[] = array( | |
'ship_via' => array( 'free_shipping' ), | |
'contents' => $special_items, | |
'name' => 'Free Shipping is available on these items', | |
'contents_cost' => array_sum( wp_list_pluck( $special_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, | |
'name' => 'Delivery Options', | |
'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(), | |
), | |
); | |
} | |
$regular_items_total = $packages[1]['contents_cost']; | |
if ( $regular_items_total >= 25 ) { | |
$packages[1]['ship_via'] = array( 'free_shipping' ); | |
} else { | |
$packages[1]['ship_via'] = array( 'flat_rate' ); | |
} | |
if ( ! $special_items ) { | |
unset( $packages[1] ); | |
$regular_items_total = $packages[0]['contents_cost']; | |
if ( $regular_items_total >= 25 ) { | |
$packages[0]['ship_via'] = array( 'free_shipping' ); | |
} else { | |
$packages[0]['ship_via'] = array( 'flat_rate' ); | |
} | |
} | |
if ( ! $regular_items_total ) { | |
unset( $packages[1] ); | |
} | |
return $packages; | |
} | |
// Rename new packages. | |
function rename_custom_shipping_package( $package_name, $i, $package ) { | |
if ( ! empty( $package['name'] ) ) { | |
$package_name = $package['name']; | |
} | |
return $package_name; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment