Jump to content

[Solved] Display categories to which product belongs in product listing page


Muller

Recommended Posts

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

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

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

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

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

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>

  • Like 2
Link to comment
Share on other sites

  • 4 weeks later...

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

  • 1 month later...
  • 6 months later...
  • 1 month later...

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

  • 2 years later...

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

  • 9 months later...

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

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...