How to Add Custom Attributes to Custom Options in Magento

    Let us add ‘details’ custom attribute to Custom Options for select types.
    Select options are stored in the tables:

    • catalog_product_option_type_value,
    • catalog_product_option_type_title,
    • catalog_product_option_type_price.

    We’ve added ‘details’ field into catalog_product_option_type_value table
    The installation file comes with:

    /* @var $installer Mage_Core_Model_Resource_Setup */
    $installer = $this;
        ->addColumn($installer->getTable('catalog/product_option_type_value'), 'details’, 'VARCHAR(128) NULL');

    The field must be: NULL.
    Note: flush the cache after adding the field.

    The next step – on the page of add/ edit product we switch the file to add options (app\design\adminhtml\default\default\template\catalog\product\edit\options\type\select.phtml) with our own.
    We copy the above file to:app\design\adminhtml\default\default\template\ikantam\catalog\product\edit\options\type\select.phtml.
    Rewrite: ‘Mage_Adminhtml_Block_Catalog_Product_Edit_Tab_Options_Type_Select’ block and replace constructor file in it by indicating our phtml file

        public function __construct()

    We add the title of the column into ‘Custom Options’ table.
    In ikantam/catalog/product/edit/options/type/select.phtml’ file in OptionTemplateSelect varilable in <tr class=”headings”> tag we add the line:

    '<th class="type-title"><?php echo Mage::helper('catalog')->__('Details') ?></th>'+

    Add OptionTemplateSelectRow tag to the variable:

    '<td><input type="text" class="input-text select-type-details" id="product_option_{{id}}_select_{{select_id}}_title" name="product[options][{{id}}][values][{{select_id}}][details]" value="{{details}}">{{checkboxScopeTitle}}</td>'+

    After that in the table a new Details field will appear. This field could be set as required or not by adding input class: required-entry.

    Save the product and the new ‘details’ attribute will be saved in the database. Though it is saved already, it is not yet uploaded from the database. If you open ‘Custom Options’, you will find ‘details’ field empty (it is empty because ‘details’ are not yet passed to the template). We need to re-write the block:
    In: ‘getOptionValues()’ method, cycle: foreach ($option->getValues() as $_value) {
    add new key: ‘details’ to the variable: $value and the value for it: $_value->getDetails();

    After that the values for the ‘details’ attribute will appear in the database.

    In order for the new attribute to appear on the frontend rewrite the class: Mage_Catalog_Block_Product_View_Options_Type_Select and add the newly added attribute to it. But be aware that depending on the type of Custom Options there different kinds of htmls generate.
    Post from

    Recent Articles


    Related Stories

    Stay on op - Ge the daily news in your inbox