Sto cercando di aggiungere nuovi valori di attribuire opzione in Magento utilizzando uno script per accelerare il processo dal momento che ho più di 2.000 fabbricantiAggiungere nuovi valori per un'opzione di attributo nel Magento
risposta
Ecco un pezzo di codice che ho usato per eseguire esattamente questo compito. Creare un modulo personalizzato (usando ModuleCreator come strumento) e quindi creare un mysql4-install-0.1.0.php
nella cartella sql/modulename_setup. Dovrebbe contenere quanto segue (adattato ai tuoi dati, ovviamente!).
$installer = new Mage_Eav_Model_Entity_Setup('core_setup');
$installer->startSetup();
$aManufacturers = array('Sony','Philips','Samsung','LG','Panasonic','Fujitsu','Daewoo','Grundig','Hitachi','JVC','Pioneer','Teac','Bose','Toshiba','Denon','Onkyo','Sharp','Yamaha','Jamo');
$iProductEntityTypeId = Mage::getModel('catalog/product')->getResource()->getTypeId();
$aOption = array();
$aOption['attribute_id'] = $installer->getAttributeId($iProductEntityTypeId, 'manufacturer');
for($iCount=0;$iCount<sizeof($aManufacturers);$iCount++){
$aOption['value']['option'.$iCount][0] = $aManufacturers[$iCount];
}
$installer->addAttributeOption($aOption);
$installer->endSetup();
Ulteriori documentazione su Magento wiki se lo si desidera.
Se non si desidera farlo in un modulo personalizzato, si può solo creare un file php che inizia con:
require_once 'app/Mage.php';
umask(0);
Mage::app('default');
risposta di Jonathan è corretta. Ma se si desidera eseguire senza installer cioè in qualsiasi altro codice, allora si potrebbe trovare questo utile:
$arg_attribute = 'manufacturer';
$manufacturers = array('Sony','Philips','Samsung','LG','Panasonic','Fujitsu','Daewoo','Grundig','Hitachi','JVC','Pioneer','Teac','Bose','Toshiba','Denon','Onkyo','Sharp','Yamaha','Jamo');
$attr_model = Mage::getModel('catalog/resource_eav_attribute');
$attr = $attr_model->loadByCode('catalog_product', $arg_attribute);
$attr_id = $attr->getAttributeId();
$option['attribute_id'] = $attr_id;
foreach ($manufacturers as $key=>$manufacturer) {
$option['value'][$key.'_'.$manufacturer][0] = $manufacturer;
}
$setup = new Mage_Eav_Model_Entity_Setup('core_setup');
$setup->addAttributeOption($option);
Maggiori informazioni possono essere trovate here.
Importante! (Si spera che questo aiuti qualcuno, perché ero bloccato come 2 ore con questo problema)
Se si utilizzano caratteri speciali (come ä, ö, ü, ß, ×, ...) assicurarsi di codificarli correttamente !
array_walk($manufacturers , create_function('&$val', '$val = utf8_encode($val);'));
Dal momento che tutti abbiamo PHP/5.3, una versione più leggibile e più veloce sarebbe '$ manufacturer = array_map ('utf8_encode', $ manufacturer);' –
Ecco un modo semplice e molto veloce ....
Elimina un'opzione
/* My option value */
$value = 'A value';
/* Delete an option */
$options = array();
$entity_type_id = Mage::getModel('eav/entity')->setType('catalog_product')->getTypeId(); // Product Entity Type ID
$attribute = Mage::getModel('eav/entity_attribute')->loadByCode($entity_type_id, $attribute_code); // Load attribute by code
if ($attribute && $attribute->usesSource()) {
$option_id = $attribute->getSource()->getOptionId($value); // Check Option ID from value...
if ($option_id) {
$options['delete'][$option_id] = true;
$attribute->setOption($options)->save();
}
}
/* END ! */
aggiungere o aggiornare un'opzione
/* Add/Update an option */
$options = array();
$entity_type_id = Mage::getModel('eav/entity')->setType('catalog_product')->getTypeId(); // Product Entity Type ID
$attribute = Mage::getModel('eav/entity_attribute')->loadByCode($entity_type_id, $attribute_code); // Load attribute by code
if ($attribute && $attribute->usesSource()) {
$option_id = $attribute->getSource()->getOptionId($value); // Check Option ID...
$options['order'][$option_id] = 10; // Can be removed... Set option order...
$options['value'][$option_id] = array(
0 => $value, // Admin Store - Required !
1 => $value, // Store id 1 - If U want ! Can be removed
);
$attribute->setDefault(array($option_id)); /* If you want set option as default value */
$attribute->setOption($options)->save(); /* That's all */
}
/* END ! */
ho creato una funzione per aggiungere dinamicamente l'opzione all'attributo
public function addAttributeOptions($attributeCode, $argValue)
{
$attribute = Mage::getModel('eav/config')
->getAttribute(Mage_Catalog_Model_Product::ENTITY, $attributeCode);
if ($attribute->usesSource()) {
$id = $attribute->getSource()->getOptionId($argValue);
if ($id)
return $id;
}
$value = array('value' => array(
'option' => array(
ucfirst($argValue),
ucfirst($argValue)
)
)
);
$attribute->setData('option', $value);
$attribute->save();
//return newly created option id
$attribute = Mage::getModel('eav/config')
->getAttribute(Mage_Catalog_Model_Product::ENTITY, $attributeCode);
if ($attribute->usesSource()) {
return $attribute->getSource()->getOptionId($argValue);
}
}
È possibile aggiungere un'opzione per l'attributo, fornendo il codice e l'opzione valore
$this->addAttributeOptions('unfiorm_type', 'leotartd')
Nel mio tutorial che sto spiegando come leggere le opzioni dal CSV e creare le opzioni pro grammaticalmente.
http://www.pearlbells.co.uk/add-attribute-options-magento-scripts/ clicca il tutorial per ulteriori spiegazioni
function createAttribute($options) {
$option = array('attribute_id' =>
Mage::getModel('eav/entity_attribute')->getIdByCode(
Mage_Catalog_Model_Product::ENTITY,
'color'
)
);
for ($i = 0; $i < count($options); $i++) {
$option['value']['option'.$i][0] = $options[ $i ]; // Store View
$option['value']['option'.$i][1] = $options[ $i ]; // Default store view
$option['order']['option'.$i] = $i; // Sort Order
}
$setup = new Mage_Eav_Model_Entity_Setup('core_setup');
$setup->addAttributeOption($option);
}
come faccio preforma questo senza l'installatore? È per un sito Web di produzione. – Tony
non ha funzionato quando si utilizza come script il messaggio di errore – Tony
? Posso assicurarti che funziona, ho usato esattamente quel codice. L'utilizzo di un programma di installazione è il metodo preferito per i siti di produzione. –