Get and use the user group name in Woocommerce










1















I am only using Wordpress with Woocommerce. Is there a way to get user group name based on logged in user's info?



I have group of users called 'Group1' and assigned 2 users to it - user1, user12



I have the second group of users called 'Group2' and assigned 2 users to it - user2 and user23.



Now I need to check from which user group is the logged in user (all users have the same role - Customers).



Here is the sample of the code of what I am trying to do:



$user_logged = wp_get_current_user();
$user_group_name = // how to get user_logged's group name ?
if ($user_group_name == 'Group1') // do 1st
if ($user_group_name == 'Group2') // do 2nd


Names of the groups will be not changed so I will put them as strings into the code. However I need to get dynamically after user loggs in the name of the group he is assigned to.



How can I do it ?



Any help is appreciated.










share|improve this question



















  • 1





    Nobody can really know what are your user groups with the provided information in your question. You should update your question giving more details about the related plugin used for your user groups and about how is saved that 'Group1' or 'Group2' in the database table wp_usermeta. WordPress and Woocommerce don't provide any usergroup functionality by default.

    – LoicTheAztec
    Nov 13 '18 at 20:47












  • Yes, you are right. I had wp-members plugin installed. All of the answers below were very helpful. Thank you.

    – Bart
    Nov 25 '18 at 22:26











  • You should accept one of this answers, the best one for you… Thank you.

    – LoicTheAztec
    Nov 26 '18 at 10:23















1















I am only using Wordpress with Woocommerce. Is there a way to get user group name based on logged in user's info?



I have group of users called 'Group1' and assigned 2 users to it - user1, user12



I have the second group of users called 'Group2' and assigned 2 users to it - user2 and user23.



Now I need to check from which user group is the logged in user (all users have the same role - Customers).



Here is the sample of the code of what I am trying to do:



$user_logged = wp_get_current_user();
$user_group_name = // how to get user_logged's group name ?
if ($user_group_name == 'Group1') // do 1st
if ($user_group_name == 'Group2') // do 2nd


Names of the groups will be not changed so I will put them as strings into the code. However I need to get dynamically after user loggs in the name of the group he is assigned to.



How can I do it ?



Any help is appreciated.










share|improve this question



















  • 1





    Nobody can really know what are your user groups with the provided information in your question. You should update your question giving more details about the related plugin used for your user groups and about how is saved that 'Group1' or 'Group2' in the database table wp_usermeta. WordPress and Woocommerce don't provide any usergroup functionality by default.

    – LoicTheAztec
    Nov 13 '18 at 20:47












  • Yes, you are right. I had wp-members plugin installed. All of the answers below were very helpful. Thank you.

    – Bart
    Nov 25 '18 at 22:26











  • You should accept one of this answers, the best one for you… Thank you.

    – LoicTheAztec
    Nov 26 '18 at 10:23













1












1








1








I am only using Wordpress with Woocommerce. Is there a way to get user group name based on logged in user's info?



I have group of users called 'Group1' and assigned 2 users to it - user1, user12



I have the second group of users called 'Group2' and assigned 2 users to it - user2 and user23.



Now I need to check from which user group is the logged in user (all users have the same role - Customers).



Here is the sample of the code of what I am trying to do:



$user_logged = wp_get_current_user();
$user_group_name = // how to get user_logged's group name ?
if ($user_group_name == 'Group1') // do 1st
if ($user_group_name == 'Group2') // do 2nd


Names of the groups will be not changed so I will put them as strings into the code. However I need to get dynamically after user loggs in the name of the group he is assigned to.



How can I do it ?



Any help is appreciated.










share|improve this question
















I am only using Wordpress with Woocommerce. Is there a way to get user group name based on logged in user's info?



I have group of users called 'Group1' and assigned 2 users to it - user1, user12



I have the second group of users called 'Group2' and assigned 2 users to it - user2 and user23.



Now I need to check from which user group is the logged in user (all users have the same role - Customers).



Here is the sample of the code of what I am trying to do:



$user_logged = wp_get_current_user();
$user_group_name = // how to get user_logged's group name ?
if ($user_group_name == 'Group1') // do 1st
if ($user_group_name == 'Group2') // do 2nd


