Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding custom product attributes in Magento using setup script

I am using module setup script to add new attributes group, attribute set and attributes. I am able to create attribute set, attribute group and add products to group/set. But I am having hard time setting is_filterable, is_visible, is_visible_on_front and is_html_allowed_on_front parameters.

$installer->addAttribute('catalog_product', 'offer_type', array(
        'backend'       => '',
        'frontend'      => '',
        'class' => '',
        'default'       => '',
        'label' => 'Offer type',
        'input' => 'text',
        'type'  => 'int',
        'source'        => '',
        'global'        => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_STORE,
        'visible'       => 1,
        'required'      => 1,
        'searchable'    => 0,
        'filterable'    => 1,
        'unique'        => 0,
        'comparable'    => 0,
        'visible_on_front' => 1,
        'is_html_allowed_on_front' => 1,
        'user_defined'  => 1,
));

$installer->addAttributeToSet('catalog_product', $sSetId, $groupName, 'offer_type');

I see offer_type getting added to Magento and to attribute set($sSetID) and to group ($groupname). Though when I look at attribute from magento admin UI (Catalog->attributes->Manage Attributes), I see is_filterable, is_visible, is_visible_on_front and is_html_allowed_on_front parameters set to No. I have tried various combinations but no luck. I'm using Magento CE 1.7.0.2. I am not sure what is missing in my setup script. I have reffered http://blog.chapagain.com.np/magento-adding-attribute-from-mysql-setup-file/ for this. Am I missing anything? Thanks in advance.

like image 260
sukkad Avatar asked Oct 17 '12 06:10

sukkad


1 Answers

Do you have properly configured your installer in your config.xml ? The standard class for magento installers is Mage_Eav_Model_Entity_Setup but when dealing with products, you'll need to use Mage_Catalog_Model_Resource_Setup instead. Why ? look at their method _prepareValues() and you'll understand what are the authorised attributes (products have more options than the standard eav_objects, you can see that when comparing the tables eav_attribute and catalog_eav_attribute)

To point to the good installer class, take a look at the standard Mage_Catalog config.xml and adapt it for your module :

<resources>
    <catalog_setup>
        <setup>
            <module>Mage_Catalog</module>
            <class>Mage_Catalog_Model_Resource_Setup</class><!-- that line !-->
        </setup>
    </catalog_setup>
</resources>

ps: note that the _prepareValues() method is called only when adding an attribute... if you want to update an attribute you'll need to use the full option name ("is_visible" and not just "visible")...

Another hack would be to add these attributes afterward, but it's not very beautiful:

// adding atribute :
// [...]

//getting the new attribute with full informations
$eavConfig = Mage::getSingleton('eav/config');
$installer->cleanCache();
$attribute = $eavConfig->getAttribute('catalog_product', $attributeCode);
$attribute->addData(array(
    'is_visible' => 1
));
$attribute->save()
like image 133
Jscti Avatar answered Sep 22 '22 01:09

Jscti