2012-03-07 60 views
2

我想爲所有產品添加新屬性。我有一個安裝腳本槽Magento:爲所有產品添加新屬性

$installer = $this; 
$installer->startSetup(); 

$this->addAttribute('catalog_product','test2',array(
    'label'  => 'test2', 
    'type'  => 'varchar', 
    'visible' => true, 
    'required' => false, 
    'required' => 0 
)); 

做,但我如何通過

$entityTypeId  = $installer->getEntityTypeId('catalog_product'); 
$attributeSetId = $installer->getDefaultAttributeSetId($entityTypeId); 
$attributeGroupId = $installer->getDefaultAttributeGroupId($entityTypeId, $attributeSetId); 
$installer->addAttributeGroup($entityTypeId, 'Default', 'test2', 0); 
$installer->endSetup(); 

回答

10

這是我用來創建自己的自定義產品的代碼示例之一添加值這個屬性屬性: -

$installer = $this; 
/* @var $installer Mage_Core_Model_Resource_Setup */ 

$installer->startSetup(); 

$attrCode = 'test2'; 
$attrGroupName = 'Test Group'; 
$attrLabel = 'Test 2'; 
$attrNote = 'Test Note'; 

$objCatalogEavSetup = Mage::getResourceModel('catalog/eav_mysql4_setup', 'core_setup'); 
$attrIdTest = $objCatalogEavSetup->getAttributeId(Mage_Catalog_Model_Product::ENTITY, $attrCode); 

if ($attrIdTest === false) { 
    $objCatalogEavSetup->addAttribute(Mage_Catalog_Model_Product::ENTITY, $attrCode, array(
     'group' => $attrGroupName, 
     'sort_order' => 7, 
     'type' => 'varchar', 
     'backend' => '', 
     'frontend' => '', 
     'label' => $attrLabel, 
     'note' => $attrNote, 
     'input' => 'text', 
     'class' => '', 
     'source' => '', 
     'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL, 
     'visible' => true, 
     'required' => false, 
     'user_defined' => true, 
     'default' => '0', 
     'visible_on_front' => false, 
     'unique' => false, 
     'is_configurable' => false, 
     'used_for_promo_rules' => true 
    )); 
} 

$installer->endSetup(); 

這是使用這兩篇​​文章的引用: -

而且,你會發現,我已經使用數組鍵「group」提屬性組名稱,在這個新的自定義屬性將駐留。具有諷刺意味的是,在上面的代碼示例中,提到這個關鍵字會在此Magento中找到的每個屬性集中自動創建此屬性。

因此,您不需要調用任何方法(如「addAttributeToSet()」)將此屬性添加到所有屬性集。

希望它有幫助。

+0

看起來你忘記了你的addattribute命令中的$ attrCode – Benubird 2013-02-25 09:43:22

+0

@Benubird - 謝謝你指出! – 2013-02-27 04:59:21

+0

@KnowledgeCraving - 最好使用Mage :: getResourceModel('catalog/setup','core_setup')'而不是'new Mage_Catalog_Model_Resource_Eav_Mysql4_Setup()'來尊重Magento的工廠系統(併爲此重寫可能性)。 – 7ochem 2013-05-08 13:30:29

0

運行此腳本在你的Magento的根目錄。(更改配置需要)

<?php 

require_once('app/Mage.php'); 
Mage::app()->setCurrentStore(Mage::getModel('core/store')->load(Mage_Core_Model_App::ADMIN_STORE_ID)); 

$installer = new Mage_Eav_Model_Entity_Setup('core_setup'); 
$installer->startSetup();     

$installer->addAttribute('catalog_product', 'snum', array(
      'label'    => 'Serial No', 
      'type'    => 'int', 
      'input'    => 'text', 
      'backend'   => '', 
      'frontend'   => '', 
      'global'   => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL, 
      'visible'   => true, 
      'required'   => false, 
      'user_defined'  => false, 
      'searchable'  => false, 
      'filterable'  => false, 
      'comparable'  => false, 
      'visible_on_front' => true, 
      'visible_in_advanced_search' => false, 
      'unique'   => false 
)); 

$installer->endSetup(); 

?> 

對於刪除產品屬性

<?php 

require_once('app/Mage.php'); 
Mage::app()->setCurrentStore(Mage::getModel('core/store')->load(Mage_Core_Model_App::ADMIN_STORE_ID)); 
$installer = new Mage_Sales_Model_Mysql4_Setup; 
$installer->startSetup(); 
$installer->removeAttribute('catalog_product', 'snum'); 
$installer->endSetup(); 

?> 
0

您可以添加自定義屬性到Magento的後端爲shown.If您將產品屬性創建爲模塊,便於從一個數據庫移動到另一個數據庫。

?php 
$this->startSetup(); 
$this->addAttribute(catalog_product, 'featured_product', array(
'group'   => 'General', 
'input'   => 'select', 
'type'   => 'text', 
'label'   => 'Featured Product', 
'backend'  => '', 
'visible'  => true, 
'required'  => false, 
'visible_on_front' => true, 
'global'  => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL, 
'source' => 'eav/entity_attribute_source_boolean', 
'sort_order'  => 8, 
)); 

$this->endSetup(); 

請參閱一步一步的解釋和文件結構我的教程。 http://www.pearlbells.co.uk/adding-custom-product-attributes-in-magento/

相關問題