Muller Posted July 31, 2011 Share Posted July 31, 2011 Hiya, In my store each product belongs to 5 categories. I'd like to display the names of those categories when the user browses through the products. Is this information available in product-list.tpl? I've looked through the ProductController.php and I can't see an array of categories, only the default category to which the product belongs. Is this information simply not available, will I have to add it myself in the controller somehow? If so, how? Thanks. Link to comment Share on other sites More sharing options...
Muller Posted July 31, 2011 Author Share Posted July 31, 2011 Well, I took a crash course in Smarty, then I edited the Product::getProductProperties and added another field to the product assignment. That's all folks. Link to comment Share on other sites More sharing options...
vivek tripathi Posted August 1, 2011 Share Posted August 1, 2011 To display categories to which product belong in product list page replace following code in CategoryController.php if ($this->category->id != 1) { $nbProducts = $this->category->getProducts(NULL, NULL, NULL, $this->orderBy, $this->orderWay, true); $this->pagination((int)$nbProducts); self::$smarty->assign('nb_products', (int)$nbProducts); $cat_products = $this->category->getProducts((int)(self::$cookie->id_lang), (int)($this->p), (int)($this->n), $this->orderBy, $this->orderWay); } with the following code if ($this->category->id != 1) { $nbProducts = $this->category->getProducts(NULL, NULL, NULL, $this->orderBy, $this->orderWay, true); $this->pagination((int)$nbProducts); self::$smarty->assign('nb_products', (int)$nbProducts); $cat_products = $this->category->getProducts((int)(self::$cookie->id_lang), (int)($this->p), (int)($this->n), $this->orderBy, $this->orderWay); $catarray=array(); for($i=0;$i<count($cat_products);$i++){ $result = Db::getInstance()->ExecuteS('SELECT cp.id_category,cl.name FROM '._DB_PREFIX_.'category_product cp, '._DB_PREFIX_.'category_lang cl where cp.id_category=cl.id_category and cl.id_lang='.(int)(self::$cookie->id_lang).' and cp.id_product='.$cat_products[$i]['id_product'].' and cp.id_category!=1' ); $catarray[$cat_products[$i]['id_product']]=$result; } self::$smarty->assign('catarray',$catarray); } 2- Replace following line in theme->category.tpl {include file="$tpl_dir./product-list.tpl" products=$products} with the following line {include file="$tpl_dir./product-list.tpl" products=$products catarray=$catarray} 3- Replace following line in product-list.tpl <h3>{if isset($product.new) && $product.new == 1}<span class="new">{l s='New'}</span>{/if}<a href="{$product.link|escape:'htmlall':'UTF-8'}" title="{$product.name|escape:'htmlall':'UTF-8'}">{$product.name|truncate:35:'...'|escape:'htmlall':'UTF-8'}</a></h3> with following code {$x=$product.id_product} This product also in category<br/> {foreach from=$catarray.$x item=cat} <a href="{$link->getcategorylink($cat.id_category)|escape:'htmlall':'UTF-8'}" class="btn_bleu">{$cat.name}</a> <br/> {/foreach} <h3>{if isset($product.new) && $product.new == 1}<span class="new">{l s='New'}</span>{/if}<a href="{$product.link|escape:'htmlall':'UTF-8'}" title="{$product.name|escape:'htmlall':'UTF-8'}">{$product.name|truncate:35:'...'|escape:'htmlall':'UTF-8'}</a></h3> For shake of Convenience, I have also attached code.txt file have complete code code.txt Link to comment Share on other sites More sharing options...
dkfranc Posted August 2, 2011 Share Posted August 2, 2011 To display categories to which product belong in product list page replace following code in CategoryController.php if ($this->category->id != 1) { $nbProducts = $this->category->getProducts(NULL, NULL, NULL, $this->orderBy, $this->orderWay, true); $this->pagination((int)$nbProducts); self::$smarty->assign('nb_products', (int)$nbProducts); $cat_products = $this->category->getProducts((int)(self::$cookie->id_lang), (int)($this->p), (int)($this->n), $this->orderBy, $this->orderWay); } with the following code if ($this->category->id != 1) { $nbProducts = $this->category->getProducts(NULL, NULL, NULL, $this->orderBy, $this->orderWay, true); $this->pagination((int)$nbProducts); self::$smarty->assign('nb_products', (int)$nbProducts); $cat_products = $this->category->getProducts((int)(self::$cookie->id_lang), (int)($this->p), (int)($this->n), $this->orderBy, $this->orderWay); $catarray=array(); for($i=0;$i<count($cat_products);$i++){ $result = Db::getInstance()->ExecuteS('SELECT cp.id_category,cl.name FROM '._DB_PREFIX_.'category_product cp, '._DB_PREFIX_.'category_lang cl where cp.id_category=cl.id_category and cl.id_lang='.(int)(self::$cookie->id_lang).' and cp.id_product='.$cat_products[$i]['id_product'].' and cp.id_category!=1' ); $catarray[$cat_products[$i]['id_product']]=$result; } self::$smarty->assign('catarray',$catarray); } 2- Replace following line in theme->category.tpl {include file="$tpl_dir./product-list.tpl" products=$products} with the following line {include file="$tpl_dir./product-list.tpl" products=$products catarray=$catarray} 3- Replace following line in product-list.tpl <h3>{if isset($product.new) && $product.new == 1}<span class="new">{l s='New'}</span>{/if}<a href="{$product.link|escape:'htmlall':'UTF-8'}" title="{$product.name|escape:'htmlall':'UTF-8'}">{$product.name|truncate:35:'...'|escape:'htmlall':'UTF-8'}</a></h3> with following code {$x=$product.id_product} This product also in category<br/> {foreach from=$catarray.$x item=cat} <a href="{$link->getcategorylink($cat.id_category)|escape:'htmlall':'UTF-8'}" class="btn_bleu">{$cat.name}</a> <br/> {/foreach} <h3>{if isset($product.new) && $product.new == 1}<span class="new">{l s='New'}</span>{/if}<a href="{$product.link|escape:'htmlall':'UTF-8'}" title="{$product.name|escape:'htmlall':'UTF-8'}">{$product.name|truncate:35:'...'|escape:'htmlall':'UTF-8'}</a></h3> For shake of Convenience, I have also attached code.txt file have complete code i have replaced all your codes but no change.. can u pls tel me the complete usage clearly. Link to comment Share on other sites More sharing options...
vivek tripathi Posted August 2, 2011 Share Posted August 2, 2011 which version you are using ? This is tested on prestashop version 1.4+ Does it show any error if still problem persist send me those file in which you have made change so that I could help better way Link to comment Share on other sites More sharing options...
Muller Posted August 3, 2011 Author Share Posted August 3, 2011 which version you are using ? This is tested on prestashop version 1.4+ Does it show any error if still problem persist send me those file in which you have made change so that I could help better way The solution I posted as the first reply to this thread is a simpler solution I think. Link to comment Share on other sites More sharing options...
vivek tripathi Posted August 3, 2011 Share Posted August 3, 2011 Hi muler I think you have simpler solution would you please elaborate more. What changes you have made in the Product::getProductProperties and other files to achieve this I have acheive this functionality by my way but it would better approach to shorten code. which version you are using ? This is tested on prestashop version 1.4+ Does it show any error if still problem persist send me those file in which you have made change so that I could help better way The solution I posted as the first reply to this thread is a simpler solution I think. Link to comment Share on other sites More sharing options...
Muller Posted August 3, 2011 Author Share Posted August 3, 2011 I wrote this code in the Product class, in the getProductProperties function: $categories_ids = Product::getProductCategories($row['id_product']); $categories_names = array(); foreach ($categories_ids AS $id) { $category = new Category($id); $categories_names[] = $category->getName(); } $row['categories'] = $categories_names; Then I wrote the following code in product-list.tpl, wherever you want to display the categories: <!-- Display categories --> <span id="product_categories"> {foreach from=$product.categories item=category name=category} {$category} <br/> {/foreach} </span> 2 Link to comment Share on other sites More sharing options...
xmurph Posted August 28, 2011 Share Posted August 28, 2011 To display categories to which product belong in product list page replace following code in CategoryController.php if ($this->category->id != 1) { $nbProducts = $this->category->getProducts(NULL, NULL, NULL, $this->orderBy, $this->orderWay, true); $this->pagination((int)$nbProducts); self::$smarty->assign('nb_products', (int)$nbProducts); $cat_products = $this->category->getProducts((int)(self::$cookie->id_lang), (int)($this->p), (int)($this->n), $this->orderBy, $this->orderWay); } wonderful code, it works great for me, but a question: How can I display categories list not in product listing page but in product detail page?? with the following code if ($this->category->id != 1) { $nbProducts = $this->category->getProducts(NULL, NULL, NULL, $this->orderBy, $this->orderWay, true); $this->pagination((int)$nbProducts); self::$smarty->assign('nb_products', (int)$nbProducts); $cat_products = $this->category->getProducts((int)(self::$cookie->id_lang), (int)($this->p), (int)($this->n), $this->orderBy, $this->orderWay); $catarray=array(); for($i=0;$i<count($cat_products);$i++){ $result = Db::getInstance()->ExecuteS('SELECT cp.id_category,cl.name FROM '._DB_PREFIX_.'category_product cp, '._DB_PREFIX_.'category_lang cl where cp.id_category=cl.id_category and cl.id_lang='.(int)(self::$cookie->id_lang).' and cp.id_product='.$cat_products[$i]['id_product'].' and cp.id_category!=1' ); $catarray[$cat_products[$i]['id_product']]=$result; } self::$smarty->assign('catarray',$catarray); } 2- Replace following line in theme->category.tpl {include file="$tpl_dir./product-list.tpl" products=$products} with the following line {include file="$tpl_dir./product-list.tpl" products=$products catarray=$catarray} 3- Replace following line in product-list.tpl <h3>{if isset($product.new) && $product.new == 1}<span class="new">{l s='New'}</span>{/if}<a href="{$product.link|escape:'htmlall':'UTF-8'}" title="{$product.name|escape:'htmlall':'UTF-8'}">{$product.name|truncate:35:'...'|escape:'htmlall':'UTF-8'}</a></h3> with following code {$x=$product.id_product} This product also in category<br/> {foreach from=$catarray.$x item=cat} <a href="{$link->getcategorylink($cat.id_category)|escape:'htmlall':'UTF-8'}" class="btn_bleu">{$cat.name}</a> <br/> {/foreach} <h3>{if isset($product.new) && $product.new == 1}<span class="new">{l s='New'}</span>{/if}<a href="{$product.link|escape:'htmlall':'UTF-8'}" title="{$product.name|escape:'htmlall':'UTF-8'}">{$product.name|truncate:35:'...'|escape:'htmlall':'UTF-8'}</a></h3> For shake of Convenience, I have also attached code.txt file have complete code Link to comment Share on other sites More sharing options...
maxkdiv Posted August 30, 2011 Share Posted August 30, 2011 Good! How to make the categories diplayed linkable? Link to comment Share on other sites More sharing options...
Belinda999 Posted October 18, 2011 Share Posted October 18, 2011 Hi ,i tried code from this reply and its work. (after 3 hours, my litlle bug:D ) In PS 1.4.5.1 I wrote this code in the Product class, in the getProductProperties function: Link to comment Share on other sites More sharing options...
omar.bobo Posted October 25, 2011 Share Posted October 25, 2011 Hi everybody, this is what I'm looking for, but I'm working whith the release 1.4.5.1 and the code is quite different. Could anybody help me whith new version? Thanks in advance. Link to comment Share on other sites More sharing options...
dramdram Posted May 4, 2012 Share Posted May 4, 2012 Hi, I am looking to get those categories as links on my products pages ? Can anyone help me? Cheers, Link to comment Share on other sites More sharing options...
yks16 Posted June 8, 2012 Share Posted June 8, 2012 To display categories to which product belong in product list page replace following code in CategoryController.php if ($this->category->id != 1) { $nbProducts = $this->category->getProducts(NULL, NULL, NULL, $this->orderBy, $this->orderWay, true); $this->pagination((int)$nbProducts); self::$smarty->assign('nb_products', (int)$nbProducts); $cat_products = $this->category->getProducts((int)(self::$cookie->id_lang), (int)($this->p), (int)($this->n), $this->orderBy, $this->orderWay); } with the following code if ($this->category->id != 1) { $nbProducts = $this->category->getProducts(NULL, NULL, NULL, $this->orderBy, $this->orderWay, true); $this->pagination((int)$nbProducts); self::$smarty->assign('nb_products', (int)$nbProducts); $cat_products = $this->category->getProducts((int)(self::$cookie->id_lang), (int)($this->p), (int)($this->n), $this->orderBy, $this->orderWay); $catarray=array(); for($i=0;$i<count($cat_products);$i++){ $result = Db::getInstance()->ExecuteS('SELECT cp.id_category,cl.name FROM '._DB_PREFIX_.'category_product cp, '._DB_PREFIX_.'category_lang cl where cp.id_category=cl.id_category and cl.id_lang='.(int)(self::$cookie->id_lang).' and cp.id_product='.$cat_products[$i]['id_product'].' and cp.id_category!=1' ); $catarray[$cat_products[$i]['id_product']]=$result; } self::$smarty->assign('catarray',$catarray); } 2- Replace following line in theme->category.tpl {include file="$tpl_dir./product-list.tpl" products=$products} with the following line {include file="$tpl_dir./product-list.tpl" products=$products catarray=$catarray} 3- Replace following line in product-list.tpl <h3>{if isset($product.new) && $product.new == 1}<span class="new">{l s='New'}</span>{/if}<a href="{$product.link|escape:'htmlall':'UTF-8'}" title="{$product.name|escape:'htmlall':'UTF-8'}">{$product.name|truncate:35:'...'|escape:'htmlall':'UTF-8'}</a></h3> with following code {$x=$product.id_product} This product also in category<br/> {foreach from=$catarray.$x item=cat} <a href="{$link->getcategorylink($cat.id_category)|escape:'htmlall':'UTF-8'}" class="btn_bleu">{$cat.name}</a> <br/> {/foreach} <h3>{if isset($product.new) && $product.new == 1}<span class="new">{l s='New'}</span>{/if}<a href="{$product.link|escape:'htmlall':'UTF-8'}" title="{$product.name|escape:'htmlall':'UTF-8'}">{$product.name|truncate:35:'...'|escape:'htmlall':'UTF-8'}</a></h3> For shake of Convenience, I have also attached code.txt file have complete code I'm working whith the release 1.4.6.2 and the code is quite different. In step 1, i have different code. My complete code in CategoryController.php is: {* * 2007-2011 PrestaShop * * NOTICE OF LICENSE * * This source file is subject to the Academic Free License (AFL 3.0) * that is bundled with this package in the file LICENSE.txt. * It is also available through the world-wide-web at this URL: * http://opensource.org/licenses/afl-3.0.php * If you did not receive a copy of the license and are unable to * obtain it through the world-wide-web, please send an email * to [email protected] so we can send you a copy immediately. * * DISCLAIMER * * Do not edit or add to this file if you wish to upgrade PrestaShop to newer * versions in the future. If you wish to customize PrestaShop for your * needs please refer to http://www.prestashop.com for more information. * * @author PrestaShop SA <[email protected]> * @copyright 2007-2011 PrestaShop SA * @version Release: $Revision: 8290 $ * @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0) * International Registered Trademark & Property of PrestaShop SA *}[/i][/size][/font][/color] [color=#7B7B7B][font=Arial, Helvetica, sans-serif][size=3][i]{if isset($products)} <!-- Products list --> <ul id="product_list" class="clear"> {foreach from=$products item=product name=products} <li class="ajax_block_product {if $smarty.foreach.products.first}first_item{elseif $smarty.foreach.products.last}last_item{/if} {if $smarty.foreach.products.index % 2}alternate_item{else}item{/if} clearfix"> <div class="center_block"> <a href="{$product.link|escape:'htmlall':'UTF-8'}" class="product_img_link" title="{$product.name|escape:'htmlall':'UTF-8'}"><img src="{$link->getImageLink($product.link_rewrite, $product.id_image, 'home')}" alt="{$product.legend|escape:'htmlall':'UTF-8'}" {if isset($homeSize)} width="{$homeSize.width}" height="{$homeSize.height}"{/if} /></a> <h3><!--{if isset($product.new) && $product.new == 1}<span class="new">{l s='New'}</span>{/if}--><a href="{$product.link|escape:'htmlall':'UTF-8'}" title="{$product.name|escape:'htmlall':'UTF-8'}">{$product.name|truncate:70:'...'|escape:'htmlall':'UTF-8'}</a></h3> <h4>{strip} {$category->name|escape:'htmlall':'UTF-8'} {$categoryNameComplement|escape:'htmlall':'UTF-8'}[/i][/size][/font][/color] [color=#7B7B7B][font=Arial, Helvetica, sans-serif][size=3][i]{/strip}</h4> <p class="product_desc"><a href="{$product.link|escape:'htmlall':'UTF-8'}" title="{$product.description_short|truncate:360:'...'|strip_tags:'UTF-8'|escape:'htmlall':'UTF-8'}">{$product.description_short|truncate:360:'...'|strip_tags:'UTF-8'}</a></p> </div> <div class="right_block"> {if isset($product.on_sale) && $product.on_sale && isset($product.show_price) && $product.show_price && !$PS_CATALOG_MODE}<span class="on_sale">{l s='On sale!'}</span> {elseif isset($product.reduction) && $product.reduction && isset($product.show_price) && $product.show_price && !$PS_CATALOG_MODE}<!--<span class="discount">{l s='Reduced price!'}</span>{/if}--> {if isset($product.online_only) && $product.online_only}<span class="online_only">{l s='Online only!'}</span>{/if} {if (!$PS_CATALOG_MODE AND ((isset($product.show_price) && $product.show_price) || (isset($product.available_for_order) && $product.available_for_order)))} <div> {if isset($product.show_price) && $product.show_price && !isset($restricted_country_mode)}<span class="price" style="display: inline;">{if !$priceDisplay}{convertPrice price=$product.price}{else}{convertPrice price=$product.price_tax_exc}{/if}</span><br />{/if} {if isset($product.available_for_order) && $product.available_for_order && !isset($restricted_country_mode)}<span class="availability">{if ($product.allow_oosp || $product.quantity > 0)}{l s='Available'}{elseif (isset($product.quantity_all_versions) && $product.quantity_all_versions > 0)}{l s='Product available with different options'}{else}{l s='Out of stock'}{/if}</span>{/if} </div> {/if} {if ($product.id_product_attribute == 0 || (isset($add_prod_display) && ($add_prod_display == 1))) && $product.available_for_order && !isset($restricted_country_mode) && $product.minimal_quantity <= 1 && $product.customizable != 2 && !$PS_CATALOG_MODE} {if ($product.allow_oosp || $product.quantity > 0)} <a class="button ajax_add_to_cart_button exclusive" rel="ajax_id_product_{$product.id_product|intval}" href="{$link->getPageLink('cart.php')}?add&id_product={$product.id_product|intval}{if isset($static_token)}&token={$static_token}{/if}" title="{l s='Add to cart'}">{l s='Add to cart'}</a> {else} <span class="exclusive">{l s='Add to cart'}</span> {/if} {/if} <a class="button" href="{$product.link|escape:'htmlall':'UTF-8'}" title="{l s='View'}">{l s='View'}</a> {if isset($comparator_max_item) && $comparator_max_item} <p class="compare"><input type="checkbox" class="comparator" id="comparator_item_{$product.id_product}" value="comparator_item_{$product.id_product}" {if isset($compareProducts) && in_array($product.id_product, $compareProducts)}checked{/if}/> <label for="comparator_item_{$product.id_product}">{l s='Select to compare'}</label></p> {/if} </div> </li> {/foreach} </ul> <!-- /Products list --> {/if} Please help me. Link to comment Share on other sites More sharing options...
isoler Posted September 22, 2014 Share Posted September 22, 2014 I wrote this code in the Product class, in the getProductProperties function: $categories_ids = Product::getProductCategories($row['id_product']); $categories_names = array(); foreach ($categories_ids AS $id) { $category = new Category($id); $categories_names[] = $category->getName(); } $row['categories'] = $categories_names; Then I wrote the following code in product-list.tpl, wherever you want to display the categories: <!-- Display categories --> <span id="product_categories"> {foreach from=$product.categories item=category name=category} {$category} <br/> {/foreach} </span> Working on 1.5.6.2 Many thanks!! Link to comment Share on other sites More sharing options...
Programming-Twit Posted June 30, 2015 Share Posted June 30, 2015 How to get link to every category in Muller's code solution ? Link to comment Share on other sites More sharing options...
Vilas Posted July 6, 2015 Share Posted July 6, 2015 How to display like below. Mobile Phone Sony x1 mobile Sony x2 mobile Micromax Mobile Computer Cabinet Circle 820 Cabinet Zebronics M1 Cabinet Printer HP 1115 Printer HP 3545 Printer All should have randomised product feature. With view product from that category feature. Anyone have any idea about it? Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now