Last active
December 2, 2019 06:24
-
-
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/
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
/** | |
* 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