Question

I have created a custom category attribute and it saves correctly in the database.

I am now trying to add this attribute's value to the topmenu. So I have created Topmenu.php in my module (m4tthg0_catcolor) so I can override the _getMenuItemClasses. I have tried several ways to get the attribute value but it always return NULL:

namespace m4tthg0\catcolor\Block;
use Magento\Framework\DataObject\IdentityInterface;
use Magento\Framework\View\Element\Template;
use Magento\Framework\Data\TreeFactory;
use Magento\Framework\Data\Tree\Node;
use Magento\Framework\Data\Tree\NodeFactory;
/**
* Html page top menu block
*/

class Topmenu extends \Magento\Theme\Block\Html\Topmenu


{    

protected function _getMenuItemClasses(\Magento\Framework\Data\Tree\Node $item)
{
    $classes = [];

    $classes[] = 'level' . $item->getLevel();
    $classes[] = $item->getPositionClass();

    if ($item->getIsFirst()) {
        $classes[] = 'first';
    }

    if ($item->getIsActive()) {
        $classes[] = 'active';
    } elseif ($item->getHasActive()) {
        $classes[] = 'has-active';
    }

    if ($item->getIsLast()) {
        $classes[] = 'last';
    }

    if ($item->getClass()) {
        $classes[] = $item->getClass();
    }

    if ($item->hasChildren()) {
        $classes[] = 'parent';
    }

    $classes [] =$item->getData('color_menu');

    return $classes;
 }
}

How would you do that ?

Magento version: 2.1CE

Was it helpful?

Solution

I know this is an old question but I just worked on a solution for this. So if anyone else is having this issue...

Have a look at the

Magento\Catalog\Plugin\Block\Topmenu

This is where Magento fills category tree for the menu.

Since this one is a plugin I decided to disable it and add a new plugin with same content (+my modifications) in my module.

Below should go in

m4tthg0\Catcolor\etc\di.xml

Disable the Magento plugin first.

<type name="Magento\Theme\Block\Html\Topmenu">
    <plugin name="catalogTopmenu" disabled="true" />
</type>

Then add your plugin

<type name="Magento\Theme\Block\Html\Topmenu">
    <plugin name="catalogTopmenuRewrite" type="m4tthg0\Catcolor\Plugin\Block\Topmenu"/>
</type>

Then create the file

m4tthg0\Catcolor\Plugin\Block\Topmenu.php

and copy everything from

Magento\Catalog\Plugin\Block\Topmenu.php

Make sure to adjust the namespace in your file.

Now let's add additional content from category collection. Have a look at the method

getCategoryTree($storeId, $rootId)

see how it selects the "name" to be added to tree data. Similarly you can add your custom attribute.

Inside getCategoryTree(),

$collection->addAttributeToSelect('custom_attribute');

Then have a look at the method

getCategoryAsArray($category, $currentCategory)

You can add your selected custom attribute here to tree data. Just add below array item.

'custom_attribute' => $category->getData('custom_attribute')

That's it!!

Go to your Topmenu.php (m4tthg0\catcolor\Block\Topmenu.php) and check inside Node($item) data. Your custom attribute value should be there.

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top