Names of the groups will be not changed so I will put them as strings into the code. However I need to get dynamically after user loggs in the name of the group he is assigned to.



How can I do it ?



Any help is appreciated.







php wordpress woocommerce usergroups






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 13 '18 at 20:55









LoicTheAztec

88.9k1365102




88.9k1365102










asked Nov 13 '18 at 19:43









BartBart

173




173







  • 1





    Nobody can really know what are your user groups with the provided information in your question. You should update your question giving more details about the related plugin used for your user groups and about how is saved that 'Group1' or 'Group2' in the database table wp_usermeta. WordPress and Woocommerce don't provide any usergroup functionality by default.

    – LoicTheAztec
    Nov 13 '18 at 20:47












  • Yes, you are right. I had wp-members plugin installed. All of the answers below were very helpful. Thank you.

    – Bart
    Nov 25 '18 at 22:26











  • You should accept one of this answers, the best one for you… Thank you.

    – LoicTheAztec
    Nov 26 '18 at 10:23












  • 1





    Nobody can really know what are your user groups with the provided information in your question. You should update your question giving more details about the related plugin used for your user groups and about how is saved that 'Group1' or 'Group2' in the database table wp_usermeta. WordPress and Woocommerce don't provide any usergroup functionality by default.

    – LoicTheAztec
    Nov 13 '18 at 20:47












  • Yes, you are right. I had wp-members plugin installed. All of the answers below were very helpful. Thank you.

    – Bart
    Nov 25 '18 at 22:26











  • You should accept one of this answers, the best one for you… Thank you.

    – LoicTheAztec
    Nov 26 '18 at 10:23







1




1





Nobody can really know what are your user groups with the provided information in your question. You should update your question giving more details about the related plugin used for your user groups and about how is saved that 'Group1' or 'Group2' in the database table wp_usermeta. WordPress and Woocommerce don't provide any usergroup functionality by default.

– LoicTheAztec
Nov 13 '18 at 20:47






Nobody can really know what are your user groups with the provided information in your question. You should update your question giving more details about the related plugin used for your user groups and about how is saved that 'Group1' or 'Group2' in the database table wp_usermeta. WordPress and Woocommerce don't provide any usergroup functionality by default.

– LoicTheAztec
Nov 13 '18 at 20:47














Yes, you are right. I had wp-members plugin installed. All of the answers below were very helpful. Thank you.

– Bart
Nov 25 '18 at 22:26





Yes, you are right. I had wp-members plugin installed. All of the answers below were very helpful. Thank you.

– Bart
Nov 25 '18 at 22:26













You should accept one of this answers, the best one for you… Thank you.

– LoicTheAztec
Nov 26 '18 at 10:23





You should accept one of this answers, the best one for you… Thank you.

– LoicTheAztec
Nov 26 '18 at 10:23












4 Answers
4






active

oldest

votes


















1














You almost made it. Based on documentation, wp_get_current_user() return WP_User object that has properties roles (the roles the user is part of).



$roles = wp_get_current_user()->roles;

if (in_array("Group1", $roles))
// do 1st

if (in_array("Group2", $roles))
// do 2nd



Hope it helps






