7

I am programmatically creating attributes and attribute sets. The attributes work like they should, but with the attribute sets I have a problem.

When I create an attribute set, I can not let it inherit from another set (for example the default set). I have searched a lot but could not find any good info that could help me further.

My first thought there should be some function that lets you duplicate a set and edit it. I could not find it.

My second thought was recreating the whole set, but I could not find a function to get the groups from an attribute set, which I do want.

I'm pretty new to Magento, so sorry if I just missed something, but I am currently stuck. Any solutions / suggestions will be greatly appreciated.

3 Answers 3

11

After trying out again, messing up a lot and finally finding the correct answer, this works:

$entityTypeId = Mage::getModel('catalog/product')
                  ->getResource()
                  ->getEntityType()
                  ->getId(); //product entity type

$attributeSet = Mage::getModel('eav/entity_attribute_set')
                  ->setEntityTypeId($entityTypeId)
                  ->setAttributeSetName("test_set");

$attributeSet->validate();
$attributeSet->save();

$attributeSet->initFromSkeleton($entityTypeId)->save();

You need to save before you do initFromSekeleton(). Otherwise it just won't work.

1
  • Like Robbie said, you can just use Mage_Catalog_Model_Product_Attribute_Set_Api::create to do all this for you
    – Erfan
    Commented Dec 23, 2015 at 4:23
10

Just a note, it appears that the attribute set API model can do this itself:

$newSetName    = 'My New Set';
$originalSetId = 4; // the set to base the new one off

/** @var Mage_Catalog_Model_Product_Attribute_Set_Api */
Mage::getModel('catalog/product_attribute_set_api')
    ->create($newSetName, $originalSetId);
1
  • 1
    Oh nice. This is a much better solution.. maybe it didn't exist 2 years ago
    – Erfan
    Commented Dec 23, 2015 at 4:22
10

See how the attribute sets are created (inherited) from the backend. Check this method: Mage_Adminhtml_Catalog_Product_SetController::saveAction().
In that method there is this line that 'clones' the attribute set:

$model->initFromSkeleton($this->getRequest()->getParam('skeleton_set')); 

Where ->getParam('skeleton_set') is the attribute set to be cloned. Basically you need to do something like this:

$cloneSetId = 4;//or anything else
$entityTypeId = Mage::getModel('catalog/product')->getResource()->getTypeId(); //product entity type
$model = Mage::getModel('eav/entity_attribute_set'); //instantiate the model
$model->setEntityTypeId($entityTypeId);//attribute set is used for products
$model->setAttributeSetName('Attribute set name here');//set the attribute set name
$attributeSet->save(); // save before initFromSkeleton
$model->initFromSkeleton($cloneSetId);//clone one attribute set
$model->save();//save the new attribute set
//do other modifications here
3
  • Hmm, it seems that this code somehow only copies the groups, and not the attributes in those groups.. Commented Jun 19, 2013 at 7:36
  • This means I might have missed something. Because in the initFromSkeleton method there is this line $newGroup->setAttributes($newAttributes);. This should set some attributes. Make sure you clone a valid set, also you can debug the Mage_Adminhtml_Catalog_Product_SetController::saveAction() and try to simulate it in your code. that one seams to work.
    – Marius
    Commented Jun 19, 2013 at 7:42
  • It is a valid ID (I just use 4, which is an existing, default set), but I can't find anything to fix it :S Commented Jun 19, 2013 at 8:26

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.