In Prestashop, we can add images (thumbs) to subcategories menu
if ((int)$category['level_depth'] > 1 && !$is_children) {
$files = scandir(_PS_CAT_IMG_DIR_);
if (count(preg_grep('/^'.$category['id_category'].'-([0-9])?_thumb.jpg/i', $files)) > 0) {
$html .= '<li class="category-thumbnail">';
foreach ($files as $file) {
if (preg_match('/^'.$category['id_category'].'-([0-9])?_thumb.jpg/i', $file) === 1) {
$html .= '<div><img src="'.$this->context->link->getMediaLink(_THEME_CAT_DIR_.$file)
.'" alt="'.Tools::SafeOutput($category['name']).'" title="'
.Tools::SafeOutput($category['name']).'" class="imgm" /></div>';
}
}
$html .= '</li>';
}
}
Unfortelly, this code didn't work for me, images are not showing, although i set the thumbs images for subcategories. Any help?
This is my solution, it's working with me
protected function generateCategoriesMenu($categories, $is_children = 0)
{
$html = '';
foreach ($categories as $key => $category) {
if ($category['level_depth'] > 1) {
$cat = new Category($category['id_category']);
$link = Tools::HtmlEntitiesUTF8($cat->getLink());
} else {
$link = $this->context->link->getPageLink('index');
}
/* Whenever a category is not active we shouldnt display it to customer */
if ((bool)$category['active'] === false) {
continue;
}
$html .= '<li'.(($this->page_name == 'category'
&& (int)Tools::getValue('id_category') == (int)$category['id_category']) ? ' class="sfHoverForce"' : '').'>';
$html .= '<a href="'.$link.'" title="'.$category['name'].'">';
if($category['level_depth'] == '3' AND Tools::file_exists_cache(_PS_CAT_IMG_DIR_.(int)$category['id_category'].'_thumb.jpg'))
$html .= '<img src="/img/c/'.(int)$category['id_category'].'_thumb.jpg'.'" class="imgm" height="30" /><br>';
$html .= $category['name'];
$html .='</a>';
if (isset($category['children']) && !empty($category['children'])) {
$html .= '<ul>';
$html .= $this->generateCategoriesMenu($category['children'], 1);
$html.= '<li class="sfHoverForce">'.$category['promo_right'].'</li>';
$html .= '</ul>';
}
$html .= '</li>';
}
return $html;
}
I want to share to you my solution on Prestashop 1.7 by editing ps_mainmenu template file :
<div {if $depth === 0} class="popover sub-menu js-sub-menu collapse"{else} class="collapse"{/if} id="top_sub_menu_{$_expand_id}">
{menu nodes=$node.children depth=$node.depth parent=$node}
{if $node.image_urls}
{foreach from=$node.children depth item=mychild}
{foreach from=$mychild.image_urls item=image_url}
<img src="{$image_url}" title="" alt="" />
{/foreach}
{/foreach}
{/if}
Take a view on my article here :
https://www.prestasoo.com/blog/how-to-show-subcategories-images-in-prestashop-s-top-menu.html