2

I have setup script which create a new attribute set. And i need remove attribute groups "Gift Options" and "Recurring Profile" from created attribute set. How can I do this?

<?php
$installer=$this;
$installer->startSetup();

$inheritBase  = $installer->getDefaultAttributeSetId('catalog_product');

$attributeSetName = 'Custom Name';
$ignoreAttributes = array(
    'news_from_date'  => 'news_from_date',
    'news_to_date' => 'news_to_date',
    'weight'  => 'weight',
    'country_of_manufacture' => 'country_of_manufacture',
    'manufacturer' => 'manufacturer',
    'featured'  => 'featured',
    'color' => 'color',
    'delivery_time'  => 'delivery_time',
    'shipping_charges' => 'shipping_charges',
    'marketplace_state'  => 'marketplace_state',
    'generate_meta' => 'generate_meta',
    'percent_donat'  => 'percent_donat',
    'organization_charity' => 'organization_charity',
    'gift_message_available' => 'gift_message_available',
    'recurring_profile' => 'recurring_profile',
    'is_recurring' => 'is_recurring'
);
$installer->addAttributeSet('catalog_product', $attributeSetName);


$groups = Mage::getModel('eav/entity_attribute_group')
    ->getResourceCollection()
    ->setAttributeSetFilter($inheritBase) 
    ->load();

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

    if(!isset($aCurrentGroups[$group->attribute_group_name])) {
        $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 {
        $groupId = $aCurrentGroups[$group->attribute_group_name]["id"];             
        $sort_order = $aCurrentGroups[$group->attribute_group_name]["sort_order"];
    }

    $groupAttributesCollection = Mage::getModel('eav/entity_attribute')
        ->getResourceCollection()
        ->setAttributeGroupFilter($group->getId())
        ->load();

    foreach ($groupAttributesCollection as $attribute) {
        if(!isset($ignoreAttributes[$attribute->getAttributeCode()])) {
            $installer->addAttributeToGroup('catalog_product', $attributeSetName, $groupId, $attribute->getId(), $attribute->sort_order);
        }
    }
}

$installer->endSetup();
3

Try the below code to remove the specific group and specific attributes while creating new attribute set by script. It works for me,

<?php
$installer = $this;
$installer->startSetup();

$attributeSetName = 'Custom Name'; 

$ignoreGroup = array('Gift Options','Recurring Profile');//Add Attribute Group Name To Remove

$ignoreAttribute = array('news_to_date','weight','news_from_date');//Add Attribute Code to Remove

$entityTypeId = Mage_Catalog_Model_Product::ENTITY;
$installer->addAttributeSet($entityTypeId,$attributeSetName,0);
$inheritBase = $installer->getDefaultAttributeSetId($entityTypeId);
$newAttributeSetId = $installer->getAttributeSetId($entityTypeId,$attributeSetName);

$groups = Mage::getModel('eav/entity_attribute_group')->getResourceCollection()->setAttributeSetFilter($inheritBase)->load();
foreach($groups as $group){
    if(!in_array($group->getAttributeGroupName(),$ignoreGroup))
    {
        $installer->addAttributeGroup($entityTypeId,$newAttributeSetId,$group->getAttributeGroupName(),$group->getSortOrder());//Add Group To new Attribute Set

        $newGroup = $installer->getAttributeGroup($entityTypeId,$newAttributeSetId,$group->getAttributeGroupName());

        $attributeCollections = Mage::getModel('eav/entity_attribute')->getResourceCollection()->setAttributeGroupFilter($group->getId())->load();
        foreach($attributeCollections as $attribute){
            if(!in_array($attribute->getAttributeCode(),$ignoreAttribute))
            {
                $installer->addAttributeToGroup($entityTypeId,$newAttributeSetId,$newGroup['attribute_group_id'],$attribute->getId(),$attribute->getSortOrder());
            }
        }
    }
}
$installer->endSetup();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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