Skip to content

Instantly share code, notes, and snippets.

@PluginHive
Last active December 2, 2019 06:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PluginHive/bc669f7d85124a18c457c0d27a00a229 to your computer and use it in GitHub Desktop.
Save PluginHive/bc669f7d85124a18c457c0d27a00a229 to your computer and use it in GitHub Desktop.
Snippet to show shipping method on order weight. PluginHive Plugins : https://www.pluginhive.com/product-category/woocommerce-plugin/
/**
* Snippet to show shipping method on order weight
* Created at : 2 December 2019
* PluginHive Plugins : https://www.pluginhive.com/product-category/woocommerce-plugin/
* Gist Link : https://gist.github.com/PluginHive/bc669f7d85124a18c457c0d27a00a229
**/
add_filter( 'woocommerce_package_rates', 'ph_show_shipping_method_on_order_weight', 10, 2 );
function ph_show_shipping_method_on_order_weight( $available_shipping_methods, $package ) {
$order_weight_limit = 150; // Weight Limit
// Services to hide
$shipping_services_to_hide = array(
'wf_shipping_ups:13',
'wf_shipping_ups:14',
'wf_shipping_ups:01',
'wf_shipping_ups:59',
'wf_shipping_ups:03',
'wf_shipping_ups:02',
'wf_shipping_ups:12',
);
$order_weight = 0;
foreach( WC()->cart->cart_contents as $key => $values ) {
$product_weight = $values[ 'data' ]->get_weight();
$quantity = $values['quantity'];
if($product_weight && $quantity) {
$order_weight = $order_weight + ( $product_weight * $quantity );
}
}
if($order_weight > $order_weight_limit) {
foreach ( $shipping_services_to_hide as &$value ) {
if( isset( $available_shipping_methods[$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