Home > Enterprise >  Display Additional Text in My Account's Dashboard for Specific User Roles for WooCommerce
Display Additional Text in My Account's Dashboard for Specific User Roles for WooCommerce

Time:11-25

My IF statement is not correctly filtering the user roles after they've logged in to display additional text within the dashboard for Corporate/Partner users.

I'm making this edit directly in dashboard.php from woocommerce/templates/myaccount

<p>
    <?php
      if( is_user_logged_in() ) {
         $user = wp_get_current_user();
         $role = ( array ) $user->roles;
      if($role[0]=="Corporate"||"Partner")
         printf('Bulk rate discount is available at listed rates: ');
        }
    ?>
</p>

What I've tried: The text showed regardless of the user's role. This means that the check failed. I'm not sure where the problem lies.

Upon changing the statement to only

if($role[0]=="Corporate")

The text does not show up at all.

What I was expecting: When the user logs in, it should recognize the roles, and show the text I've included if they are relevant.

CodePudding user response:

Please try the below code: If you want to check the logged in user role, then uncomment the echo line.

        global $current_user;

    $user_roles = $current_user->roles;
    $user_role = array_shift($user_roles);

    //echo $user_role;

      if($user_role =="corporate" || $user_role == "partner")
         printf('Bulk rate discount is available at listed rates: ');
      

I have tested it and it's working. Please let me know if you find any issue with the code

  • Related