Jump to content

PHP isn't targeting guest customers in Woocommerce


Recommended Posts

  • Replies 52
  • Created
  • Last Reply

Top Posters In This Topic

Top Posters In This Topic

I have got the fix!

add_filter( 'woocommerce_package_rates', 'shipping_methods_based_on_wholesale_customer', 10, 2 );
function shipping_methods_based_on_wholesale_customer( $rates, $package ) {
   $user  = wp_get_current_user();
   $roles = (array) $user->roles;
   // Set the shipping methods rate ids in the arrays.
   if ( ! in_array( 'wholesale_customer', $roles, true ) ) {
      $shipping_rates_ids = array( 'flat_rate:10', 'flat_rate:7' ); // To be removed for NON Wholesale users.
   } else {
      $shipping_rates_ids = array( 'flat_rate:13', 'flat_rate:15' ); // To be removed for Wholesale users.
   // Loop through shipping rates from the current shipping package.
   foreach ( $rates as $rate_key => $rate ) {
      if ( in_array( $rate_key, $shipping_rates_ids, true ) ) {
         unset( $rates[ $rate_key ] );
   return $rates;




Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.