Skip to content

Instantly share code, notes, and snippets.

@zstepek
Last active August 29, 2015 14:19
Show Gist options
  • Save zstepek/1b72178e9cd59370d7e2 to your computer and use it in GitHub Desktop.
Save zstepek/1b72178e9cd59370d7e2 to your computer and use it in GitHub Desktop.
Conditionally add free shipping to users based on their user role.
<?php
// Add free shipping for users in subscriber or administrator roles.
function mindsize_wc_conditional_free_shipping( $rates, $package ) {
get_currentuserinfo();
global $current_user;
if ($current_user->ID) {
$user_roles = $current_user->roles;
$user_role = array_shift($user_roles);
if ($user_role == 'subscriber' || $user_role == 'administrator') {
$freeshipping = $rates['free_shipping'];
$rates = array();
$rates[] = $freeshipping;
} else {
unset($rates['free_shipping']);
}
}
return $rates;
}
add_filter( 'woocommerce_package_rates', 'mindsize_wc_conditional_free_shipping', 10, 2);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment