10

Hi Could someone help me with this?

I created a custom attribute set and custom attribute as

$installer = $this;
/* @var $installer Mage_Eav_Model_Entity_Setup */
$installer->startSetup();

//Create Attribute set with Based on Default attribute set
//$installer->removeAttributeSet(Mage_Catalog_Model_Product::ENTITY, 'New Attr Set');
/*
$skeletonID=$installer->getAttributeSetId('catalog_product','Default');
$entityTypeId = Mage::getModel('catalog/product')
->getResource()
->getEntityType()
->getId(); //product entity type

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

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

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


//Create attribute new_attr
//$installer->removeAttribute('catalog_product', 'new_attr');
$data= array (
'attribute_set' =>  'New Attr Set',
'group' => 'General',
'label'    => 'New Attr',
'visible'     => true,
'type'     => 'int', // multiselect uses comma-sep storage
'input'    => 'boolean',
'system'   => false,
'required' => false,
'user_defined' => 1,//defaults to false; if true, define a group
'source' => 'eav/entity_attribute_source_boolean',
'default' => 1,
'global'        => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_STORE,
 );

 $installer->addAttribute('catalog_product','new_attr',$data);
 */

This code adds the attibute 'new_attr' to the group 'General' and so the custom attribute are displayed in all the attribute sets such as 'Default' also.

I want to add the custom attibute 'new_attr' only to the custom attribute set 'New Attr Set' under the group 'General'. Is that possible?

1
  • Is this even possible to achieve? Dec 4, 2014 at 17:56

2 Answers 2

18

Yes, this is possible.

First, set these keys in your $data array to following values to avoid adding attribute to all attribute sets:

'user_defined'         => true,
'group'                => ''

Then add attribute to your attribute set:

$attributeSetId = $this->getAttributeSetId($entityTypeId, 'New Attr Set');
$this->addAttributeToSet($entityTypeId, $attributeSetId, 'General', 'new_attr', 10);
0

My function for add Attribute (by code) to Attribute set

    public function addToAttributeSet($code, $attributeSetName = 'Default', $groupName = 'Details')
{
    try {
        $setup = new Mage_Eav_Model_Entity_Setup('core_setup');

        $attributeSetId = $setup->getAttributeSetId('catalog_product', $attributeSetName);
        $attributeGroupId = $setup->getAttributeGroupId('catalog_product', $attributeSetId, $groupName);
        $attributeId = $setup->getAttributeId('catalog_product', $code);

        $setup->addAttributeToSet($entityTypeId = 'catalog_product', $attributeSetId, $attributeGroupId, $attributeId);

        $this->_success[] = 'Added Attribute to SET ' . $attributeSetName . ' (' . $code . ')';
        return true;

    } catch (Exception $e) {
        $this->_errors[] = 'ERROR when added Attribute to SET ' . $attributeSetName . ' (' . $code . ')';
        $this->_errors[] = $e->getMessage();
        return false;
    }
}

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.