faisal Posted September 21, 2015 Share Posted September 21, 2015 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. 0 Quote Link to comment Share on other sites More sharing options...
brian! Posted September 21, 2015 Share Posted September 21, 2015 take a look at the thread below... you won't be able to use those variables listed above, you'll need to query the database to create them. http://forum.whmcs.com/showthread.php?105774-Show-product-groups-to-homepage-tpl&p=435310#post435310 if you're not familiar with hooks, it might be easier to do it as a data feed. 0 Quote Link to comment Share on other sites More sharing options...
faisal Posted September 22, 2015 Author Share Posted September 22, 2015 Thanks for your replay I read all the topics in forum and docs before I post here I don't want to use {php} tags in the template! Data feed get 1 product group so it will be same as adding it manually to template! 0 Quote Link to comment Share on other sites More sharing options...
faisal Posted September 22, 2015 Author Share Posted September 22, 2015 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 0 Quote Link to comment Share on other sites More sharing options...
brian! Posted September 22, 2015 Share Posted September 22, 2015 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> 1 Quote Link to comment Share on other sites More sharing options...
faisal Posted September 22, 2015 Author Share Posted September 22, 2015 (edited) 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. 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 September 22, 2015 by faisal 0 Quote Link to comment Share on other sites More sharing options...
brian! Posted September 22, 2015 Share Posted September 22, 2015 One last question pleaseIs 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. 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> 1 Quote Link to comment Share on other sites More sharing options...
faisal Posted September 22, 2015 Author Share Posted September 22, 2015 Thanks, This is very helpful information And the examples make me learn how to do/clone stuff. In fact, The examples is the easiest way to learn, at least for me. I am very grateful. 0 Quote Link to comment Share on other sites More sharing options...
faisal Posted September 22, 2015 Author Share Posted September 22, 2015 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. 0 Quote Link to comment Share on other sites More sharing options...
brian! Posted September 22, 2015 Share Posted September 22, 2015 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. 0 Quote Link to comment Share on other sites More sharing options...
faisal Posted September 22, 2015 Author Share Posted September 22, 2015 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 Again I repeat my thanks and gratitude. You are my today's hero 0 Quote Link to comment Share on other sites More sharing options...
brian! Posted September 22, 2015 Share Posted September 22, 2015 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 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. 0 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.