Adding custom product attributes in Magento using setup script

前端 未结 2 1056
离开以前
离开以前 2020-12-10 07:28

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. B

2条回答
  •  臣服心动
    2020-12-10 08:11

    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 :

    
        
            
                Mage_Catalog
                Mage_Catalog_Model_Resource_Setup
            
        
    
    

    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()
    

提交回复
热议问题