0

i need some custom attribute on every category so i create a module and in mysql4-install-0.0.1.php i write :

$installer = $this;
$installer->startSetup();

$tagline  = array(
    'type'     => 'text',
    'label'    => 'tag-line',
    'input'    => 'text',
    'global'   => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_STORE,
    'visible'           => true,
    'required'          => false,
    'user_defined'      => false,
    'default'           => "",
    'group'         => "General Information",
    'visible_on_front'  => 1
);


$visible  = array(
    'type'     => 'int',
    'label'    => 'is visible on homepage',
    'input'    => 'select',
    'global'   => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_STORE,
    'visible'           => true,
    'required'          => false,
    'user_defined'      => false,
    'default'           => 0,
    'group' => "General Information",
    'visible_on_front'  => 1,
    'source' => 'eav/entity_attribute_source_boolean'
);

$installer->addAttribute('catalog_category', 'tagline', $tagline);
$installer->addAttribute('catalog_category', 'on_index_page', $visible);

$installer->endSetup();

setup work as expected and eveything is good, now i want to update the option of the on_index_page attribute so i create an other file name mysql4-upgrade-0.0.1-0.0.2.php in my setup folder, change the version form the config file and write this code :

$installer = $this;
$installer->startSetup();



file_put_contents("filename.txt", "data5"); // checking if file ever executed
$options = array ( 'value' => array(
                            '0' => array('no'),
                            '1' => array('Brands'),
                            '2' => array('Fruits'),
                            )
            );

//$installer->updateAttribute('catalog_category','on_index_page','source','Hsn_Cat/Cat');
$installer->updateAttribute('catalog_category','on_index_page', 'option' , $options);



$installer->endSetup();

Magento run this file , as i do get the "filename.txt", but the option remain the same.

information 1. the setup file is properly configured,and it execute without error.

  1. while creating the attribute i use the 'source' => 'eav/entity_attribute_source_boolean' so i also test to undo that by doing this $installer->updateAttribute('catalog_category','on_index_page','source','');

  2. i also tried to set the source to my own class as i see that the eav/entity_attribute_source_boolean have a function which return the array of option so i create a class like:

class Hsn_Cat_Model_Cat extends Mage_Core_Model_Abstract{

public function getAllOptions() {

    file_put_contents("called", "im called");
    if (is_null($this->_options)) {
        $this->_options = array(
            array(
                'label' => 'Brands',
                'value' => 2
            ),
            array(
                'label' =>'Fruits',
                'value' => 1
            ),
            array(
                'label' => 'No',
                'value' => 0
            ),
        );
    }
    return $this->_options;
}

}

0

Use eav/entity_attribute_source_boolean as source model means,Your attribute only support boolean value means 0 and 1.

As your want to use Custom source then model Then using source model,you need to update source model as you have created.

<?php
$installer = Mage::getResourceModel('catalog/setup','default_setup');
$installer->startSetup();
$installer->updateAttribute(
    'catalog_category',
    'on_index_page',
    'source_model', //a.o.t. 'source'
    '[YOUR_MODULE_MODEl_Prefix]/cat'
);

$installer->endSetup();

Also you need to change source model extends class

Mage_Core_Model_Abstract to Mage_Eav_Model_Entity_Attribute_Source_Abstract

<?php 
class Hsn_Cat_Model_Cat extends Mage_Eav_Model_Entity_Attribute_Source_Abstract{

    public function getAllOptions() {

        file_put_contents("called", "im called");
        if (is_null($this->_options)) {
            $this->_options = array(
                array(
                    'label' => 'Brands',
                    'value' => 2
                ),
                array(
                    'label' =>'Fruits',
                    'value' => 1
                ),
                array(
                    'label' => 'No',
                    'value' => 0
                ),
            );
        }
        return $this->_options;
    }


}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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