• Basisthema
  • Tweede menu item
    • Sub menu item
Inloggen
Home » Volgorde (sub)menu items Magento menu

Volgorde (sub)menu items Magento menu

Volgorde (sub)menu items Magento menu

Voeg de eerste stuk snippet toe aan app/code/Gladior/Menu/Block/Menu.php
Vervang de loop van de submenu's in het menu door de tweede snippet in app/code/Gladior/Menu/frontend/templates/main-menu.phtml

PHP Code

php
    /**
     * Get sorted subcategories for a parent category
     * @param int|string $parentId
     * @return \Magento\Catalog\Model\ResourceModel\Category\Collection
     */
    public function getSortedSubcategories($parentId)
    {
        $currentStore = $this->_storeManager->getStore();
        $subcategories = $this->_categoryCollection->create()
            ->addAttributeToSelect('*')
            ->addFieldToFilter('is_active', 1)
            ->addFieldToFilter('include_in_menu', 1)
            ->addAttributeToFilter('parent_id', $parentId)
            ->setOrder('position', 'ASC')
            ->setStore($currentStore);
        
        return $subcategories;
    }





<?php foreach ($block->getSortedSubcategories($category->getId()) as $subcategory) :
                $rawUrl = $block->getUrl($subcategory->getData('url_path'));
                $url = trim($rawUrl, '/');
            ?>
                <li class="menu-item sub-menu-item<?php echo $subcategory->hasChildren() ? ' has-children' : ' no-children';  ?>">
                    <a href="<?php echo $url; ?>" class="item-link sub-item-link" id="category-<?php echo $subcategory->getId(); ?>">
                        <span class="item-name sub-item-name"><?php echo $subcategory->getName(); ?></span>
                    </a>
                    <?php if ($subcategory->hasChildren()) : ?>
                        <ul class="sub-sub-menu">
                            <div class="menu-step-back mobile"></div>
                            <?php foreach ($block->getSortedSubcategories($subcategory->getId()) as $subsubcategory) :
                                $rawUrl = $block->getUrl($subsubcategory->getData('url_path'));
                                $url = trim($rawUrl, '/');
                            ?>
                                <li class="menu-item sub-sub-menu-item">
                                    <a href="<?php echo $url; ?>" class="item-link sub-sub-item-link">
                                        <span class="item-name sub-sub-item-name"><?php echo $subsubcategory->getName(); ?></span>
                                    </a>
                                </li>
                            <?php endforeach; ?>
                        </ul>
                        <div class="child-toggle mobile"></div>
                    <?php endif; ?>
                </li>
            <?php endforeach; ?>
Bekijk Snippet

Door Gladior

  • Facebook
  • Linkedin
  • Mail
  • Instagram

Contact

service@gladior.com

Overige informatie

Basisthema voor WordPress