Jump to content

Presta 1.7 - How to add subcategories images in the top menu ?


Recommended Posts

Hello,

 

In my menu, I would like to have the images in place of the brand name.

 

Menu:

MainMenu.png

 

 

Each brands is a category, the images are uploaded via the admin interface and present in the directory img / c /

 

I wanted to recover the url of images but it does not pass in the template…

 

Here is the debug result of the mainmenu template:

Smarty_Variable Object (3)
->value = Array (9)
  type => "root"
  label => null
  url => ""
  children => Array (5)
    0 => Array (9)
      type => "category"
      label => "E-JUICES"
      url => "http://vape.dev/index.php?id_category..."
      children => Array (1)
        0 => Array (9)
          type => "category"
          label => "Marques"
          url => "http://vape.dev/index.php?id_category..."
          children => Array (11)
            0 => Array (9)
              type => "category"
              label => "Liquideo"
              url => "http://vape.dev/index.php?id_category..."
              children => Array (0)
              open_in_new_window => false
              image_urls => Array (0)
              page_identifier => "category-18"
              current => false
              depth => 3
            1 => Array (9)
              type => "category"
              label => "3Bubbles"
              url => "http://vape.dev/index.php?id_category..."
              children => Array (0)
              open_in_new_window => false
              image_urls => Array (0)
              page_identifier => "category-21"
              current => false
              depth => 3
            2 => Array (9)
              type => "category"
              label => "Aj Vape"
              url => "http://vape.dev/index.php?id_category..."
              children => Array (0)
              open_in_new_window => false
              image_urls => Array (0)
              page_identifier => "category-22"
              current => false
              depth => 3
            3 => Array (9)
              type => "category"
              label => "Bad Joos"
              url => "http://vape.dev/index.php?id_category..."
              children => Array (0)
              open_in_new_window => false
              image_urls => Array (0)
              page_identifier => "category-23"
              current => false
              depth => 3
            4 => Array (9)
              type => "category"
              label => "Cake Monster"
              url => "http://vape.dev/index.php?id_category..."
              children => Array (0)
              open_in_new_window => false
              image_urls => Array (0)
              page_identifier => "category-24"
              current => false
              depth => 3
            5 => Array (9)
              type => "category"
              label => "Dark Market"
              url => "http://vape.dev/index.php?id_category..."
              children => Array (0)
              open_in_new_window => false
              image_urls => Array (0)
              page_identifier => "category-25"
              current => false
              depth => 3
            6 => Array (9)
              type => "category"
              label => "Entourage Vapor"
              url => "http://vape.dev/index.php?id_category..."
              children => Array (0)
              open_in_new_window => false
              image_urls => Array (0)
              page_identifier => "category-26"
              current => false
              depth => 3
            7 => Array (9)
              type => "category"
              label => "Fat Boy"
              url => "http://vape.dev/index.php?id_category..."
              children => Array (0)
              open_in_new_window => false
              image_urls => Array (0)
              page_identifier => "category-27"
              current => false
              depth => 3
            8 => Array (9)
              type => "category"
              label => "Green Vapes"
              url => "http://vape.dev/index.php?id_category..."
              children => Array (0)
              open_in_new_window => false
              image_urls => Array (0)
              page_identifier => "category-28"
              current => false
              depth => 3
            9 => Array (9)
              type => "category"
              label => "Hype Juice"
              url => "http://vape.dev/index.php?id_category..."
              children => Array (0)
              open_in_new_window => false
              image_urls => Array (0)
              page_identifier => "category-29"
              current => false
              depth => 3
            10 => Array (9)
              type => "category"
              label => "Illuminati Vapor"
              url => "http://vape.dev/index.php?id_category..."
              children => Array (0)
              open_in_new_window => false
              image_urls => Array (0)
              page_identifier => "category-30"
              current => false
              depth => 3
          open_in_new_window => false
          image_urls => Array (0)
          page_identifier => "category-17"
          current => false
          depth => 2
      open_in_new_window => false
      image_urls => Array (0)
      page_identifier => "category-12"
      current => false
      depth => 1
    1 => Array (9)
      type => "category"
      label => "DIY"
      url => "http://vape.dev/index.php?id_category..."
      children => Array (0)
      open_in_new_window => false
      image_urls => Array (0)
      page_identifier => "category-13"
      current => false
      depth => 1
    2 => Array (9)
      type => "category"
      label => "MODS"
      url => "http://vape.dev/index.php?id_category..."
      children => Array (0)
      open_in_new_window => false
      image_urls => Array (0)
      page_identifier => "category-14"
      current => false
      depth => 1
    3 => Array (9)
      type => "category"
      label => "CLEAROMISEURS"
      url => "http://vape.dev/index.php?id_category..."
      children => Array (0)
      open_in_new_window => false
      image_urls => Array (0)
      page_identifier => "category-15"
      current => false
      depth => 1
    4 => Array (9)
      type => "category"
      label => "RECONSTRUCTIBLES"
      url => "http://vape.dev/index.php?id_category..."
      children => Array (0)
      open_in_new_window => false
      image_urls => Array (0)
      page_identifier => "category-16"
      current => false
      depth => 1
  open_in_new_window => false
  image_urls => Array (0)
  page_identifier => null
  depth => 0
  current => false
->nocache = false
->scope = "module:ps_mainmenu/ps_mainmenu.tpl"

Thanks !

Link to comment
Share on other sites

I find the solution, moving a condition in the method (ps_mainmenu.php)

    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']  = [];

            if (isset($category['children']) && !empty($category['children'])) {

                $node['children'] = $this->generateCategoriesMenu($category['children'], 1);
                /*
                $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;
                        }
                    }
                }
                */
            }

            $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;
                    }
                }
            }

            $nodes[] = $node;
        }
        return $nodes;
    }

Link to comment
Share on other sites

  • 10 months later...
  • 3 weeks later...

Hello,

I need to show subcategory images above the name of my subcategories in my submenu. I insert my images back end but they do not appear in front-end.

I tried the code above but it does not work. Someone has a solution for this problem. I am on prestashop 1.7.3.3

Thanks for the help :)

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