create attribute set in Magento setup script

create attribute set in Magento setup script

August 13, 2026 · Illustrated author avatarBy Stefan Bogdanescu
create attribute set in Magento setup script

Solving Magento Attribute Set Creation Issues

Creating attribute sets and populating them with default attributes is a common task during Magento setup scripts, but it can sometimes lead to unexpected results like an empty set after initialization from the skeleton (initFromSkeleton).

Understanding the Problem: Missing Default Attributes

The provided code attempts to create a new attribute set called ‘women_sizing_denim’ and initialize it with data from the default set. While the initFromSkeleton method appears to load the correct attributes, the subsequent save() call results in an empty set.

$setup->addAttributeSet('catalog_product', 'women_sizing_denim');

$oAttributeSetModel = Mage::getModel("eav/entity_attribute_set")
    ->load($setup->getAttributeSetId('catalog_product', 'women_sizing_denim'))
    ->initFromSkeleton($setup->getAttributeSetId('catalog_product', 'default'))
    ->save();

The Default Set Behavior

The issue lies in how Magento handles default sets. When a new set is initialized from the default, it only copies the structural information (like sort order) and not the actual attribute data.

By design, default sets are meant to be templates for other sets, so copying all attributes would defeat this purpose. Instead of inheriting full attribute configurations, child sets should have their own unique attribute configurations.

The Solution: Manually Populating Attributes

To resolve this issue, you’ll need to manually add the required attributes to your new set after the save() call. Here’s a corrected version of the script:

$setup->addAttributeSet('catalog_product', 'women_sizing_denim');

$attributeSetId = $setup->getAttributeSetId('catalog_product', 'women_sizing_denim');
$attributeSetModel = Mage::getSingleton("eav/entity_attribute_set");
$attributeSetModel->load($attributeSetId);

foreach ($setup->getDefaultAttributes('catalog_product') as $attributeCode) {
    $attributeModel = Mage::getSingleton("eav/config")->getAttribute('catalog_product', $attributeCode);
    if (!$attributeSetModel->getAttributeSetInfoByCode($attributeCode)) {
        $attributeSetModel->addAttributeToSet(
            'catalog_product',
            $attributeSetId,
            null,
            $attributeCode
        );
    }
}

$attributeSetModel->save();

This script loads the default attributes and adds them to your new set using addAttributeToSet(). This ensures that all necessary attributes like SKU, price, name, etc. are included.

Best Practices for Attribute Set Management

  • Always create a unique attribute set for each product type or category with specific requirements.
  • Avoid relying on the default set as it might change between Magento versions.
  • Manually configure your sets to ensure they match your business needs exactly.
  • Never use initFromSkeleton if you need to preserve all attributes from the default set.

Conclusion

Creating attribute sets in Magento setup scripts requires careful handling of the default set. By understanding the limitations of initFromSkeleton, you can manually add necessary attributes after creating a new set.

Remember, Magento is designed for flexibility and customization, so while there are conventions like default sets, they may not always meet your specific needs out-of-the-box.

For more advanced Magento setup script techniques, I recommend checking our detailed guides at Laravel Company.

Happy coding!

Catalog Themes Configuration Orders

Our network