Creating new options for Magento attributes

Asked

Viewed 671 times

4

I’m having problems trying to create new options in the "Manage Options" tab. When creating an attribute, I already know how to save the data correctly in the database. I am overwriting Mage_Adminhtml_Block_Catalog_Product_Attribute_Edit_Tab_Optionswith 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 I put in the custom fields:

inserir a descrição da imagem aqui

So everything indicates to do this need to be added new columns in the table eav_attribute_option. For example, campo_1, campo_2.

To save the additional fields I need to rewrite Mage_Eav_Model_Resource_Entity_Attribute::_saveOption().

Any hint on how to do this without changing the core in the same way I did above using rewrite, and how to load database data for inputs when editing the attribute?

1 answer

1


You can rewrite Eav/entity_attribute using another model.

To do this add in config.xml

<global>
    ...
    <models>
        ...
        <eav_resource>
            <rewrite>
                <entity_attribute>Seumodulo_Model_Eav_Resource_Entity_Attribute</entity_attribute>
            </rewrite>
        </eav_resource>
        ...
    </models>
    ...
</global>

Create your file in

/app/code/local/Seumodulo/Eav/Model/Resource/Entity/Attribute.php

and use the class signature like this

class Seumodulo_Eav_Model_Resource_Entity_Attribute extends Mage_Core_Model_Resource_Db_Abstract {
    ...
}

Make sure you have cleared the cache configuration, and check that the module is showing up in System->Configuration->Advanced->Disable Modules Output

Test like this to see if it worked

echo get_class(Mage::getResourceModel('eav/entity_attribute'));

To save you can override the methods that enable actions before and after saving. They are

Mage_Catalog_Model_Resource_Attribute::_beforeSave()
Mage_Catalog_Model_Resource_Attribute::_afterSave()

in place of

Mage_Eav_Model_Resource_Entity_Attribute::_saveOption()

So you won’t have to worry about the original code.

See some examples in the links

--

As the resolution of the problem evolves in the comments, it is concluded that the method that should be overwritten is the catalog_entity_attribute_save_before

  • Thanks for the help, in the XML part I have configured, my xml, and he restored me Ceicom_Swatches_Model_Eav_Resource_Entity_Attribute when using echo get_class(Mage::getResourceModel('eav/entity_attribute')); but he doesn’t seem to be taking the codigo de dos after e before, he follows the class: http://pastebin.com/RDZhCEHv

  • foreground Mage_eav_model_resource_entity_attribute instead of Mage_core_model_resource_db_abstract

  • still not accessing the methods, before when I used to Mage_Core_Model_Resource_Db_Abstract, and was giving the following error http://pastebin.com/yF3cK3ba, now that I use Mage_Eav_Model_Resource_Entity_Attribute the error does not occur but also does not work

  • This error occurs pq when extending the Mage_core_model_resource_db_abstract you need to implement all the methods that the abstract class has. That’s why I told you to extend to Mage_eav_model_resource_entity_attribute, but if it didn’t work it must do some check that invalidates the override

  • I guess what I want to do has no way to be done then ;(

  • Tries to copy the whole originating class to yours, and then you add your methods. It should work

  • I’ve tried that, but it performs only the function of Core and not mine

  • 1

    Have you tried watching the "model_save_before" event? Remembering that the event name can be prefixed to a specific model, for example "product_save_before" (product instead of model): http://magedev.com/2009/06/22/event-prefix-in-magento-models/

  • I found that when he saves he dispatches the event catalog_entity_attribute, I tried to create an Observer but it doesn’t seem to be working http://pastebin.com/sZ42pQar

  • 1

    the event was actually catalog_entity_attribute_save_before, I had only taken the prefix without the _save_before

Show 5 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.