2013-03-20 22 views
7

Ho un attributo esistente per un codice di incorporamento. Ho bisogno di associare questo attributo con 120+ set di attributi esistenti.Aggiunta dell'attributo esistente a tutti i set di attributi

Se conosco l'ID set dell'attributo, come posso aggiungere l'attributo a tutti gli insiemi di set a livello di codice?

+0

hai un comune gruppo di attributi (come generale/meta/etc ,,) in cui vuoi aggiungere l'attributo? – Meabed

+0

@Meabed Facciamo, tuttavia, tutti i prodotti sono assegnati a specifici (e diversi) set di attributi. Tuttavia tutti gli insiemi di attributi devono avere questo specifico attributo. Quindi devo trovare un modo per aggiornare di massa. –

risposta

21

ho trovato interessante la scrittura di codice per questo problema ecco la soluzione che funziona :)

esegue questo codice in script php compresi mage.php e fatemi sapere se funziona bene.

replace (firstname) with the attribute code that you want to mass add to all attribute sets

$attSet = Mage::getModel('eav/entity_type')->getCollection()->addFieldToFilter('entity_type_code','catalog_product')->getFirstItem(); // This is because the you adding the attribute to catalog_products entity (there is different entities in magento ex : catalog_category, order,invoice... etc) 
    $attSetCollection = Mage::getModel('eav/entity_type')->load($attSet->getId())->getAttributeSetCollection(); // this is the attribute sets associated with this entity 
    $attributeInfo = Mage::getResourceModel('eav/entity_attribute_collection') 
     ->setCodeFilter('firstname') 
     ->getFirstItem(); 
    $attCode = $attributeInfo->getAttributeCode(); 
    $attId = $attributeInfo->getId(); 
    foreach ($attSetCollection as $a) 
    { 
     $set = Mage::getModel('eav/entity_attribute_set')->load($a->getId()); 
     $setId = $set->getId(); 
     $group = Mage::getModel('eav/entity_attribute_group')->getCollection()->addFieldToFilter('attribute_set_id',$setId)->setOrder('attribute_group_id',"ASC")->getFirstItem(); 
     $groupId = $group->getId(); 
     $newItem = Mage::getModel('eav/entity_attribute'); 
     $newItem->setEntityTypeId($attSet->getId()) // catalog_product eav_entity_type id (usually 10) 
        ->setAttributeSetId($setId) // Attribute Set ID 
        ->setAttributeGroupId($groupId) // Attribute Group ID (usually general or whatever based on the query i automate to get the first attribute group in each attribute set) 
        ->setAttributeId($attId) // Attribute ID that need to be added manually 
        ->setSortOrder(10) // Sort Order for the attribute in the tab form edit 
        ->save() 
     ; 
     echo "Attribute ".$attCode." Added to Attribute Set ".$set->getAttributeSetName()." in Attribute Group ".$group->getAttributeGroupName()."<br>\n"; 
    } 
+0

Ha funzionato come un incantesimo, tuttavia spero (chiedendomi) se c'è un modo per impostare la posizione. Idealmente, mi piacerebbe che fosse l'ultimo elemento della sezione Generale. È possibile? Screenshot: http://cl.ly/image/0O0H0x0E2h05. Grazie mille! –

+1

cambia posizione nella scheda -> setSortOrder (10) // cambia questo numero da 10 in poi! – Meabed

+0

Il numero più alto imposterà l'ordinamento, quindi posso assicurarmi che sia in fondo. Tuttavia non esiste un modo chiaro per selezionare il gruppo Generale. In questo momento sembra che tu stia usando -> getFirstItem(), che richiederebbe che il gruppo Generale sia sempre il primo. C'è un modo per selezionare solo il gruppo Generale? Grazie ancora! –

2

per persone che hanno problemi con il codice di cui sopra,

come: chiamata a una funzione membro getModelInstance() su un non-oggetto

è necessario aggiungere quanto segue al di all'inizio del file:

include 'app/Mage.php'; 
Mage::app(); 

edit:

im utilizzando Magento 1.8.1.0 e il codice ancora non ha funzionato

ho dovuto aggiungere la seguente riga a $ newItem, in questo modo la convalida passa

->setAttributeCode($attCode) 
2

La funzione può essere utilizzata Mage_Catalog_Model_Resource_Setup::addAttribute() per aggiornare e aggiungere attributi. Un'altra cosa che trovo utile è se si specifica un gruppo con questa funzione che viene automaticamente assegnato a tutti i set.

$attributeCode = 'name'; // choose your attribute here 
$setup = Mage::getResourceSingleton('catalog/setup'); 
$setup->addAttribute('catalog_product', $attributeCode, array(
    // no need to specify fields already used like 'label' or 'type' 
    'group'  => 'General', 
    'sort_order' => 10 
)); 
+0

Quando si prova questo metodo su 1.9.3.1, ottengo" Errore fatale PHP: chiamata a una funzione membro select() su un oggetto non nell'app/code/core/Mage/Core/Modello/risorsa/Setup.php sulla linea 733 " – Eric

0

Non utilizzare

Mage::getResourceSingleton('catalog/setup'); 

ma l'uso

Mage::getResourceModel('catalog/setup', 'catalog_setup'); 
1

Se si utilizza uno script esterno (non il Magento messe a punto sceneggiatura) questo lavoro costituiscono mi

<?php 
/// run in magento root 
require_once 'app/Mage.php'; 
ini_set('display_errors', 1); 
error_reporting(E_ALL); 
Mage::app(); 

$attributeCode = 'my_attr_code'; 
$group = 'MyGroup'; 
$sortOrder = 10; 

//this way you config the setup connections 
$setup = new Mage_Catalog_Model_Resource_Setup('catalog_setup'); 

$setup->startSetup(); 
$setup->addAttribute('catalog_product', $attributeCode, array(
    'group'  => $group, 
    'sort_order' => $sortOrder 
)); 

(come risposta a @Eric)

Problemi correlati