Jump to content

Test if user is a reseller or has a certain product to customise menu


Recommended Posts

Hi,

 

I've tried to find an answer to this without much success.

 

Is there a way to test if a user is a reseller or has a certain product?

 

e.g. like {if $loggedin}

 

So that you can customise the Services menu for example to only show entries for resellers.

 

e.g. {if $isReseller}

display reseller specific menu item

{/if}

 

Does anybody know a good reference for these variables that can be tested?

 

Thanks,

 

Dan

Link to comment
Share on other sites

it is possible using Action Hook with simple API call, we can check if current user has any product/service (related to resellers):

 

<?php

function hook_ThisClientIsAReseller($vars){

   # Products/Services Available To Resellers
   $resellerProductIDs = array(4,10,11);

   # Admin Username, Needed For API Requests
   $adminUsername = "admin";

   $isReseller = false;

   if (isset($_SESSION['uid']) && $_SESSION['uid']!=''){
       $getServices = localAPI("getclientsproducts", array("clientid" => $_SESSION['uid']), $adminUsername);
       if (is_array($getServices['products']) && count($getServices['products'])>0){
           foreach ($getServices['products'] as $k => $product){
               if (in_array($product['0']['pid'], $resellerProductIDs)){
                   $isReseller = true;
               }
           }
       }
   }

   return array("isReseller" => $isReseller);
}
add_hook("ClientAreaPage", 1, "hook_ThisClientIsAReseller");

 

create new file inside /path/to/whmcs/includes/hooks/ folder with anyname like (clientisreseller.php) or something and put this script code inside it, you may need to modify ($resellerProductIDs, $adminUsername) with valid information.

 

after that from .tpl files you can check if current logged client is a reseller or not using

{if $isReseller}
// Display this content to resellers only
{/if}

 

---

also you can do the same thing using client groups, if your resellers assigned to specific group(s)

Link to comment
Share on other sites

Thanks very much, that's very helpful.

 

So this is the best way to do it, there's nothing baked in?

 

Would I duplicate this and test against other product IDs if I wanted to test another category?

Or I'm wondering if I should combine it to be more efficient.

 

For example if we wanted to test iseCommerce

 

How would you test a client group?

 

Thanks

Link to comment
Share on other sites

Thanks very much, that's very helpful.

 

So this is the best way to do it, there's nothing baked in?

you're welcome, yes that's it, this action hook will work with both v5.x and v6

 

Would I duplicate this and test against other product IDs if I wanted to test another category?

Or I'm wondering if I should combine it to be more efficient.

 

For example if we wanted to test iseCommerce

you can duplicate it and change two/three lines of code to make it work for what you need.

 

How would you test a client group?

 

you will need to create client groups from admin area (Resellers, eCommerce, etc) and manually assign clients to their appropriate group, then we write another action hook to check:

 

<?php

function hook_checkClientGroup($vars){
   $resellersGroupID = '2'; // Specify Resellers Group
   $eCommerceGroupID = '3'; // Specify eCommerce Clients Group

   $adminUsername = "admin"; // Add Valid Admin Username

   $isReseller = false;
   $isEcommerce = false;

   if (isset($_SESSION['uid']) && $_SESSION['uid']!=''){

       $getClientDetails = localAPI("getclientsdetails",
                                    array("clientid" => $_SESSION['uid'],
                                          "stats" => false,
                                          "responsetype" => "json"), $adminUsername);

       if ($getClientDetails['groupid']==$resellersGroupID){
           $isReseller = true;
       }
       if ($getClientDetails['groupid']==$eCommerceGroupID){
           $isEcommerce = true;
       }

   }

   return array("isReseller" => $isReseller, "isEcommerce" => $isEcommerce);
}
add_hook("ClientAreaPage", 1, "hook_checkClientGroup");

Link to comment
Share on other sites

Gosh, thanks.

 

I did have a question about checking whether a client is a reseller using product IDs...

 

In WHMCS you can list all Reseller clients by going to Clients > Products/Services > Reseller Accounts.

 

That made me think there would an account type category stored somewhere.

 

So you could just check a client to see if they have a reseller account category?

Rather than having to check against each product ID.

Link to comment
Share on other sites

Right, thanks.

 

So can we not query whether the client has a product of "Product Type" Reseller Account, rather than check each product ID that happens to correspond to ones reseller products?

 

That way this hook could be provided without modification required for anybody that wants to test if a client is a reseller. And I'd imagine it'd be a little more efficient?

Link to comment
Share on other sites

then we need to modify the first action hook to get IDs of products with "Product Type = Resellers", so you don't have to add these IDs manually:

 

<?php

function hook_ThisClientIsAReseller($vars){

   # Products/Services Available To Resellers
   $resellerProductIDs = array();

   # Admin Username, Needed For API Requests
   $adminUsername = "admin";

   # Get Products/Services with Type = Resellers
   $select_products = full_query("SELECT `id` FROM `tblproducts` WHERE `type`='reselleraccount'");
   while ($_product = mysql_fetch_assoc($select_products)){
       array_push($_product, $resellerProductIDs);
   }

   $isReseller = false;

   if (isset($_SESSION['uid']) && $_SESSION['uid']!=''){
       $getServices = localAPI("getclientsproducts", array("clientid" => $_SESSION['uid']), $adminUsername);
       if (is_array($getServices['products']) && count($getServices['products'])>0){
           foreach ($getServices['products'] as $k => $product){
               if (in_array($product['0']['pid'], $resellerProductIDs)){
                   $isReseller = true;
               }
           }
       }
   }

   return array("isReseller" => $isReseller);
}
add_hook("ClientAreaPage", 1, "hook_ThisClientIsAReseller");

 

- - - Updated - - -

 

unable to edit last post, please change this:

array_push($_product, $resellerProductIDs);

 

with this:

array_push($_product['id'], $resellerProductIDs);

Link to comment
Share on other sites

Ahh, I see

 

I thought checking for "Product Type = Resellers" would negate the need for checking each product ID.

 

I've gone with your first solution as it's more efficient and happy to manually enter the reseller product IDs - also adds more control for allowing other product owners to also see the content.

 

Works great, thanks very much indeed!

 

We'll certainly have a look at your services if we require a custom module.

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.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  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.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use & Guidelines and understand your posts will initially be pre-moderated