Jump to content

Any Hook guru out there?


ChrisTERiS

Recommended Posts

I'm trying to show the value of a product field (have manually added to tblproducts table) to appear in:

  1. products.tpl which means I want to add {$product.myfield} in the $products array
  2. viecart.tpl which means I want to add {$product.productinfo.myfield} in the $products.productinfo array

(2) is not so important, but is good to know if it can be add.

When I started dealing with task I thought that all fields of tblproducts table should be available, but have been mistaken. Using {$products|print_r} in the template I seen that only a few fields are available.

After all, I think that using hooks is the only way to do it (if it's possible even with hooks).

Thank you

Chris

Link to comment
Share on other sites

Hi Chris,

27 minutes ago, WHMUp said:

I'm trying to show the value of a product field (have manually added to tblproducts table)

I don't think WHMCS wants new fields added to existing tables, they tend to prefer new tables to be used and then referenced - but not to worry.

28 minutes ago, WHMUp said:
  1. products.tpl which means I want to add {$product.myfield} in the $products array
  2. viecart.tpl which means I want to add {$product.productinfo.myfield} in the $products.productinfo array

(2) is not so important, but is good to know if it can be add.

I would have thought that 1. would be a clientareapagecart hook that grabs $vars['products'] from the template (I assume only on products.tpl); loops through the array; for each product id in the array, you then do a db query to get the 'field' value for that id; add it to the array and then at the end, return the modified $products array back to the template.

2. should be more or less the same principle I think.

forget the unset in the above hook, but it should show you how you can loop through the $products array and add a new element to it - in your case, you should just need to add the db query before setting the value...

actually, the hook below will be a better example, including a db query to create a new array element...

23 minutes ago, WHMUp said:

When I started dealing with task I thought that all fields of tblproducts table should be available, but have been mistaken. Using {$products|print_r} in the template I seen that only a few fields are available.

yeah, only what WHMCS thinks is needed by default is searched for in the creation of $products.

29 minutes ago, WHMUp said:

After all, I think that using hooks is the only way to do it (if it's possible even with hooks).

depends on the purpose of the new field, e.g if/where it's going to be shown.... there could be multiple different hook options depending on that answer... hell, you might get away with Data Feeds assuming they're not overused on the page and this field is to be shown.

Link to comment
Share on other sites

@brian! Thank you so much. I'll try to figure out how to resolve this issue reading those posts.

I do agree that is better (and more secure) to deal with separate tables, this is what usually I'm doing. But this time is just a field.

As this field holds an image filename I think that I'll use a fast and dirty solution merging the image tag at the top of $product.description and then save the full code to description field. This way should be always available. But first I want to try out the normal way.

Currently I'm doing test modifying this code:

// Products List
	function ct_product_list($vars) 
	{
		global $product;

		if ($product["id"] > 0)
		{

			$ct_product = Capsule::table('tblproducts')
					->where('id','=',$product["id"])
					->orderBy('id','asc')
					->first();
			$product["mod_logo"] = $ct_product->mod_logo;
			array_push($products, $product);
		}

		return;
	}
	// Hooks
	add_hook("ClientAreaPage",1,"ct_product_list");

 

Edited by WHMUp
Link to comment
Share on other sites

@brian!

This works fine. Once more, really appreciate your help!!

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

 

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