Jump to content

List Products Groups Hook! please help


faisal

Recommended Posts

Hi all

 

 

I know little PHP and I am trying to make my first hook to display Products Groups List in whmcs/index.php but I couldn't do it :(

 

 

I need your help to create a hook that allow me to use something like this in template files:

 

    <ul>
       {foreach key=num item=productgroup from=$productgroups}
           <li><a href="cart.php?gid={$productgroup.gid}">{$productgroup.name}</a></li>
       {/foreach}
   </ul>

 

So the end result will be:

  • product group 1
  • product group 2
  • product group 3
  • and so on

 

Thanks in advance.

Link to comment
Share on other sites

This is the last work i did in my hook file productgroupslist.php

 

if (!defined("WHMCS"))
   die("This file cannot be accessed directly");

use Carbon\Carbon;
use WHMCS\User\Client;
use WHMCS\View\Menu\Item;



function productgroupslist($vars){

   if ($vars['filename']=='index'){




       $table = "tblproductgroups";
       $fields = "id,name,hidden";
       $where = array("hidden"=>"0");
       $result = select_query($table,$fields,$where);
       $data = mysql_fetch_array($result);
       $pgid = $data['id'];
       $pgname = $data['name'];
       $pgnames = array(
                              'pgname' => $pgname,
                              );


       $testvar = "This is just a test!";
   }

   return array(
                "testvar" => $testvar,
                "pgid" => $pgid,
                "pgname" => $pgname,
                "pgnames" => $pgnames
                );

}
add_hook("ClientAreaPageHome", 1, "productgroupslist");

 

 

and this is homepage.tpl template code:

 

{$testvar}
<br>

<ul>
   {foreach key=num item=pgname from=$pgnames}
       <li><a href="cart.php?gid={$pgid}">{$pgname}</a></li>
   {/foreach}
</ul>

 

 

It only show one product group!

 

Please help with this

Link to comment
Share on other sites

It only show one product group!

because you need to loop through the array - otherwise it will just show one group. :)

 

here's a data feed that will display a list of non-hidden product groups and add links to them... create a file in the feeds directory, call it 'productgroups.php' and add the following code...

 

<?php

/*
*** USAGE SAMPLES ***

<script language="javascript" src="feeds/productgroups.php"></script>

*/

require("../init.php");

$result = "SELECT id,name FROM tblproductgroups WHERE tblproductgroups.hidden = 0 ORDER BY tblproductgroups.order ASC";
$data = mysql_query($result);
while ($row = mysql_fetch_assoc($data)) {
   $code .= '<li><a href="cart.php?gid='.$row['id'].'">'.$row['name'].'</a></li>';
}  
echo "document.write('".$code."');";

to use it in your template, replace your {foreach} loop above with...

 

<script language="javascript" src="feeds/productgroups.php"></script>

Link to comment
Share on other sites

Thanks brian!

This works just great

One last question please

 

Is this way (datafeed) better than making a hook?

 

 

because you need to loop through the array - otherwise it will just show one group. smile.png

Can you give example :)

 

*I am doing this because I want to remove the domain checker and the news/tweets from whmcs/index.php and I wand to fill the page with something else like products groups, FAQs and some other things :)

Edited by faisal
Link to comment
Share on other sites

One last question please

Is this way (datafeed) better than making a hook?

that depends on what you want to use it for - and where! :)

 

if it's external to WHMCS, e.g your main website, an affiliate's website etc, a data feed would be better suited.

 

if it's within WHMCS, then either - but given the choice, a hook would probably be better - especially if you are familiar with writing them;

 

if not, I tend to suggest using an existing data feed and modifying it - a poorly coded hook can cause unwanted events; whereas a bad data feed will likely just show you nothing.

 

*I am doing this because I want to remove the domain checker and the news/tweets from whmcs/index.php and I want to fill the page with something else like products groups, FAQs and some other things :)

to do that, you could go either way - hook or feed - whichever you feel most comfortable using. :idea:

 

e.g, if you wanted to list the product groups using an action hook instead of a data feed, you could create a hook in the hooks folder, call it 'productgroups.php' (or anything you like!) and add the following to it...

 

<?php

use Illuminate\Database\Capsule\Manager as Capsule;

function hook_getProductGroups($vars){

   # Get Product Groups
           $getProductGroups = Capsule::table('tblproductgroups')
                               ->where('tblproductgroups.hidden', '0')
                               ->select('id', 'name')
                               ->orderBy('order', 'asc')
                               ->get();

   $encodedata = json_encode($getProductGroups);
   $decodedata = json_decode($encodedata, true);

   return array("productgroups" => $decodedata);
}
add_hook("ClientAreaPageHome", 1, "hook_getProductGroups");

that will create a $productgroups array and make it available to the homepage - then in the homepage.tpl template, you can use your {foreach} loop to output the list of product groups and add links to them...

 

<ul>
   {foreach $productgroups as $pgroup}
       <li><a href="cart.php?gid={$pgroup.id}">{$pgroup.name}</a></li>
   {/foreach}
</ul>

Link to comment
Share on other sites

And I think if use hook then I can use different class or layout for the list in every template using one hook

 

example: in home

 

<ul class="something">
   {foreach $productgroups as $pgroup}
       <li><a href="cart.php?gid={$pgroup.id}">{$pgroup.name}</a></li>
   {/foreach}
</ul> 

 

And in another template I can do for example

 

<ul class="something-else">
   {foreach $productgroups as $pgroup}
       <li><a href="cart.php?gid={$pgroup.id}">{$pgroup.name}</a></li>
   {/foreach}
</ul>

 

Or even:

 

{foreach $productgroups as $pgroup}
       <a href="cart.php?gid={$pgroup.id}" class="btn btn-primary">{$pgroup.name}</a>
   {/foreach}

 

 

With only one hook :)

 

 

But in data feed need to override css or create another data feed file.

Link to comment
Share on other sites

And I think if use hook then I can use different class or layout for the list in every template using one hook - But in data feed need to override css or create another data feed file.

yes but you've moved the goalposts... your original question was about doing one specific thing on one specific page - for that task, hook or feed would work fine.

 

if you need it to be flexible and styled differently on different pages, then you wouldn't necessarily need multiple feeds - one would be enough with various styling options built in.

 

also, don't forget the above hook is coded only to work on the homepage - if you wanted to use it on other templates, you'd need to modify it before other templates could access the productgroup array.

Link to comment
Share on other sites

yes but you've moved the goalposts... your original question was about doing one specific thing on one specific page - for that task, hook or feed would work fine.

 

if you need it to be flexible and styled differently on different pages, then you wouldn't necessarily need multiple feeds - one would be enough with various styling options built in.

 

also, don't forget the above hook is coded only to work on the homepage - if you wanted to use it on other templates, you'd need to modify it before other templates could access the productgroup array.

 

True, and what you provide is what I need 100% (I want product groups in homepage.tpl only)

 

I am just saying/trying to compare to help other people like me (not code writers but know little php and like to do it by themselves) decide what to use :-P

 

 

Again I repeat my thanks and gratitude. You are my today's hero :)

Link to comment
Share on other sites

I am just saying/trying to compare to help other people like me (not code writers but know little php and like to do it by themselves) decide what to use :-P

the best way to think of them is that a data feed can provide you with the complete output; whereas a hook (in the clientarea) is generally used to pass variables to the page for use in the template.

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