Magento:在安装脚本中为分组产品添加新属性
I am developing a small Magento-extension for grouped products. This extension needs another attribute, so I figured I could just write a setup script which adds a new attribute to grouped products. But as pretty much everything I tried to do in Magento, that turned out to be way more complex than I expected. The official Magento forum is no real help, so I hope for some support here :)
The new attribute should only appear in the "General" tab of grouped products; simple products, configurable products, bundle products etc. should stay unaltered. The attribute should be there independently of the selected attribute set, just like it was a system attribute.
To do so I thought I could just add the attribute to the entity of grouped products, but as I found out, there's no special entity for grouped products, only the entity "catalog_product" for products in general. Thus my next thought was, I needed to add the attribute to the "catalog_product" entity and then assign it to the correct attribute group, so that it only applies to grouped products.
Problem is that I am not that deep into Magento yet and I have totally no clue how I am supposed to find the corresponding attribute group or if my idea is gonna work at all, maybe I'm totally on the wrong track here :/
Just to let you know what I got so far: I registerd my setup script in the extension's config file and it is being executed, only problem is the setup script itself, it looks like the following atm, because - as I said - I have no clue yet:
$installer = $this;
$installer->startSetup();
$installer->addAttribute("catalog_product", "my_attrib_name", array( /* just a placeholder */ ));
$installer->endSetup();
Pretty basic...
I figured out how to do it on my own now. My approach was correct, I just needed to find the corresponding parameters.
The addAttribute()-call looks like the following now:
// ...
$installer->addAttribute(
"catalog_product", // Entity the new attribute is supposed to be added to
"my_attrib_code", // attribute code
array( // Array containing all settings:
"type" => "varchar",
"label" => "My attribute",
"note" => "Insert additional information about the attribute here",
"input" => "text",
"global" => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_STORE,
// Dont know if this is really necessary, but it makes sure
// the attribute is created as a system attribute:
"user_defined" => false,
// This makes sure the attribute only applies to grouped products
"apply_to" => Mage_Catalog_Model_Product_Type::TYPE_GROUPED
)
);
// ...
Now the attribute added by the installer is a system attribute, it gets automatically added to the "General" group of every attribute set and cannot be changed/moved. As I intended it also only applies to grouped products.