Jump to content

Tutorial: displaying list of categories and products in any tpl


Amazzing

Recommended Posts

I needed to display a list of categories and products in one of my submenu dropdowns.

It is not a regular PS feature, so I tried to look fo a tutorial of how to do that.

 

I saw some requests, but i couldn't find a solution, that worked for me.

 

So, I spent some time digging into the code and made all modifications by myself.

 

After that I decided to post a tutorial here, in case anybody else needs to perform same modifications.

 

This tutorial is just about getting the lists of categories and products on your page. 

It doesn't include CSS styling and JS modifications.

tested on PS version 1.5.5.0.

 

1) Modifications for modules/blocktopmenu/blocktopmenu.php:

(you can use any other php file, bdepending on what tpl you will modify)

Find public function hookDisplayTop($param)...

and insert the below code block before this line:

$html = $this->display(__FILE__, 'blocktopmenu.tpl', $this->getCacheId()); 

In 1.5.5.0 it is line 740.

//submenu added code									
		$lang = (int)Context::getContext()->language->id;
		$cats = Category::getCategories($lang);				
		$this->smarty->assign('categories', $cats);		
			
		$sqlprod =	'SELECT
					ps_product_lang.`name`,
					ps_product_lang.`link_rewrite`,
					ps_product_lang.`id_product`,
					ps_category_product.`id_category`,
					ps_product_shop.`active`
					FROM
					`ps_product_lang`
					LEFT JOIN 
					`ps_category_product`
					ON ps_product_lang.`id_product`= ps_category_product.`id_product`
					LEFT JOIN 
					`ps_product_shop`
					ON ps_product_lang.`id_product`= ps_product_shop.`id_product`
					WHERE
					`id_lang` = '.$lang.'
					';		
		
		$prods = Db::getInstance()->executeS($sqlprod);		
		$this->smarty->assign('products', $prods);		
		//end added code

What we did here: 

-We got all the categories using predefined PS function and send this array to smarty. 
-We got all the products using our own query. I didn't use the predefined fucntion getProducts() here, because it returns an array that doesn't include data about multiple categories, and it has lots of fields that we dont' need here. 

Note that query is made for tables with ps_ prefix . If you have another prefix in you database tables, just change it in this here. 

 

So now our tpl file has the array of products and categories.

It is time to display them.

 

2) Modifications for /themes/THEMENAME/modules/blocktopmenu/blocktopmenu.tpl:

Just insert this code where you need it, basing on your page layout.

<div>			
		{*3rd category is what we need here*}
		{foreach from=$categories.3 item=category}
			{$catid=$category.infos.id_category}
			{if $category.infos.active == 1}
			<div class="catcol catid{$catid}">
				<a href="{$link->getCategoryLink($catid, $category.infos.link_rewrite)|escape:'htmlall':'UTF-8'}" class="topcatname">
					{$category.infos.name|escape:'htmlall':'UTF-8'|truncate:50:'...'}:
				</a>
				<ul>		
				{foreach from=$products item=product}
					{if $product.id_category == $catid && $product.active == 1}
						<li>
							<a href="{$link->getProductLink($product.id_product, $product.link_rewrite)|escape:'htmlall':'UTF-8'}">
							{$product.name|escape:'htmlall':'UTF-8'|truncate:50:'...'}
							</a>
						</li>
					{/if}
				{/foreach}
				</ul>
			</div>
			{/if}
		{/foreach}
</div>	

What we did here:

We just processed arrays and constructed divs with lists of products for each category.

Both categories and products here have links to their pages.

Note that I needed just subcategories of 3-rd category, so it was defined here: ...from=$categories.3...

 

This code works only for categories of first level.

If you need to display category tree with nested subcategories, you will need to do some additional modifications.

Theoretically you have all required data for that in {$categories} array

 

If you have any suggestions of how to optimize these modifications, you're welcome!

May be there is another predefined function of getting the list of products, or smth else...

 

Have a good day!

Edited by Amazzing (see edit history)
  • Like 4
Link to comment
Share on other sites

  • 3 years later...
  • 1 year later...

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...