1

I'm having problems trying to create a new option "Manage Options" when creating an attribute, do not know how to save the data correctly in the database, I'm overwriting "Mage_Adminhtml_Block_Catalog_Product_Attribute_Edit_Tab_Options" with my module to create custom fields, I want he save these fields when I save the attribute.

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

enter image description here

any idea how I can save the values ​​of these fields in the bank correctly?

1 Answer 1

0

To do that you need to add new columns to eav_attribute_option table.

For example field_1, field_2.

To make your additional fields save rewrite

Mage_Eav_Model_Resource_Entity_Attribute::_saveOption()

Find code:

$sortOrder = !empty($option['order'][$optionId]) ? $option['order'][$optionId] : 0;
if (!$intOptionId) {
    $data = array(
        'attribute_id'  => $object->getId(),
        'sort_order'    => $sortOrder
    );
    $adapter->insert($optionTable, $data);
    $intOptionId = $adapter->lastInsertId($optionTable);
} else {
    $data  = array('sort_order'    => $sortOrder);
    $where = array('option_id =?' => $intOptionId);
    $adapter->update($optionTable, $data, $where);
}

And change it to

$sortOrder = !empty($option['order'][$optionId]) ? $option['order'][$optionId] : 0;
$field1 = !empty($option['field1'][$optionId]) ? $option['field1'][$optionId] : '';
$field2 = !empty($option['field2'][$optionId]) ? $option['field2'][$optionId] : '';
$data = array(
    'sort_order'    => $sortOrder,
    'field1'        => $field1,
    'field2'        => $field2
);
if (!$intOptionId) {
    $data['attribute_id'] = $object->getId();
    $adapter->insert($optionTable, $data);
    $intOptionId = $adapter->lastInsertId($optionTable);
} else {
    $where = array('option_id =?' => $intOptionId);
    $adapter->update($optionTable, $data, $where);
}

The only thing that left is to display saved values in edit form.

3
  • I tried to rewrite using this tip, but was unable to. can tell me how to do use this code in module not modify the core Commented Jan 31, 2014 at 18:56
  • Please take a look on this answer it explains stap by stap how to create rewrite extension. Commented Jan 31, 2014 at 19:18
  • THX, but I need to overwrite the php file that contains saveoption() function Commented Jan 31, 2014 at 19:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.