Skip to content

Instantly share code, notes, and snippets.

@kloon
Last active September 20, 2018 10:49
Show Gist options
  • Save kloon/7463458 to your computer and use it in GitHub Desktop.
Save kloon/7463458 to your computer and use it in GitHub Desktop.
WooCommerce hide free shipping based on total cart weight
<?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;
}
?>
@sree541
Copy link

sree541 commented Mar 27, 2017

This is not working for me. I need to add free shipping based on cart amount.

@kumaxim
Copy link

kumaxim commented Oct 27, 2017

The filter 'woocommerce_available_shipping_methods' does not exist in official Woocommerce Hook Reference https://docs.woocommerce.com/wc-apidocs/hook-docs.html

@Ciantic
Copy link

Ciantic commented Sep 20, 2018

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