Ajouter des attributs personnalisés à l'attribut personnalisé défini par programme
-
12-12-2019 - |
Question
Bonjour, quelqu'un pourrait-il m'aider avec ça?
J'ai créé un ensemble d'attributs personnalisé et un attribut personnalisé comme
$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);
*/
Ce code ajoute l'Attibute 'New_ATRTR' au groupe "Général" et l'attribut personnalisé est donc affiché dans tous les ensembles d'attributs tels que "Par défaut".
Je veux ajouter l'atticipation personnalisée 'new_attr' uniquement à l'attribut personnalisé Set "Nouvel ensemble d'attract" sous le groupe "Général".Est-ce possible?
La solution
Oui, c'est possible.
Tout d'abord, définissez ces touches de votre réseau de données $ sur des valeurs suivantes pour éviter d'ajouter l'attribut à tous les ensembles d'attributs:
'user_defined' => true,
'group' => ''
Ajoutez l'attribut à votre attribut Set:
$attributeSetId = $this->getAttributeSetId($entityTypeId, 'New Attr Set');
$this->addAttributeToSet($entityTypeId, $attributeSetId, 'General', 'new_attr', 10);
Autres conseils
Ma fonction pour ajouter un attribut (par code) à l'attribut 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;
}
}