Skip to content

Instantly share code, notes, and snippets.

@WPprodigy
Created October 27, 2015 08:01
Show Gist options
  • Save WPprodigy/75203edae00875d1fcd9 to your computer and use it in GitHub Desktop.
Save WPprodigy/75203edae00875d1fcd9 to your computer and use it in GitHub Desktop.
Conditionally toggle Free Shipping based on the user's role and contents of the cart
function wc_ninja_free_shipping_for_a_user_role( $rates, $package ) {
// Only modify rates if free_shipping is present
if ( isset( $rates['free_shipping'] ) ) {
// Add a user role specific capability here
if ( ! current_user_can( 'manage_woocommerce' ) ) {
// Cart Subtotal
$subtotal = WC()->cart->subtotal;
// Number of products in the cart
$count = WC()->cart->cart_contents_count;
// Add some optional conditions here using $subtotal and $count
if ( $count >= 50 || $subtotal >= 150) {
unset( $rates['free_shipping'] );
}
}
}
return $rates;
}
add_filter( 'woocommerce_package_rates', 'wc_ninja_free_shipping_for_a_user_role', 10, 2 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment