Last active
December 22, 2015 20:49
-
-
Save tworzenieweb/6529598 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$installer = new Mage_Eav_Model_Entity_Setup('eav_setup'); | |
$installer->startSetup(); | |
$inheritBase = 9; // default for product | |
$attributeSetName = 'Custom Attribute Set'; | |
$installer->addAttributeSet('catalog_product', $attributeSetName); | |
// Get groups from given set | |
$groups = Mage::getModel('eav/entity_attribute_group') | |
->getResourceCollection() | |
->setAttributeSetFilter($inheritBase) // default attribute set | |
->load(); | |
// Get possibly existing groups from newly inserted set | |
$currentSet = Mage::getModel('eav/entity_attribute_group') | |
->getResourceCollection() | |
->setAttributeSetFilter($installer->getAttributeSetId('catalog_product', $attributeSetName)) | |
->load(); | |
$aCurrentGroups = array(); | |
foreach($currentSet AS $group) | |
{ | |
$aCurrentGroups[$group->attribute_group_name] = array("id" => $group->getId(), "sort_order" => $group->sort_order); | |
} | |
foreach ($groups as $group) | |
{ | |
$groupId = 0; | |
$sort_order = 0; | |
// Check if current group already exists | |
if(!isset($aCurrentGroups[$group->attribute_group_name])) | |
{ | |
// Does not exist, create new group and get id | |
$installer->addAttributeGroup('catalog_product', $attributeSetName, $group->attribute_group_name, $group->sort_order); | |
$newGroup = $installer->getAttributeGroup('catalog_product', $attributeSetName, $group->attribute_group_name); | |
$groupId = $newGroup['attribute_group_id']; | |
$sort_order = $newGroup['sort_order']; | |
} | |
else | |
{ | |
// Already exists, use existing ids | |
$groupId = $aCurrentGroups[$group->attribute_group_name]["id"]; | |
$sort_order = $aCurrentGroups[$group->attribute_group_name]["sort_order"]; | |
} | |
// Get attributes from installer group | |
$groupAttributesCollection = Mage::getModel('eav/entity_attribute') | |
->getResourceCollection() | |
->setAttributeGroupFilter($group->getId()) | |
->load(); | |
foreach ($groupAttributesCollection as $attribute) | |
{ | |
// Add attribute to the new group in the new set | |
$installer->addAttributeToGroup('catalog_product', $attributeSetName, $groupId, $attribute->getId(), $attribute->sort_order); | |
} | |
} | |
$installer->endSetup(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment