Créer et ajouter un groupe d'attributs avec des attributs à tous les ensembles d'attributs
-
12-12-2019 - |
Question
Je dois créer et ajouter un groupe d'attributs avec 6 attributs à tous les ensembles d'attributs d'une installation existante.
J'ai trouvé un script pour créer un groupe d'attributs, cela fonctionne très bien, mais cela ne peut pas ajouter d'attributs à ce nouveau groupe.
Toute aide sera grandement appréciée.
Ce script fonctionne très bien pour ajouter un groupe d'attributs, mais comment puis-je ajouter 6 attributs à un nouveau groupe :
<?php
//enable errors to see if something is wrong
error_reporting(E_ALL | E_STRICT);
ini_set('display_errors', 1);
//include Mage.php
define('MAGENTO_ROOT', getcwd());
$mageFilename = MAGENTO_ROOT . '/app/Mage.php';
require_once $mageFilename;
//tell magento that you are running on developer mode, for additional error messages (if any)
Mage::setIsDeveloperMode(true);
//instantiate the application
Mage::app();
//get the type ID of the product - you will need it later
$entityTypeId = Mage::getModel('catalog/product')->getResource()->getTypeId();
//get all attribute sets
$sets = Mage::getModel('eav/entity_attribute_set')
->getResourceCollection()
//filter only sets for products - that's why you needed the product type ID
->addFilter('entity_type_id', $entityTypeId);
//loop through all the sets
foreach ($sets as $set){
//create an attribute group instance
$modelGroup = Mage::getModel('eav/entity_attribute_group');
//set the group name
$modelGroup->setAttributeGroupName('Some group name here') //change group name
//link to the current set
->setAttributeSetId($set->getId())
//set the order in the set
->setSortOrder(100);
//save the new group
$modelGroup->save();
}
La solution
C'est beaucoup plus facile de le faire via le script de configuration.Voici un exemple:
// Add new Attribute group
$groupName = 'My Attr Group';
$entityTypeId = $installer->getEntityTypeId('catalog_product');
$attributeSetId = $installer->getDefaultAttributeSetId($entityTypeId);
$installer->addAttributeGroup($entityTypeId, $attributeSetId, $groupName, 100);
$attributeGroupId = $installer->getAttributeGroupId($entityTypeId, $attributeSetId, $groupName);
// Add existing attribute to group
$attributeId = $installer->getAttributeId($entityTypeId, ATTRIBUTE_CODE);
$installer->addAttributeToGroup($entityTypeId, $attributeSetId, $attributeGroupId, $attributeId, null);
Note: $installer
la variable est une instance de Mage_Eav_Model_Entity_Setup
classe.Vous pouvez y chercher pour plus de références.Vous pouvez également regarder dans app/code/core/Mage/Catalog/sql/catalog_setup
dossier pour quelques exemples supplémentaires.
Autres conseils
à l'aide d'un script d'installateur
essayer
$filters = array(
'my_attribute_id_1',
'my_attribute_id_2'
..
);
$product_tab = 'General';
foreach($filters as $value){
$eavModel = Mage::getModel('eav/entity_setup','core_setup');
$attributeId = $eavModel->getAttributeId('catalog_product', $value);
foreach($eavModel->getAllAttributeSetIds('catalog_product') as $id) {
$attributeGroupId = $eavModel->getAttributeGroupId('catalog_product', $id, $product_tab);
$eavModel->addAttributeToSet('catalog_product', $id, $attributeGroupId, $attributeId);
}
}
Si vous voulez ...
- Créer de nouveaux attributs de produit
- Nouveau groupe d'attributs pour tous les ensembles
- Attributs Asign aux groupes associés dans tous les ensembles
- ou modifier les attributs existants (groupe, position, ...)
... vous pouvez essayer ceci.Je recommanderais de le mettre dans un script d'installation pour une réutilisation ultérieure ...
/** @var Mage_Catalog_Model_Resource_Setup $setup */ $setup = Mage::getResourceModel('catalog/setup', 'default_setup'); $entity = Mage_Catalog_Model_Product::ENTITY; $entityTypeId = $setup->getEntityTypeId($entity); ## config here: $attributesDataMap = ... see below foreach ($setup->getAllAttributeSetIds($entityTypeId) as $attributeSetId) { foreach ($attributesDataMap as $groupName => $groupData) { # add groups to all sets OR change position $setup->addAttributeGroup( $entityTypeId, $attributeSetId, $groupName, $groupData['sort_order'] ); if (count($groupData['attributes'])) { foreach ($groupData['attributes'] as $attributeCode => $attributeData) { $attributeId = $setup->getAttributeId($entityTypeId, $attributeCode); $attributeData = array_merge( ['group' => $groupName], $defaultData, $groupData['default'], $attributeData ); # add new attribute if (!$attributeId) { $setup->addAttribute( $entityTypeId, $attributeCode, $attributeData ); # update attribute psotion and group } else { $setup->updateAttribute( $entityTypeId, $attributeCode, $attributeData ); $setup->addAttributeToSet( $entityTypeId, $attributeSetId, $groupName, $attributeId, $attributeData['sort_order'] ); } } } } }
config:
Juste un tableau imbriqué dans mon cas, qui détient le nom du groupe d'attributs, la position et les attributs associés ...
$defaultData = [ 'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL, 'backend' => '', 'visible' => true, 'searchable' => false, 'filterable' => false, 'comparable' => false, 'is_configurable' => false, 'visible_on_front' => false, 'visible_in_advanced_search' => false, 'used_in_product_listing' => false, 'user_defined' => false, 'required' => true, ]; $attributesDataMap = [ 'General' => [ 'sort_order' => null, 'default' => $defaultData, 'attributes' => [ 'ean' => [ 'label' => 'EAN', 'sort_order' => '3', ], 'mpn' => [ 'label' => 'MPN', 'sort_order' => '4', ], ], ], 'eBay' => [ 'sort_order' => 80, 'default' => $defaultData, 'attributes' => [ 'ebay_title' => [ 'label' => 'eBay Titel', 'sort_order' => '10', 'frontend_class' => 'validate-length maximum-length-80', 'note' => 'Maximum 80 chars', ], ... ] ] ];