config.xml:
<?xml version="1.0"?> <config> <modules> <Bysoft_Import> <version>0.1.1</version> </Bysoft_Import> </modules> <global> <helpers> <import> <class>Bysoft_Import_Helper</class> </import> </helpers> <models> <import> <class>Bysoft_Import_Model</class> <resourceModel>import_mysql4</resourceModel> </import> <import_mysql4> <class>Bysoft_Import_Model_Mysql4</class> <entities> <product> <table>bysoft_import_product</table> </product> <category> <table>bysoft_import_category</table> </category> </entities> </import_mysql4> </models> <resources> <import_setup> <setup> <module>Bysoft_Import</module> <class>Mage_Eav_Model_Entity_Setup</class> </setup> <connection> <use>core_setup</use> </connection> </import_setup> <import_write> <connection> <use>core_write</use> </connection> </import_write> <import_read> <connection> <use>core_read</use> </connection> </import_read> </resources> </global> </config>
upgrade script:
<?php
$installer = $this;
$installer->startSetup();
$installer->addAttribute('catalog_product', "specific_usage", array(
'type' => 'int',
'input' => 'select',
'label' => 'Specific Usage',
'sort_order' => 1000,
'required' => false,
'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_STORE,
'backend' => 'eav/entity_attribute_backend_array',
'option' => array (
'values' => array(
)
),
));
$attSet = Mage::getModel('eav/entity_type')->getCollection()->addFieldToFilter('entity_type_code','catalog_product')->getFirstItem(); // This is because the you adding the attribute to catalog_products entity ( there is different entities in magento ex : catalog_category, order,invoice... etc )
$attSetCollection = Mage::getModel('eav/entity_type')->load($attSet->getId())->getAttributeSetCollection(); // this is the attribute sets associated with this entity
$attributeInfo = Mage::getResourceModel('eav/entity_attribute_collection')
->setCodeFilter('specific_usage')
->getFirstItem();
$attCode = $attributeInfo->getAttributeCode();
$attId = $attributeInfo->getId();
foreach ($attSetCollection as $a)
{
$set = Mage::getModel('eav/entity_attribute_set')->load($a->getId());
$setId = $set->getId();
$group = Mage::getModel('eav/entity_attribute_group')->getCollection()->addFieldToFilter('attribute_set_id',$setId)->setOrder('attribute_group_id',ASC)->getFirstItem();
$groupId = $group->getId();
$newItem = Mage::getModel('eav/entity_attribute');
$newItem->setEntityTypeId($attSet->getId()) // catalog_product eav_entity_type id ( usually 10 )
->setAttributeSetId($setId) // Attribute Set ID
->setAttributeGroupId($groupId) // Attribute Group ID ( usually general or whatever based on the query i automate to get the first attribute group in each attribute set )
->setAttributeId($attId) // Attribute ID that need to be added manually // Sort Order for the attribute in the tab form edit
->save()
;
//echo "Attribute ".$attCode." Added to Attribute Set ".$set->getAttributeSetName()." in Attribute Group ".$group->getAttributeGroupName()."<br>\n";
}
$installer->endSetup();