Skip to content

Instantly share code, notes, and snippets.

@kloon
Created June 21, 2013 13:28
Show Gist options
  • Save kloon/5831114 to your computer and use it in GitHub Desktop.
Save kloon/5831114 to your computer and use it in GitHub Desktop.
WooCommerce Free Shipping based on user role
<?php
function get_user_role() {
global $current_user;
$user_roles = $current_user->roles;
$user_role = array_shift($user_roles);
return $user_role;
}
add_filter( 'woocommerce_available_shipping_methods', 'woocommerce_unset_free_shipping_for_certain_users' );
function woocommerce_unset_free_shipping_for_certain_users( $methods ) {
$user_role = get_user_role();
if ( $user_role != 'administrator' )
unset( $methods['free_shipping'] );
return $methods;
}
?>
@gabrielboley
Copy link

hate to ask, but how do i implement this?

@d4mation
Copy link

d4mation commented Dec 1, 2015

Granted this is an old Gist, but for anyone finding this via Google as I did: The filter doesn't exist anymore in WooCommerce.

You'll want to do something like this:

add_filter( 'woocommerce_package_rates', 'woocommerce_set_free_shipping_for_certain_users', 10, 2 );
function woocommerce_set_free_shipping_for_certain_users( $rates, $package ) {

    $user_role = get_user_role();
    if ( $user_role != 'administrator' )
        unset( $rates['free_shipping'] );
    return $rates;

}

Edit: Figured it out

@austinwebdeveloper
Copy link

austinwebdeveloper commented Nov 18, 2016

Where should i add this code? in functions.php?

Help with implementation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment