Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

create attribute set in Magento setup script

Creating attributes and assigning them to existing attribute sets is a solved problem but we are running into an issue trying to create an attribute set and populate it with default and specific attributes is failing. This is the code in use:

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

I can verify by debugging through that the initfromSkeleton method does load the attributes from the default attribute_set as advertised, however after the save(), the new set is empty.

Adding new attributes to the set is possible, so it does exist and is created correctly, but the missing default attributes make it unusable since SKU, price, name, etc are all required.

like image 291
Jonathan Day Avatar asked Jun 19 '12 06:06

Jonathan Day


1 Answers

I remember that the issue with creating attribute sets based on the default attribute set was, that you need to save the attribute set twice, once before calling initSkeleton() and once after it.

I don't remember the exact reason anymore, it's too long ago. Anyway, here's what worked for me:

// Mage_Eav_Model_Entity_Setup
$oEntitySetup = $this;
$oEntitySetup->startSetup();

$sNewSetName = 'myset';
$iCatalogProductEntityTypeId = (int) $oEntitySetup->getEntityTypeId('catalog_product');

$oAttributeset = Mage::getModel('eav/entity_attribute_set')
    ->setEntityTypeId($iCatalogProductEntityTypeId)
    ->setAttributeSetName($sNewSetName);

if ($oAttributeset->validate()) {
    $oAttributeset
        ->save()
        ->initFromSkeleton($iCatalogProductEntityTypeId)
        ->save();
}
else {
    die('Attributeset with name ' . $sNewSetName . ' already exists.');
}

$oEntitySetup->endSetup();
like image 170
Jürgen Thelen Avatar answered Sep 28 '22 04:09

Jürgen Thelen