Jump to content

Product Info on Custom Pages


BAJI26

Recommended Posts

Is there a way to show the product name, product price and product description on custom whmcs page without using the javascript data feed?

yes by using a hook - I assume the custom page will know the product ID, and therefore you can use that in a query to the database to get name and description from one database table and it's price(s) from another table.

Link to comment
Share on other sites

and are these four plans all in the one product group and which billingcycle price do you intend to show?

 

I mean roughly it's going to be along the lines of the hook I wrote in the thread below... but you'll likely need to trim it down because that would give you an array of all products and pricing.

 

https://forum.whmcs.com/showthread.php?120498-Retrieving-product-pricing-information-using-capsule&p=485706#post485706

Link to comment
Share on other sites

It belongs to the same group...

then you can replace the code below into the hook to limit the query (just replace x with the productgroup ID value).

 

    $products = Capsule::table('tblproducts')
                       ->join('tblpricing', 'tblproducts.id', '=', 'tblpricing.relid')
                       ->select('tblproducts.*','tblpricing.*')
                       ->where('tblproducts.gid', 'x')
                       ->where('tblpricing.type', 'product')
                       ->get();

also you can edit the select line to limit what fields you're going to need - if it's just a pricing table, then it might just be name, description and the billingcycle required.... and at some point, you'll need to also limit this hook to working only on your custom page - otherwise it will run on all client area pages.

 

in case it's not obvious, the hook will do nothing visibly in your template - it's just passes the array of information you require... it's then down to using a foreach loop to create your pricing table to show the product details.

Link to comment
Share on other sites

Where do I put that code and how to add it to the custom page to show the particular price, description?

you create a .php file in /includes/hooks, give it a memorable filename, e.g pricingtable.php and paste the code into it... then when you refresh the page, it should then be able to access the array.

 

as I said previously, you won't see any difference in the template output unless you either foreach loop through the array to output it... or add {debug} to the end of the template code to get a popup window.

 

in terms of the foreach, they're used on most template pages within WHMCS - take a look at a pricing page, or a products.tpl page etc... but as it's most basic...

 

    <table>
   <tr><th>Name</th><th>Description</th><th>Monthly Price</th></tr>
   {foreach $myproducts as $myproduct}
       <tr><td>{$myproduct.name}</td><td>{$myproduct.description}</td><td>{$myproduct.monthly}</td></tr>
   {/foreach}
   </table>

now you could use divs instead for the layout, but that's basically how to output from the array - it's just a case of styling the output to suit the content.

 

btw - now you can see why I said it would be easier to use a data feed! :)

Link to comment
Share on other sites

Hi, this is what my hook php file looks like:

 


<?php

$products = Capsule::table('tblproducts')
                       ->join('tblpricing', 'tblproducts.id', '=', 'tblpricing.relid')
                       ->select('tblproducts.*','tblpricing.*')
                       ->where('tblproducts.gid', 'x')
                       ->where('tblpricing.type', 'product')
                       ->get();  








?>                       

 

But it makes my site blank...

Link to comment
Share on other sites

But it makes my site blank...

as I said, you need to replace the 'x' with the product group ID of the group that has these four plans - when you give it the correct number,e.g '1' - then it will work. :idea:

 

and also, you can't just add a query like that, you're going to need the rest of the hook code from the other thread too...

 

<?php

use Illuminate\Database\Capsule\Manager as Capsule;

function homepage_products_hook($vars) {

   $products = Capsule::table('tblproducts')
                       ->join('tblpricing', 'tblproducts.id', '=', 'tblpricing.relid')
                       ->select('tblproducts.*','tblpricing.*')
                       ->where('tblproducts.gid','x')
                       ->where('tblpricing.type', 'product')
                       ->get();

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

   return array("myproducts" => $decodedata);
}
add_hook("ClientAreaPage", 1, "homepage_products_hook");
?>  

Link to comment
Share on other sites

I did put in the gid and also just used the last snippet above and the site still renders blank page... none accessible!

 

I removed the last line

 add_hook("ClientAreaPage", 1, "homepage_products_hook"); 

and the site comes back...

Edited by BAJI26
Link to comment
Share on other sites

I did put in the gid and also just used the last snippet above and the site still renders blank page... none accessible!

I removed the last line

 add_hook("ClientAreaPage", 1, "homepage_products_hook"); 

