Magento add product attribute and assign to all group

$attributes = array(
    'product_type'      =>   array(
        'type'   => 'int',
        'input' => 'select',
        'source_model'       => 'mcatalog/source_eav_attribute_product_type',
        'frontend_label' => '产品类型',
        'is_global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
        'frontend_input' => 'select',
        'backend_type' => 'int',
        'used_in_product_listing' => false,
        'is_visible_on_front' => false,
        'is_required' => true,
        'user_defined' => true,
        'searchable' => false,
        'filterable' => false,
        'comparable' => false,
        'position' => 15,
        'is_unique' => false,
    )
);

$productEntityId = $installer->getEntityTypeId(Mage_Catalog_Model_Product::ENTITY);
$productAttributeSets = Mage::getModel('eav/entity_attribute_set')->getCollection()
                        ->addFieldToFilter('entity_type_id', $productEntityId);

foreach($attributes as $attributeCode=>$attribute)
{
    try{

        $attributeModel =   Mage::getModel('catalog/resource_eav_attribute');
        $attributeModel->addData($attribute);
        $attributeModel->setAttributeCode($attributeCode);
        $attributeModel->setEntityTypeId($productEntityId);
        $re = $attributeModel->save();

        foreach ($productAttributeSets as $attributeSet){

            $generalAttributeGroupId = $installer->getAttributeGroupId($productEntityId,$attributeSet->getId(),'General');
            Mage::getModel('eav/entity_attribute')
            ->setAttributeSetId($attributeSet->getId())
            ->setAttributeGroupId($generalAttributeGroupId)
            ->setEntityTypeId($productEntityId)
            ->setAttributeId($re->getId())
            ->save();
        }

    }catch (Exception $e){

        Mage::logException($e);
    }

}

  

posted on 2017-02-28 10:34  冯亮  阅读(247)  评论(0编辑  收藏  举报

导航