Creare e Aggiungi gruppo di attributi con attributi a tutti i set di attributi
-
12-12-2019 - |
Domanda
Devo creare e aggiungere un gruppo di attributi con 6 attributi a tutti gli attributi impostati in un'installazione esistente.
Ho trovato uno script per creare un gruppo di attributi, funziona alla grande, ma questo non può aggiungere attributi a questo nuovo gruppo.
Qualsiasi aiuto sarà molto apprezzato.
Questo script funziona ideale per aggiungere un gruppo di attributi, ma come posso aggiungere 6 attributi a un nuovo gruppo:
<?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();
}
. Soluzione
È molto più facile farlo tramite lo script di installazione.Ecco un esempio:
// 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);
.
Nota: la variabile $installer
è un'istanza della classe Mage_Eav_Model_Entity_Setup
.Puoi guardare lì per ulteriore riferimento.Puoi anche guardare nella cartella app/code/core/Mage/Catalog/sql/catalog_setup
per alcuni esempi più esempi.
Altri suggerimenti
Utilizzo di uno script di installazione
Prova
$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);
}
}
. Se vuoi ...
- .
- Crea nuovi attributi del prodotto
- Nuovo gruppo di attributi per tutti i set
- Attributi Asign a gruppi correlati in tutti i set
- o modificare gli attributi esistenti (gruppo, posizione, ...)
... puoi provare questo.Consiglierei di inserirlo in uno script di installazione per il riutilizzo successivo ...
.
./** @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:
Solo un array nidificato nel mio caso, che tiene il nome del gruppo, la posizione e gli attributi correlati di attributi ...
.$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', ], ... ] ] ];