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

Module 在Prestashop 1.7中,如何在主菜单模块中显示类别,module,menu,prestashop,categories,Module,Menu,Prestashop,Categories,我无法在主菜单模块(ps_mainmenu)中显示类别拇指。我在Prestashop 1.6中管理它,但模块已经更改 在Prestashop 1.7管理员中,您可以添加一个类别缩略图图像 下面是一张图片,展示了我试图实现的目标: 致以最良好的祝愿 Quentin您需要修改菜单的模板文件,最好在themethemes/your_theme/modules/ps_main menu/ps_main menu.tpl中进行修改,并在其中添加类似的代码部分 {if $node.type == 'cate

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

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

下面是一张图片,展示了我试图实现的目标:

致以最良好的祝愿


Quentin

您需要修改菜单的模板文件,最好在theme
themes/your_theme/modules/ps_main menu/ps_main menu.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}
这是可行的,但对我来说不是绝对可以预测的。因为它只将图像添加到具有子类别的类别中。所以,如果可以,您可以将其保持在此状态,但如果不可以,并且您希望显示所有类别的图像,则需要修改模块控制器文件。 转到核心模块文件夹中的模块
modules/ps_main menu/ps_main menu.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(设置($category['children'])和&!empty($category['children']))

然后把它放在条件之前。

我要回到我的项目中,把我的前置菜单拉长了,然后我的菜单消失了,我忘记了这个答案。再次感谢亚历山大·格罗苏尔

在这里,如何覆盖PS_主菜单并使类别拇指显示,即使类别没有子项

  • 转到覆盖/模块/ps_主菜单
  • 您可以从原始模块复制文件ps_mainus.php并清理它,或者创建一个新的php文件
  • 新文件:

    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;
    }
    }
    
  • 确保删除缓存或强制编译


  • 这应该可以做到,即使在更新后也会保持不变。

    谢谢!这似乎奏效了。我读到在1.7中我们不能再做重写了。因为这是在核心模块内完成的,这意味着它不会持续更新?你知道我怎么不能在下拉列表中显示子类别(深度2)吗?是的,最后一部分是在核心模块中完成的(在我的例子中)。但我相信,您可以像以前一样轻松地重写此部分,而不会产生任何后果使用{if$nodes | count&$depth<2}而不是{if$nodes | count},并且您不会看到任何深度超过2的子类别!很好,现在我只有想要的类别,标签上面的图片。你应该喝杯好咖啡或啤酒:)谢谢
    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;
    }
    }