Daniel Alexander Karr
Daniel Alexander Karr

Reputation: 325

Magento: Create custom attribute, set default attribute set

just one simple but head-scratching question.

I create a attribute, set it to Drop-Down behavior (in Admin-Store, to be seen as a Drop-Down Attribute) it is creating quiet smooth, but didn't set the attribute_set.

In short:

is-state: my attribute --> not linked to a attribute set

to-state: my attribute --> linked to default attribute set

Upvotes: 0

Views: 4441

Answers (1)

vicch
vicch

Reputation: 706

Administration Way:

It is natural that a newly created attribute is not assigned to an attribute set (including the Default attribute set) automatically. You need to do it manually.

In the Admin, under Catalog > Attributes > Manage Attribute Sets.

source

Programming Way:

Try this code:

$installer = Mage::getModel('eav/entity_setup');
$installer->addAttributeToSet($entityTypeId, $setId, $groupId, $attributeId, $sortOrder);

The specifications for function addAttributeToSet:

  • mixed $entityTypeId
  • mixed $setId
  • mixed $groupId
  • mixed $attributeId
  • int $sortOrder=null
  • @return Mage_Eav_Model_Entity_Setup

The arguments ending with 'Id' don't actually have to be ids according to the codes. Set and group names can be transalated to ids automatically. However for the attribute, you should use code (usually written in small letters, ex. 'firstname') rather than names (ex. 'First Name').

For example, you want to add a product attribute called 'popularity' to the 'Default' attribute set under 'General' group, just write like this:

$installer = Mage::getModel('eav/entity_setup');
$installer->addAttributeToSet('catalog_product', 'Default', 'General', 'popularity');

Unfortunately I don't have an installation to test the code for the moment, hopefully it should work :)

Upvotes: 4

Related Questions