Creating new options for Magento attributes

attributes, magento, php

Solution

This is a possible workaround:

At the beggining I tried to override the eav class Mage_Eav_Model_Resource_Entity_Attribute, but I didn't work. After looking closer to the code I found that the _saveOption method is called by another class that extends Mage_Eav_Model_Resource_Entity_Attribute. So if you override Mage_Eav_Model_Resource_Entity_Attribute with your custom class I wont have any effect in the saving option process. I also realized that there are another clases that extends Mage_Eav_Model_Resource_Entity_Attribute

That clases are:

- Mage_Catalog_Model_Resource_Attribute

- Mage_Eav_Model_Mysql4_Entity_Attribute

- Mage_Eav_Model_Resource_Attribute (abstract)

In order to be able to override the method in the attribute option save process you will have to:

1) Created a class that extends Mage_Eav_Model_Resource_Entity_Attribute and override the method

`class My_Module_Model_Eav_Resource_Entity_Attribute extends Mage_Eav_Model_Resource_Entity_Attribute{ protected function _saveOption(Mage_Core_Model_Abstract $object){ //your custom logic here } } `

Dont override Mage_Eav_Model_Resource_Entity_Attribute in your module config, the above class will serve as parent of our main target class Mage_Catalog_Model_Resource_Attribute which is the one that take part in the saving process.

2) Override in your module configuration the class Mage_Catalog_Model_Resource_Attribute with a new class that will extends you previous created class My_Module_Model_Eav_Resource_Entity_Attribute

Your config will look like this

<global>
      <models>
            <!-- Overrides Mage_Catalog_Model_Resource_Attribute -->
            <catalog_resource>
                <rewrite>
                    <attribute>My_Module_Model_Catalog_Resource_Attribute</attribute>
                </rewrite>
            </catalog_resource>
        </models>
       <!-- The rest of global config section -->
</global>

Now you will see your custom __saveOption method being executed when you save an attribute.

Problem

I'm having problems trying to create new options in the "Manage Options" tab. When you create an attribute, I know how to save the data correctly in the database. I'm replacing `Mage_Adminhtml_Block_Catalog_Product_Attribute_Edit_Tab_Options` with my module to create custom fields. My module: config.xml ``` <config> <blocks> <adminhtml> <rewrite> <catalog_product_attribute_edit_tabs>Ceicom_Swatches_Block_Adminhtml_Tabs</catalog_product_attribute_edit_tabs> <catalog_product_attribute_edit_tab_options>Ceicom_Swatches_Block_Adminhtml_Options</catalog_product_attribute_edit_tab_options> </rewrite> </adminhtml> </blocks> </config> ``` Ceicom/Swatches/Block/Adminhtml/Options.php ``` class Ceicom_Swatches_Block_Adminhtml_Options extends Mage_Adminhtml_Block_Catalog_Product_Attribute_Edit_Tab_Options { public function __construct() { parent::__construct(); $this->setTemplate('ceicom/attribute/options.phtml'); } } ``` in the Phtml file placed in the custom fields: Apparently to do this needs adding new columns in the table `eav_attribute_option`. For example]: `field_1`, `field_2`. To save additional fields I need to rewrite: `Mage_Eav_Model_Resource_Entity_Attribute::_saveOption()`. Any tips on how to do this without changing the CORE, just as I did above using `rewrite`, and how to load the data bank for inputs to edit the attribute?

Original source