zoukankan      html  css  js  c++  java
  • 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);
        }
    
    }
    

      

    作者:冯亮
             
    能力有限,水平一般。如有错误,欢迎指正
  • 相关阅读:
    Java IO: InputStreamReader和OutputStreamWriter
    Java IO: 序列化与ObjectInputStream、ObjectOutputStream
    Java IO: Buffered和Data
    [Codeforces Round #613 (Div. 2)]
    [算进] 巴士
    [算进] 小木棍
    [算进] 数据备份
    [算进] 双端队列 题解
    [算进] 蚯蚓 题解
    [算进] 赶牛入圈 题解
  • 原文地址:https://www.cnblogs.com/fengliang/p/6477720.html
Copyright © 2011-2022 走看看