Forked from strangerstudios/my_pmprowc_free_shipping.php
Last active
January 18, 2023 13:49
-
-
Save JarrydLong/234adc37f7f5a3e50831698d4b7d40fd to your computer and use it in GitHub Desktop.
Give members of specific level IDs Free Shipping in your WooCommerce store.
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
/* | |
* 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' ); | |
$free_shipping_key = ""; | |
foreach( $rates as $key => $val ){ | |
if( strpos( $key, 'free_shipping' ) !== false ){ | |
$free_shipping_key = $key; | |
} | |
} | |
if(function_exists('pmpro_hasMembershipLevel') && isset( $rates[$free_shipping_key] ) && pmpro_hasMembershipLevel($pmprowc_free_shipping_levels) ) | |
{ | |
// 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
This recipe is included in the blog post on " Give Members “Free Shipping” at Shop Checkout using Jigoshop or WooCommerce" at Paid Memberships Pro here: https://www.paidmembershipspro.com/give-members-free-shipping-at-shop-checkout/