Skip to content

Instantly share code, notes, and snippets.

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 MaryOJob/5af53e8050eb2b55f40d91cd72428896 to your computer and use it in GitHub Desktop.
Save MaryOJob/5af53e8050eb2b55f40d91cd72428896 to your computer and use it in GitHub Desktop.
Give members of specific level IDs Free Shipping on orders above $75 in your WooCommerce store.
<?php // Do not copy this line
/**
* Add the following code to your PMPro Customizations Plugin - https://www.paidmembershipspro.com/create-a-plugin-for-pmpro-customizations/
* 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 ) {
$free_shipping_allowed = false;
$pmprowc_free_shipping_levels = array( '1', '2' );
$free_shipping_key = "";
foreach( $rates as $key => $val ){
if( strpos( $key, 'free_shipping' ) !== false ){
$free_shipping_key = $key;
}
}
$minimum_order_amount = 75;
$cart_total = WC()->cart->get_total();
if ( $cart_total > $minimum_order_amount ) {
$free_shipping_allowed = true;
}
if(function_exists('pmpro_hasMembershipLevel') && isset( $rates[$free_shipping_key] ) && pmpro_hasMembershipLevel($pmprowc_free_shipping_levels) && free_shipping_allowed )
{
// Get Free Shipping array into a new array
$freeshipping = array();
$freeshipping = $rates[$free_shipping_key];
// 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_key] );
}
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