Magento1.9 add attribute to catalog product & assign to all attribute set general group

$installer = $this;

$attributes = array(
    'region'      =>   array(
        'type'   => 'int',
        'input' => 'select',
        'source_model'       => 'evebit_catalog/entity_attribute_source_region',
        'frontend_label' => 'Region',
        'is_global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_STORE,
        'frontend_input' => 'select',
        'backend_type' => 'int',
        'used_in_product_listing' => true,
        'is_visible_on_front' => true,
        'is_required' => false,
        'is_user_defined' => true,
        'searchable' => true,
        'filterable' => true,
        'comparable' => false,
        'visible_in_advanced_search'=>true,
        'position' => 15,
        'is_unique' => false,
    )
);

$productEntityId = $installer->getEntityTypeId(Mage_Catalog_Model_Product::ENTITY);
$allAttributeSetIds = $installer->getAllAttributeSetIds($productEntityId);

foreach($attributes as $attributeCode=>$attribute)
{
    $attributeModel =   Mage::getModel('catalog/resource_eav_attribute');
    $attributeModel->addData($attribute);
    $attributeModel->setAttributeCode($attributeCode);
    $attributeModel->setEntityTypeId($productEntityId);


    try
    {
        $attributeModel->save();

    }catch (Mage_Core_Exception $e)
    {
        Mage::logException($e);
    }

    try
    {
        foreach ($allAttributeSetIds as $attributeSetId){

            $generalAttributeGroup = $installer->getAttributeGroupId($productEntityId,$attributeSetId,'General');
            $attributeModel->setAttributeSetId($attributeSetId);
            $attributeModel->setAttributeGroupId($generalAttributeGroup);
            $attributeModel->save();
        }

    }catch (Mage_Core_Exception $e)
    {
        Mage::logException($e);
    }
}

$installer->updateAttribute(Mage_Catalog_Model_Product::ENTITY, 'country_of_manufacture', 'frontend_label', 'Country');

$installer->endSetup();

  

posted on 2017-05-25 17:13  冯亮  阅读(174)  评论(0编辑  收藏  举报

导航