ChrisTERiS Posted January 30, 2021 Share Posted January 30, 2021 I'm trying to show the value of a product field (have manually added to tblproducts table) to appear in: products.tpl which means I want to add {$product.myfield} in the $products array 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 0 Quote Link to comment Share on other sites More sharing options...
brian! Posted January 30, 2021 Share Posted January 30, 2021 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: products.tpl which means I want to add {$product.myfield} in the $products array 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. 0 Quote Link to comment Share on other sites More sharing options...
ChrisTERiS Posted January 30, 2021 Author Share Posted January 30, 2021 (edited) @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 January 30, 2021 by WHMUp 0 Quote Link to comment Share on other sites More sharing options...
ChrisTERiS Posted January 30, 2021 Author Share Posted January 30, 2021 @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"); 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.