Last active
September 20, 2018 10:49
-
-
Save kloon/7463458 to your computer and use it in GitHub Desktop.
WooCommerce hide free shipping based on total cart weight
This file contains hidden or 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
<?php | |
add_filter( 'woocommerce_available_shipping_methods', 'wc_hide_free_shipping_based_on_weight' , 10, 1 ); | |
function wc_hide_free_shipping_based_on_weight( $available_methods ) { | |
if ( isset( $available_methods['free_shipping'] ) { | |
global $woocommerce; | |
$weight = 0; | |
if ( sizeof( $woocommerce->cart->get_cart() ) > 0 ) { | |
foreach ( $woocommerce->cart->get_cart() as $item_id => $values ) { | |
$_product = $values['data']; | |
if ( $_product->exists() && $values['quantity'] > 0 ) { | |
if ( ! $_product->is_virtual() ) { | |
$weight += $_product->get_weight() * $values['quantity']; | |
} | |
} | |
} | |
if ( $weight > 100 ) | |
unset( $available_methods['free_shipping'] ); | |
} | |
} | |
return $available_methods; | |
} | |
?> |
The filter 'woocommerce_available_shipping_methods' does not exist in official Woocommerce Hook Reference https://docs.woocommerce.com/wc-apidocs/hook-docs.html
It was deprecated, new hook is woocommerce_package_rates
and needs another kind of code.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is not working for me. I need to add free shipping based on cart amount.