Forked from JarrydLong/my_pmprowc_free_shipping.php
Last active
January 18, 2023 13:51
-
-
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.
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 // 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