Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding a Category Attribute in Magento 1.8.1

I am currently trying to add a new category attribute to the category admin screen in Magento 1.8.1, I am having issues with getting anything to show though.

The only code examples I can find include mysql4, however I thought this had been retired? Please can anyone point us in the right direction here.

I can see my extension under Config > Advanced and in the core_resources table. But not in the front end of the site.

like image 440
Dave Sims Avatar asked Apr 08 '14 16:04

Dave Sims


People also ask

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

You can use Category's CollectionFactory class and select all attributes by using a star (*) symbol in addAttributeToSelect method. You can use this code example below in your class. protected $_categoryFactory; public function __construct( // ...

What is category attribute?

Category attributes are characteristics of a category which can be used to extend webshop search and also for filtering of products in the webshop using facets. To assign category attributes to the category hierarchy click: Product information management > Setup > Categories > Category hierarchies.


Video Answer


3 Answers

We've tried this recently with 1.8.2.0. You don't really need to create a module just to add a category attribute, once. It seems such a waste to go through so much file cruft to get something installed just once.

Category attributes tend to stay permanently once installed so what worked better for us is to just use a one-off script. Save this one right at magento root.

    <?php

    require_once "app/Mage.php";

    Mage::app()->setCurrentStore(Mage::getModel('core/store')->load(Mage_Core_Model_App::ADMIN_STORE_ID));


    $installer = new Mage_Sales_Model_Mysql4_Setup;

    // change details below:
    $attribute  = array(
        'type' => 'int',
        'label'=> 'Your attribute label',
        'input' => 'text',
        'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
        'visible' => true,
        'required' => false,
        'user_defined' => true,
        'default' => "",
        'group' => "General Information"
    );

    $installer->addAttribute('catalog_category', 'your_attribute_code', $attribute);

    $installer->endSetup();

Save it as add_category_attribute.php or something else memorable for you.

You can either use the browser to get to this file, or use php-cli to run this:

php -f add_category_attribute.php

Good luck.

like image 161
Seth Malaki Avatar answered Oct 06 '22 18:10

Seth Malaki


Change the file name from mysql4-install-0.0.1.php to install-0.0.1.php

like image 25
Hum Avatar answered Oct 06 '22 19:10

Hum


@h3nr1ke You can get attribute with:

$category = Mage::registry('current_category');
if ($category){
   $value = $category->getData('YOUR_CUSTOM_ATTRIBUTE_CODE');
}
like image 35
Dariodor Avatar answered Oct 06 '22 18:10

Dariodor