2012-03-07 18 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()のような)メソッドを呼び出す必要はありません。

希望します。削除製品属性

<?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

addattributeコマンドで$ attrCodeを忘れてしまったようです。 – Benubird

+0

@Benubird - それを指摘してくれてありがとう! –

+0

@KnowledgeCraving - 新しいMage_Catalog_Model_Resource_Eav_Mysql4_Setup()の代わりに、Magentoのファクトリシステム(とそのための書き換え可能性)を尊重するために、 'Mage :: getResourceModel( 'catalog/setup'、 'core_setup')'を使用する方がよい。 – 7ochem

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

?> 

あなたはshown.IfとしてMagentoのバックエンドにカスタム属性を追加することができます製品属性をモジュールとして作成し、あるデータベースから別のデータベースに移動するのは簡単です。

?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/

0

については

関連する問題