5

I'm create new attribute is small image for category.In admin i have save image for category-1 then i get this image in menu category but image exist for all category.Although in admin i just save image for category-1.Same this image red box is category save image in admin.pls see help me. I Will add image in file custom Topmenu.php `

 $objectManager = \Magento\Framework\App\objectManager::getInstance();
        $cate = $objectManager->get('\Magento\Catalog\Model\Category');
        $id = substr($child->getId(),14);            
        $item  = $cate->load($id);
   $image = $id !='' ? '<img src="'.$item->getSmallImageUrl().'" width="30" height="30">' :'';            
        $outermostClassCode = '';
        $outermostClass = $menuTree->getOutermostClass();

        if ($childLevel == 0 && $outermostClass) {
            $outermostClassCode = ' class="' . $outermostClass . '" ';
            $child->setClass($outermostClass);
        }

        if (count($colBrakes) && $colBrakes[$counter]['colbrake']) {
            $html .= '</ul></li><li class="column"><ul>';
        }

        $html .= '<li ' . $this->_getRenderedMenuItemAttributes($child) . '>';
        $html .= '<a href="' . $child->getUrl() . '" ' . $outermostClassCode . '>'.$image.'<span>' . $this->escapeHtml(
            $child->getName()
        ) . '</span></a>' . $this->_addSubMenu(
            $child,
            $childLevel,
            $childrenWrapClass,
            $limit
        ) . '</li>';
        $itemPosition++;
        $counter++;`

enter image description here

0

2 Answers 2

1

Replace

$image = $id !='' ? '<img src="'.$item->getSmallImageUrl().'" width="30" height="30">' :'';   

with

 $image = $id !='' ? '<img src="'.$item->getImageUrl().'" width="30" height="30">' :'';
-1

You should change

$cate = $objectManager->get('\Magento\Catalog\Model\Category'); to  $cate = $objectManager->create('\Magento\Catalog\Model\Category');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.