Skip to content

Instantly share code, notes, and snippets.

@strangerstudios
Last active April 20, 2020 11:12
Show Gist options
  • Save strangerstudios/03cad36119165950097f to your computer and use it in GitHub Desktop.
Save strangerstudios/03cad36119165950097f to your computer and use it in GitHub Desktop.
Give members of specific level IDs Free Shipping in your WooCommerce store.
/*
* Give members of level 1 or 2 Free Shipping when free shipping is available (must set free shipping as an option in store settings).
* Change $pmprowc_free_shipping_levels = array(1,2); to include level IDs that receive free shipping
*/
function my_pmprowc_free_shipping( $rates, $package ) {
$pmprowc_free_shipping_levels = array('1','2');
if(function_exists('pmpro_hasMembershipLevel') && isset( $rates['free_shipping'] ) && pmpro_hasMembershipLevel($pmprowc_free_shipping_levels) )
{
// Get Free Shipping array into a new array
$freeshipping = array();
$freeshipping = $rates['free_shipping'];
// Empty the $available_methods array
unset( $rates );
// Add Free Shipping back into $avaialble_methods
$rates = array();
$rates[] = $freeshipping;
}
else
{
// remove free shipping option
unset( $rates['free_shipping'] );
}
return $rates;
}
add_filter( 'woocommerce_package_rates', 'my_pmprowc_free_shipping' , 10, 2 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment