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?

Teja Bhagavan Kollepara
3,8275 gold badges33 silver badges69 bronze badges
asked Dec 4, 2014 at 16:07
1
  • Is this even possible to achieve? Commented 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);
answered Dec 6, 2014 at 22:43
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;
 }
}
answered Jan 22, 2019 at 19:39

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.