-
-
Save arelthia/d856f6954e45a1bfd6b8 to your computer and use it in GitHub Desktop.
WooCommerce conditional shipping methods based on cart contents weight
This file contains 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
/** | |
* Shipping based on cart contents weight | |
*/ | |
add_filter( 'woocommerce_cart_shipping_packages', 'ps_woocommerce_cart_shipping_packages_by_weight' ); | |
function ps_woocommerce_cart_shipping_packages_by_weight( $packages ) { | |
$packages = array(); | |
$cart_items = WC()->cart->get_cart(); | |
if( WC()->cart->cart_contents_weight > 4){ | |
$packages[] = array( | |
'ship_via' => array( 'ups' ), /*Shipping methods that are available for order over 4 lbs */ | |
'contents' => $cart_items, | |
'contents_cost' => array_sum( wp_list_pluck( $cart_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() | |
) | |
); | |
}else{ | |
$packages[] = array( | |
'ship_via' => array('usps'), /*Shipping methods that are available for order under 4 lbs */ | |
'contents' => $cart_items, | |
'contents_cost' => array_sum( wp_list_pluck( $cart_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; | |
} |
This file contains 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
/** | |
* Shipping based on cart contents weight and shipping class. Allow for multiple shipping | |
* types in one cart. | |
*/ | |
add_filter( 'woocommerce_cart_shipping_packages', 'ps_woocommerce_cart_shipping_packages_by_weight' ); | |
function ps_woocommerce_cart_shipping_packages_by_weight( $packages ) { | |
$packages = array(); | |
$free_items = array(); | |
$regular_items = array(); | |
// Sort rack from regular | |
foreach ($regular_items WC()->cart->get_cart() as $item ) { | |
if ( $item['data']->needs_shipping() ) { | |
if ( $item['data']->get_shipping_class() == 'free-shipping' ) { | |
$free_items[] = $item; | |
} else { | |
$regular_items[] = $item; | |
} | |
} | |
} | |
if ( $free_items ) { | |
$packages[] = array( | |
'ship_via' => array( 'free_shipping' ), | |
'contents' => $free_items, | |
'contents_cost' => array_sum( wp_list_pluck( $free_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 && WC()->cart->cart_contents_weight > 4){ | |
$packages[] = array( | |
'ship_via' => array( 'ups' ), /*Shipping methods that are available for order over 4 lbs */ | |
'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() | |
) | |
); | |
}elseif ($regular_items){ | |
$packages[] = array( | |
'ship_via' => array('usps'), /*Shipping methods that are available for order under 4 lbs */ | |
'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