5

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.

3 Answers 3

5

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();
Sign up to request clarification or add additional context in comments.

2 Comments

thanks Jurgen, will give that a try. I have seen other instances where a Magento object needed to be saved before modifications, its not logical, but definitely possible!
Saving twice was the real issue for me. Thanks for the answer!
0

Please notice that setup class needs to extend

Mage_Catalog_Model_Resource_Eav_Mysql4_Setup

so that

$oEntitySetup->getEntityTypeId('catalog_product');

can be called.

Comments

0

I used Jürgen Thelen answer which worked.

But I found the new attribute set did not have default options and options group such as general and invoice etc.

So to get round this include $installer->getAttributeSetId('catalog_product', 'default') in the initFromSkeleton()

if($attributeSet->validate()) {
$attributeSet
    ->save()
    ->initFromSkeleton($installer->getAttributeSetId('catalog_product', 'default'))
    ->save();
} else {
die('Attributeset with name ' . $setName . ' already exists.');
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.