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

      

    作者:冯亮
             
    能力有限,水平一般。如有错误,欢迎指正
  • 相关阅读:
    初识你Swift【上篇】
    初识你Swift【下篇】
    单元测试基础
    时间都去哪了?
    iOS App上线的秘密
    mysql系列——DQL常见操作汇总(四)
    Get和Post请求有什么区别?
    SpringBoot2+WebSocket之聊天应用实战
    OCR识别
    mysql系列——子查询(非常重要)(八)
  • 原文地址:https://www.cnblogs.com/fengliang/p/6904938.html
Copyright © 2020-2023  润新知