在Prestashop 1.7中,如何在主菜单模块中显示类别拇指

时间:2018-06-06 13:15:09

标签: module menu prestashop categories

我无法在主菜单模块(ps_mainmenu)中设置显示类别拇指。我在Prestashop 1.6中管理它,但模块已经改变。

在Prestashop 1.7管理员中,您可以添加类别缩略图。

这是一张图片,展示了我尝试实现的目标:Category thumb above category labels in main menu

致以最诚挚的问候,

昆汀

2 个答案:

答案 0 :(得分:0)

您需要修改菜单的模板文件,最好在主题themes/your_theme/modules/ps_mainmenu/ps_mainmenu.tpl中进行修改,然后添加相似的代码部分

{if $node.type == 'category'}
  {if isset($node.image_urls) && $node.image_urls}
    {foreach from=$node.image_urls item='thumb'}
      <img src="{$thumb}" alt="" />
    {/foreach}
  {/if}
{/if}

{foreach from=$nodes item=node}内 就我而言,它看起来像这样

{assign var=_counter value=0}
{function name="menu" nodes=[] depth=0 parent=null}
    {if $nodes|count}
      <ul class="top-menu" {if $depth == 0}id="top-menu"{/if} data-depth="{$depth}">
        {foreach from=$nodes item=node}
          <li class="{$node.type}{if $node.current} current {/if}" id="{$node.page_identifier}">
            {if $node.type == 'category'}
              {if isset($node.image_urls) && $node.image_urls}
                {foreach from=$node.image_urls item='thumb'}
                  <img src="{$thumb}" alt="" />
                {/foreach}
              {/if}
            {/if}
            {assign var=_counter value=$_counter+1}
              <a
                class="{if $depth >= 0}dropdown-item{/if}{if $depth === 1} dropdown-submenu{/if}"
                href="{$node.url}" data-depth="{$depth}"
                {if $node.open_in_new_window} target="_blank" {/if}
              >
                {if $node.children|count}
                  {* Cannot use page identifier as we can have the same page several times *}
                  {assign var=_expand_id value=10|mt_rand:100000}
                  <span class="float-xs-right hidden-md-up">
                    <span data-target="#top_sub_menu_{$_expand_id}" data-toggle="collapse" class="navbar-toggler collapse-icons">
                      <i class="material-icons add">&#xE313;</i>
                      <i class="material-icons remove">&#xE316;</i>
                    </span>
                  </span>
                {/if}
                {$node.label}
              </a>
              {if $node.children|count}
              <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}
              </div>
              {/if}
            </li>
        {/foreach}
      </ul>
    {/if}
  {/function}

<div class="menu js-top-menu position-static hidden-sm-down" id="_desktop_top_menu">
    {menu nodes=$menu.children}
    <div class="clearfix"></div>
</div>

它起作用但对我来说不是绝对可预测的。因为它仅将图像添加到具有子类别的类别。因此,如果可以,则可以将其置于此状态,但如果没有,并且您希望显示所有类别的图像,则需要修改模块控制器文件。 转到核心模块文件夹modules/ps_mainmenu/ps_mainmenu.php内的模块并修改方法generateCategoriesMenu。删除代码

$files = scandir(_PS_CAT_IMG_DIR_);

if (count(preg_grep('/^'.$category['id_category'].'-([0-9])?_thumb.jpg/i', $files)) > 0) {
  foreach ($files as $file) {
    if (preg_match('/^'.$category['id_category'].'-([0-9])?_thumb.jpg/i', $file) === 1) {
      $image_url = $this->context->link->getMediaLink(_THEME_CAT_DIR_.$file);
      $node['image_urls'][] = $image_url;
    }
  }
}
来自条件if (isset($category['children']) && !empty($category['children']))

并把它准确地放在条件之前。

答案 1 :(得分:0)

我回到我的项目并升级了Prestashop,然后我的菜单消失了,我忘记了这个答案。再次感谢Alexander Grosul。

这里有什么方法可以覆盖PS_MainMenu并使类别拇指显示出来,即使类别没有子代也是如此。

  1. 转到覆盖/模块/ ps_mainmenu
  2. 您可以从原始模块中复制文件ps_mainenu.php并清理它,也可以只创建一个新的php文件
  3. 新文件:

    class Ps_MainMenuOverride extends Ps_MainMenu {
    
    protected function generateCategoriesMenu($categories, $is_children = 0)
    {
    $nodes = [];
    
    foreach ($categories as $key => $category) {
        $node = $this->makeNode([]);
    
        if ($category['level_depth'] > 1) {
            $cat = new Category($category['id_category']);
            $link = $cat->getLink();
        } else {
            $link = $this->context->link->getPageLink('index');
        }
    
        $node['url'] = $link;
        $node['type'] = 'category';
        $node['page_identifier'] = 'category-' . $category['id_category'];
    
        /* Whenever a category is not active we shouldnt display it to customer */
        if ((bool)$category['active'] === false) {
            continue;
        }
    
        $current = $this->page_name == 'category' && (int)Tools::getValue('id_category') == (int)$category['id_category'];
        $node['current'] = $current;
        $node['label']   = $category['name'];
        $node['image_urls']  = [];
    
        $files = scandir(_PS_CAT_IMG_DIR_);
    
        if (count(preg_grep('/^'.$category['id_category'].'-([0-9])?_thumb.jpg/i', $files)) > 0) {
            foreach ($files as $file) {
                if (preg_match('/^'.$category['id_category'].'-([0-9])?_thumb.jpg/i', $file) === 1) {
                    $image_url = $this->context->link->getMediaLink(_THEME_CAT_DIR_.$file);
                    $node['image_urls'][] = $image_url;
                }
            }
        }
    
        if (isset($category['children']) && !empty($category['children'])) {
            $node['children'] = $this->generateCategoriesMenu($category['children'], 1);
        }
    
        $nodes[] = $node;
    }
    
    return $nodes;
    }
    }
    
  4. 确保删除缓存或强制编译

这应该可以解决问题,并且即使在更新后也可以保留。