Jump to content

Adding element(s) to price-area


trenttdogg

Recommended Posts

Hello,

I'm using version 7.7 and trying to edit the products.tpl (premium comparison). i am trying to figure out how to put some text in the price-area for each product. Ideally, i would like to put it under the $price/month field. I added a field to DB (tblproducts) called priceDetails and tried to get it to display on the page using: 

<h3 id="product{$product@iteration}-priceDetails">{$product.priceDetails}</h4> ...

 

it was a shot in the dark which did not work. Any ideas?  

Thanks,

Trent

Link to comment
Share on other sites

HI Trent,

On 20/04/2019 at 07:14, trenttdogg said:

i am trying to figure out how to put some text in the price-area for each product. Ideally, i would like to put it under the $price/month field. I added a field to DB (tblproducts) called priceDetails

i'm not sure it was a good idea to add a new column to an existing table - not least because $products is a complex array and it's not just generated from a simple query of the tblproducts table by WHMCS internally.

I think the better way might have been to use a new table (perhaps with pid & pricedetails as columns) and used a hook to add it to the products array.... however, we are where we are...

On 20/04/2019 at 07:14, trenttdogg said:

it was a shot in the dark which did not work. Any ideas?  

first thought would be to double check that the new variable doesn't exist in the array (logically it shouldn't, and your lack of output seems to confirm that - but confirmation is always good to have!) 🙂

you can do that by adding {debug} to the end of the products.tpl template, refreshing the page in the browser and you should get a popup window of all (actually it's only some) the available Smarty arrays & variables to the template... I suspect the values won't be there... don't forget to remove {debug} from the template when you've finished.

if it is missing, then you can add it simply by using a hook - if you had used a separate table, it would have made the database query slightly more complicated than it is, but not by much.

<?php

# Add Price Details To Products Array Hook
# @author brian!

use WHMCS\Database\Capsule;

function add_price_details_to_products_array_hook($vars)
{
	if ($vars['templatefile'] == "products") {
		$products = $vars['products'];	
		foreach ($products as $key => $product) {
			$pricedetails = Capsule::table('tblproducts')->where('id',$product['pid'])->value('priceDetails');
			$products[$key]['priceDetails'] = $pricedetails;
		}
		return array("products" => $products);
	}
}
add_hook("ClientAreaPageCart", 1, "add_price_details_to_products_array_hook");

if it works (it will work with another field, but I haven't got a pricedetails field in my tblproducts table!), then you should a) see the {product.priceDetails} output on the page and b) it should also be present in the $products array when looking at the popup window. 🙂

Link to comment
Share on other sites

Thanks Brian,

I did the {debug} and nothing returned, so I guess that's good...

Regarding the code that you so kindly wrote:

On 4/21/2019 at 1:58 AM, brian! said:

<?php # Add Price Details To Products Array Hook # @author brian! use WHMCS\Database\Capsule; function add_price_details_to_products_array_hook($vars) { if ($vars['templatefile'] == "products") { $products = $vars['products']; foreach ($products as $key => $product) { $pricedetails = Capsule::table('tblproducts')->where('id',$product['pid'])->value('priceDetails'); $products[$key]['priceDetails'] = $pricedetails; } return array("products" => $products); } } add_hook("ClientAreaPageCart", 1, "add_price_details_to_products_array_hook");

Which file(s) would I include this?

Thanks, for your help. 

Link to comment
Share on other sites

On 23/04/2019 at 03:21, trenttdogg said:

Which file(s) would I include this?

sorry I should probably have mentioned that bit! 🙄

generally with hooks, you create a .php file in /includes/hooks, give it a memorable filename, copy&paste the code into it, save and the next time you refresh the page in the browser, you should have access to those new variables.

i've attached the hook to this post, so just upload it to /includes/hooks and you should be good to go. 🙂

productnewfield.php

Link to comment
Share on other sites

  • WHMCS Developer

If you're encountering an "Oops!" page, then that indicates that a PHP error has occurred.

You'll want to enable the Display Errors option in the Setup > General Settings > Other section of your Admin Area, and then reproduce the issue.

You should then see a PHP error displayed which you can use to determine what the issue with the hook-file is.

Link to comment
Share on other sites

21 hours ago, trenttdogg said:

I got the dreaded  "Oops! Something went wrong and we couldn't process your request." error when I refreshed after installing that file in the hooks directory. 

What @WHMCS Nathan suggests is the way to go with this... the most likely error is the one below where it can't find the database column you're trying to add...

Quote

Column not found: 1054 Unknown column 'priceDetails' in 'field list'

if that's what you're seeing, then that new column can't be called priceDetails... the rest of the hook looks ok to me, that new database field is the only unknown that i'm working with... let me know what the error is.

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