Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Magento get category attribute value in Topmenu.php

I have created a custom category attribute. Now i need to access it's value in the _getHtml() function from Topmenu.php.

Can anyone tell me how to do this?:)

Any help is appreciated :)

like image 533
Wesley Smits Avatar asked Oct 31 '12 09:10

Wesley Smits


People also ask

How can I get category custom attribute value in Magento 2?

Try below code: $categoryId = 5; $_objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $object_manager = $_objectManager->create('Magento\Catalog\Model\Category')->load($categoryId); echo "<pre>"; print_r($object_manager->getData()); die('shasha test');

How do I get customer attribute value in Magento 2?

In other cases where you would want to get custom customer attribute value in Magento 2 stores is integration with 3rd party APIs. The APIs would return the customer attributes value which is not the part of the default Magento 2 and you'd want to use them further and require these custom attributes values.

How do I show attributes in frontend in Magento 2?

Login to Admin panel. Navigate to Store > Attributes > Product. Open an attribute to edit. Change the "Allow HTML Tags on Storefront" and "Visible on Catalog Pages on Storefront" to yes.


1 Answers

Andrew's answer is the way i usually do it. However, it is important to note that if you are adding a custom attribute and if you want your store to work with and without the category flat tables enabled you need to make sure you have the following added to your code:

On your module's config.xml:

...
<frontend>
    <category>
        <collection>
            <attributes>
                <my_attribute /><!-- your attribute code here -->
            </attributes>
        </collection>
    </category>
</frontend>
...

That will make sure your attribute is loaded when the default category collection is created on Mage_Catalog_Model_Resource_Category_Tree::_getDefaultCollection(). Now, that works great when the store is set NOT to use the category flat tables. In case you want to use the flat tables, you will also need to add your attributes in Mage_Catalog_Model_Resource_Category_Flat::_loadNodes. Find the code below, where the select is created, and also add your attribute code there:

$select = $_conn->select()
    ->from(
        array('main_table' => $this->getMainStoreTable($storeId)),
        array('entity_id',
            new Zend_Db_Expr('main_table.' . $_conn->quoteIdentifier('name')),
            new Zend_Db_Expr('main_table.' . $_conn->quoteIdentifier('path')),
            'is_active',
            'is_anchor',
            'my_attribute')) /* add your attribute code here */

Only after that your attribute will show up on the Observer. Needless to say, do it using overwriting, never change the core code.

like image 95
Gabriel Queiroz Silva Avatar answered Sep 27 '22 19:09

Gabriel Queiroz Silva