and the site comes back...

that would always work because it simply stops the activation of the hook code... if you ever see a blank page with a hook, then goto setup -> general settings -> other and enable "Display Errors" - that should give you an indication as to where the error is.

 

however, just I looking at the code I can see the error - it's cos I manually typed in tlbproducts instead of tblproducts in the reply - yes, that's how sensitive these hooks are to errors...

 

anyway, try using this improved version...

 

<?php

use Illuminate\Database\Capsule\Manager as Capsule;

function homepage_products_hook($vars) {

   $client = Menu::context('client');

   $products = Capsule::table('tblproducts')
                       ->join('tblpricing', 'tblproducts.id', '=', 'tblpricing.relid')
                       ->select('tblproducts.*','tblpricing.*')
                       ->where('tblpricing.type', 'product')
                       ->where('tblproducts.hidden','0')
                       ->where('tblproducts.gid','x')
                       ->groupBy('tblproducts.order')
                       ->get();

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

   return array("myproducts" => $decodedata);
}
add_hook("ClientAreaPage", 1, "homepage_products_hook");
?>

not pretty, but it will work...

 

oiwSBt8.png

 

What should I look for in the pop-up page after I added {debug}?

for you, probably it's only use will be to tell you which array variable names of $myproducts (specifically $myproduct) you can use in the output...

 

QWgn3RG.png

 

as I said previously, you probably won't need 90% of the array, so you could trim that down to just the bare essentials (when you know what you want), and then tweak the hook to only work on this one custom page (currently it will be working on all pages)... but get it working first!

Link to comment
Share on other sites

Yes,I wanna get it working first then I'll play and learn....

lol - it does feel like i'm running a correspondence course where you can learn to write a hook by buying a weekly magazine and eventually at week 50 you'll have the complete hook! :)

 

I don't have that [highlight]$myproducts[/highlight] in the debug window

then the hook doesn't seem to be running - even if the query was wrong, it would exist but be empty.

 

you could try creating the output on the homepage (or any other client area page) - in fact, just quickly adding {debug} to one of those templates will tell you if the array is there or not.

 

I suspect i'm going to need to see the site in general, and perhaps the code of this custom page... it's quite difficult to advise on a page you can't see (especially if I don't know whether it's been created correctly).

 

I know you have just sent me a PM, but that didn't include any code or a link!

 

With some changes to the hook file I got this in the debug!

then that tells me your query is wrong and returning no results.

Link to comment
Share on other sites

ok, you made two errors in the hook when editing/renaming it - the biggest one being that you changed the hook call itself...

 

add_hook("WordPress Hosting", 1, "RG_wordpress_page_hooks");

for a custom page like this, it needs to be ClientAreaPage.

 

add_hook("ClientAreaPage", 1, "RG_wordpress_page_hooks");

 

try it again now - it's fixed as I can see the correct array in the debug window... and as a bonus, i've tweaked it so that the hook will now only be called on this one custom page. :idea:

 

one other thing, looking at the page if you want to call a price directly (and you can trust that your product order on the page is the same as the query result), you can replace...

 

<span><script language="javascript" src="feeds/productsinfo-No-Currency.php?pid=85&get=price&billingcycle=monthly"></script></span>/Monthly

with this...

<span>${$myproducts.0.monthly}</span>/Monthly

similarly, you could change the names to be called directly from the hook result...

 

<h4>Starter</h4>

to...

<h4>{$myproducts.0.name}</h4>

if you did that, and did it consistently for all products, descriptions, prices etc then it wouldn't matter if you got the order wrong, they should always match (apart from the taglines which aren't in the database)... it might be slightly easier that way for you to replace the feeds with specific Smarty variables rather than use a foreach loop. :)

Link to comment
Share on other sites

Ok! Thanks! Exactly what I wanted!

I've used <span>${$myproducts.0.monthly}</span>/Monthly on the different packages and just changed the 0 to 1 and it will show for the next package which worked for 3 of the 4 but the 4th package no price shows... What is that number in relation to the product itself?

Link to comment
Share on other sites

Thanks! So I can use what you did and apply it to other pages just create another hook php file correct?

yeah - but remember that IF statement contains the name of the template filename that can access the hook... so if you duplicate the hook code to use on another template, remember to replace that filename used in the IF statement with the new template name.

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.

×
×
  • 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