Created
March 19, 2015 09:26
-
-
Save corsonr/81a67e71dca0e9bd18f3 to your computer and use it in GitHub Desktop.
WooCommerce: filter shipping packages
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', 'bulky_woocommerce_cart_shipping_packages' ); | |
| function bulky_woocommerce_cart_shipping_packages( $packages ) { | |
| // Reset the packages | |
| $packages = array(); | |
| // Bulky items | |
| $bulky_items = array(); | |
| $regular_items = array(); | |
| // Sort bulky from regular | |
| foreach ( WC()->cart->get_cart() as $item ) { | |
| if ( $item['data']->needs_shipping() ) { | |
| if ( $item['data']->get_shipping_class() == 'free' ) { | |
| $bulky_items[] = $item; | |
| } else { | |
| $regular_items[] = $item; | |
| } | |
| } | |
| } | |
| // Put inside packages | |
| if ( $bulky_items ) { | |
| $packages[] = array( | |
| 'ship_via' => array( 'flat_rate' ), | |
| 'contents' => $bulky_items, | |
| 'contents_cost' => array_sum( wp_list_pluck( $bulky_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; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment