Skip to content

Instantly share code, notes, and snippets.

@WooForce
Last active May 31, 2016 11:27
Show Gist options
  • Save WooForce/321b1a8a567b8d3feef48d0aab8961a8 to your computer and use it in GitHub Desktop.
Save WooForce/321b1a8a567b8d3feef48d0aab8961a8 to your computer and use it in GitHub Desktop.
Show/Hide shipping methods based on order weight
add_filter( 'woocommerce_package_rates', 'show_shipping_method_on_order_weight', 10, 2 );
function show_shipping_method_on_order_weight( $available_shipping_methods, $package ) {
$order_weight = 0;
foreach( WC()->cart->cart_contents as $key => $values ) {
$product_weight = woocommerce_get_weight($values[ 'data' ]->get_weight(),'lbs');
$quantity = $values['quantity'];
if($product_weight && $quantity){
$order_weight = $order_weight + $product_weight*$quantity;
}
}
$weight_oz = $order_weight*16;
if($weight_oz <= 6){
$shipping_services_to_hide = array(
'wf_shipping_usps:flat_rate_box_priority',
'wf_shipping_usps:flat_rate_box_express',
'wf_shipping_usps:D_FIRST_CLASS',
'wf_shipping_usps:D_EXPRESS_MAIL',
'wf_shipping_usps:D_STANDARD_POST',
'wf_shipping_usps:D_MEDIA_MAIL',
'wf_shipping_usps:D_LIBRARY_MAIL',
'wf_shipping_usps:D_PRIORITY_MAIL',
'wf_shipping_usps:I_EXPRESS_MAIL',
'wf_shipping_usps:I_PRIORITY_MAIL',
'wf_shipping_usps:I_GLOBAL_EXPRESS',
'wf_shipping_usps:I_FIRST_CLASS',
'wf_shipping_usps:I_POSTCARDS',
);
}else{
$shipping_services_to_hide = array('flat_rate');
}
foreach ( $shipping_services_to_hide as &$value ) {
unset( $available_shipping_methods[$value] );
}
return $available_shipping_methods;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment