Guest User

Sepreate Free Shipping into it's own "box"

a guest
Aug 30th, 2016
192
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. add_filter( 'woocommerce_cart_shipping_packages', 'bulky_woocommerce_cart_shipping_packages' );
  2.  
  3. function bulky_woocommerce_cart_shipping_packages( $packages ) {
  4.     // Reset the packages
  5.     $packages = array();
  6.  
  7.     // Bulky items
  8.     $bulky_items   = array();
  9.     $regular_items = array();
  10.    
  11.     // Sort bulky from regular
  12.     foreach ( WC()->cart->get_cart() as $item ) {
  13.         if ( $item['data']->needs_shipping() ) {
  14.             if ( $item['data']->get_shipping_class() == 'free-shipping' ) {
  15.                 $bulky_items[] = $item;
  16.             } else {
  17.                 $regular_items[] = $item;
  18.             }
  19.         }
  20.     }
  21.    
  22.     // Put inside packages
  23.     if ( $bulky_items ) {
  24.         $packages[] = array(
  25.             'ship_via'        => array( 'flat_rate' ),
  26.             'contents'        => $bulky_items,
  27.             'contents_cost'   => array_sum( wp_list_pluck( $bulky_items, 'line_total' ) ),
  28.             'applied_coupons' => WC()->cart->applied_coupons,
  29.             'destination'     => array(
  30.                 'country'   => WC()->customer->get_shipping_country(),
  31.                 'state'     => WC()->customer->get_shipping_state(),
  32.                 'postcode'  => WC()->customer->get_shipping_postcode(),
  33.                 'city'      => WC()->customer->get_shipping_city(),
  34.                 'address'   => WC()->customer->get_shipping_address(),
  35.                 'address_2' => WC()->customer->get_shipping_address_2()
  36.             )
  37.         );
  38.     }
  39.     if ( $regular_items ) {
  40.         $packages[] = array(
  41.             'ship_via'        => array( 'ups' ),
  42.             'contents'        => $regular_items,
  43.             'contents_cost'   => array_sum( wp_list_pluck( $regular_items, 'line_total' ) ),
  44.             'applied_coupons' => WC()->cart->applied_coupons,
  45.             'destination'     => array(
  46.                 'country'   => WC()->customer->get_shipping_country(),
  47.                 'state'     => WC()->customer->get_shipping_state(),
  48.                 'postcode'  => WC()->customer->get_shipping_postcode(),
  49.                 'city'      => WC()->customer->get_shipping_city(),
  50.                 'address'   => WC()->customer->get_shipping_address(),
  51.                 'address_2' => WC()->customer->get_shipping_address_2()
  52.             )
  53.         );
  54.     }    
  55.    
  56.     return $packages;
  57. }
RAW Paste Data