share|improve this answer






























    1














    If you mean that you want to know what the role of an online user is?



    Finally, you should use the following code.



    $get = wp_get_current_user();
    if ( in_array( 'administrator', $get->roles ) )
    // do 1st
    if ( in_array( 'editor', $get->roles ) )
    // do 2st
    if ( in_array( 'group1', $get->roles ) )
    // do 3st
    if ( in_array( 'groupN', $get->roles ) )
    // do N st





    share|improve this answer






























      1














      You can use easily and directly current_user_can() WordPress dedicated function with a specific user role (or a user capability) like:



      if ( current_user_can( 'Group1' ) ) 
      // do 1st
      elseif ( current_user_can( 'Group2' ) )
      // do 2st




      WordPress and Woocommerce don't provide any Usergroup functionality by default. What related plugin are you using for those Usergroups.

      You need to search in the database table wp_usermeta to see if 'Group1' or 'Group2' are assigned to users and give us the meta_key used for it.







      share|improve this answer
































        1














        Use wp_get_current_user()->roles to retrieve the groups the user is a member of.



        $roles = wp_get_current_user()->roles;

        if (in_array('group1',$roles))
        echo 'Do something here for group 1';






        share|improve this answer
























          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53288398%2fget-and-use-the-user-group-name-in-woocommerce%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          4 Answers
          4






          active

          oldest

          votes








          4 Answers
          4






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          You almost made it. Based on documentation, wp_get_current_user() return WP_User object that has properties roles (the roles the user is part of).



          $roles = wp_get_current_user()->roles;

          if (in_array("Group1", $roles))
          // do 1st

          if (in_array("Group2", $roles))
          // do 2nd



          Hope it helps






          share|improve this answer



























            1














            You almost made it. Based on documentation, wp_get_current_user() return WP_User object that has properties roles (the roles the user is part of).



            $roles = wp_get_current_user()->roles;

            if (in_array("Group1", $roles))
            // do 1st

            if (in_array("Group2", $roles))
            // do 2nd



            Hope it helps






            share|improve this answer

























              1












              1








              1







              You almost made it. Based on documentation, wp_get_current_user() return WP_User object that has properties roles (the roles the user is part of).



              $roles = wp_get_current_user()->roles;

              if (in_array("Group1", $roles))
              // do 1st

              if (in_array("Group2", $roles))
              // do 2nd



              Hope it helps






              share|improve this answer













              You almost made it. Based on documentation, wp_get_current_user() return WP_User object that has properties roles (the roles the user is part of).



              $roles = wp_get_current_user()->roles;

              if (in_array("Group1", $roles))
              // do 1st

              if (in_array("Group2", $roles))
              // do 2nd



              Hope it helps







              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Nov 13 '18 at 20:39









              deerawandeerawan

              2,67941525




              2,67941525























                  1














                  If you mean that you want to know what the role of an online user is?



                  Finally, you should use the following code.



                  $get = wp_get_current_user();
                  if ( in_array( 'administrator', $get->roles ) )
                  // do 1st
                  if ( in_array( 'editor', $get->roles ) )
                  // do 2st
                  if ( in_array( 'group1', $get->roles ) )
                  // do 3st
                  if ( in_array( 'groupN', $get->roles ) )
                  // do N st





                  share|improve this answer



























                    1














                    If you mean that you want to know what the role of an online user is?



                    Finally, you should use the following code.



                    $get = wp_get_current_user();
                    if ( in_array( 'administrator', $get->roles ) )
                    // do 1st
                    if ( in_array( 'editor', $get->roles ) )
                    // do 2st
                    if ( in_array( 'group1', $get->roles ) )
                    // do 3st
                    if ( in_array( 'groupN', $get->roles ) )
                    // do N st





                    share|improve this answer

























                      1












                      1








                      1







                      If you mean that you want to know what the role of an online user is?



                      Finally, you should use the following code.



                      $get = wp_get_current_user();
                      if ( in_array( 'administrator', $get->roles ) )
                      // do 1st
                      if ( in_array( 'editor', $get->roles ) )
                      // do 2st
                      if ( in_array( 'group1', $get->roles ) )
                      // do 3st
                      if ( in_array( 'groupN', $get->roles ) )
                      // do N st





                      share|improve this answer













                      If you mean that you want to know what the role of an online user is?



                      Finally, you should use the following code.



                      $get = wp_get_current_user();
                      if ( in_array( 'administrator', $get->roles ) )
                      // do 1st
                      if ( in_array( 'editor', $get->roles ) )
                      // do 2st
                      if ( in_array( 'group1', $get->roles ) )
                      // do 3st
                      if ( in_array( 'groupN', $get->roles ) )
                      // do N st






                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Nov 13 '18 at 20:22









                      Arman HosseiniArman Hosseini

                      2010




                      2010





















                          1














                          You can use easily and directly current_user_can() WordPress dedicated function with a specific user role (or a user capability) like:



                          if ( current_user_can( 'Group1' ) ) 
                          // do 1st
                          elseif ( current_user_can( 'Group2' ) )
                          // do 2st




                          WordPress and Woocommerce don't provide any Usergroup functionality by default. What related plugin are you using for those Usergroups.

                          You need to search in the database table wp_usermeta to see if 'Group1' or 'Group2' are assigned to users and give us the meta_key used for it.







                          share|improve this answer





























                            1














                            You can use easily and directly current_user_can() WordPress dedicated function with a specific user role (or a user capability) like:



                            if ( current_user_can( 'Group1' ) ) 
                            // do 1st
                            elseif ( current_user_can( 'Group2' ) )
                            // do 2st




                            WordPress and Woocommerce don't provide any Usergroup functionality by default. What related plugin are you using for those Usergroups.

                            You need to search in the database table wp_usermeta to see if 'Group1' or 'Group2' are assigned to users and give us the meta_key used for it.







                            share|improve this answer



























                              1












                              1








                              1







                              You can use easily and directly current_user_can() WordPress dedicated function with a specific user role (or a user capability) like:



                              if ( current_user_can( 'Group1' ) ) 
                              // do 1st
                              elseif ( current_user_can( 'Group2' ) )
                              // do 2st




                              WordPress and Woocommerce don't provide any Usergroup functionality by default. What related plugin are you using for those Usergroups.

                              You need to search in the database table wp_usermeta to see if 'Group1' or 'Group2' are assigned to users and give us the meta_key used for it.







                              share|improve this answer















                              You can use easily and directly current_user_can() WordPress dedicated function with a specific user role (or a user capability) like:



                              if ( current_user_can( 'Group1' ) ) 
                              // do 1st
                              elseif ( current_user_can( 'Group2' ) )
                              // do 2st




                              WordPress and Woocommerce don't provide any Usergroup functionality by default. What related plugin are you using for those Usergroups.

                              You need to search in the database table wp_usermeta to see if 'Group1' or 'Group2' are assigned to users and give us the meta_key used for it.








                              share|improve this answer














                              share|improve this answer



                              share|improve this answer








                              edited Nov 13 '18 at 20:51

























                              answered Nov 13 '18 at 20:33









                              LoicTheAztecLoicTheAztec

                              88.9k1365102




                              88.9k1365102





















                                  1














                                  Use wp_get_current_user()->roles to retrieve the groups the user is a member of.



                                  $roles = wp_get_current_user()->roles;

                                  if (in_array('group1',$roles))
                                  echo 'Do something here for group 1';






                                  share|improve this answer





























                                    1














                                    Use wp_get_current_user()->roles to retrieve the groups the user is a member of.



                                    $roles = wp_get_current_user()->roles;

                                    if (in_array('group1',$roles))
                                    echo 'Do something here for group 1';






                                    share|improve this answer



























                                      1












                                      1








                                      1







                                      Use wp_get_current_user()->roles to retrieve the groups the user is a member of.



                                      $roles = wp_get_current_user()->roles;

                                      if (in_array('group1',$roles))
                                      echo 'Do something here for group 1';






                                      share|improve this answer















                                      Use wp_get_current_user()->roles to retrieve the groups the user is a member of.



                                      $roles = wp_get_current_user()->roles;

                                      if (in_array('group1',$roles))
                                      echo 'Do something here for group 1';







                                      share|improve this answer














                                      share|improve this answer



                                      share|improve this answer








                                      edited Nov 14 '18 at 16:19

























                                      answered Nov 13 '18 at 20:01









                                      DaveDave

                                      2,34051725




                                      2,34051725



























                                          draft saved

                                          draft discarded
















































                                          Thanks for contributing an answer to Stack Overflow!


                                          • Please be sure to answer the question. Provide details and share your research!

                                          But avoid


                                          • Asking for help, clarification, or responding to other answers.

                                          • Making statements based on opinion; back them up with references or personal experience.

                                          To learn more, see our tips on writing great answers.




                                          draft saved


                                          draft discarded














                                          StackExchange.ready(
                                          function ()
                                          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53288398%2fget-and-use-the-user-group-name-in-woocommerce%23new-answer', 'question_page');

                                          );

                                          Post as a guest















                                          Required, but never shown





















































                                          Required, but never shown














                                          Required, but never shown












                                          Required, but never shown







                                          Required, but never shown

































                                          Required, but never shown














                                          Required, but never shown












                                          Required, but never shown







                                          Required, but never shown







                                          Popular posts from this blog

                                          Use pre created SQLite database for Android project in kotlin

                                          Darth Vader #20

                                          Ondo