Wie füge ich programmgesteuert Attributoptionswerte in ein Datenaktualisierungsskript ein?


Antworten:


12

Fügen Sie der Upgrade-Skriptdatei den folgenden Code hinzu

<?php   
$installer = new Mage_Eav_Model_Entity_Setup('core_setup');
$installer->startSetup();

$attributeCode = 'manufacturer';
$attribute = Mage::getModel('eav/entity_attribute')->loadByCode('catalog_product', $attributeCode);

if ($attribute->getId() && $attribute->getFrontendInput()=='select') {
    $option['attribute_id'] = $attribute->getId();
    $option['value']        =  array('Red','Black', 'Yellow');
    $installer->addAttributeOption($option);
}

//OR
/*
if($attribute->getId() && $attribute->getFrontendInput()=='select') {
    $option['attribute_id'] = $attribute->getId();
    $option['value']['r'][0] = 'Red';
    $option['value']['b'][1] = 'Black';
    $option['value']['y'][2] = 'Yellow';
    $installer->addAttributeOption($option);
}*/

$installer->endSetup();

Überprüfen Sie den doppelten Optionswertcode:

<?php   
$installer = new Mage_Eav_Model_Entity_Setup('core_setup');
$installer->startSetup();

$attributeCode = 'manufacturer';
$attribute = Mage::getModel('eav/entity_attribute')->loadByCode('catalog_product', $attributeCode);

 if($attribute->getId() && $attribute->getFrontendInput()=='select') {
    $newOptions =  array('Red','Black', 'Yellow');
    $exitOptions =  array();
    $options = Mage::getModel('eav/entity_attribute_source_table')
                        ->setAttribute($attribute)
                        ->getAllOptions(false);
    foreach ($options as $option) {
        if (in_array($option['label'], $newOptions)) {
            array_push($exitOptions, $option['label']);
        }else {

        }
    }
    $insertOptions = array_diff($newOptions, $exitOptions);
    if(!empty($insertOptions)) {
        $option['attribute_id'] = $attribute->getId();
        $option['value']        =  $insertOptions;  
        $installer->addAttributeOption($option);
    }            
}

$installer->endSetup();

1
Was ist die Bedeutung von Indizes 'r', 'b', 'y'in $option['value']['r'][0] = 'Red';?
Anton Belonovich

1
Dies führt hier auf Magento CE 1.9 zu einer fehlerhaften Dropdown-Option. Tabelle eav_attribute_optionerhält eine neue Zeile, jedoch ohne entsprechende Zeile eav_attribute_option_value. Muss etwas mit der $optionArray-Struktur sein.
Anse

Können Sie mir bitte helfen, den Attributwert zu überprüfen, wenn dieser Wert bereits verfügbar ist? Also doppelter Wert nicht in Attribut einfügen
Purushotam Sharma

@ Purushotam Sharma: Updates und pls überprüfen und lassen Sie mich wissen, dass es funktioniert oder nicht, da ich kein getesteter Code bin.
Abdul

Hallo @Abdul! Es wird keine Option hinzugefügt, während dieses Skript ausgeführt wird
SagarPPanchal

12

Versuche dies,

für Einzelwert: -

$arg_attribute = 'color';
$arg_value = 'red';

$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;
$option['value']['any_option_name'][0] = $arg_value;

$setup = new Mage_Eav_Model_Entity_Setup('core_setup');
$setup->addAttributeOption($option);

für mehrere Werte: -

$arg_attribute = 'color';
$key_data = array('red','black','orange');
$setup = new Mage_Eav_Model_Entity_Setup('core_setup');
$attr_model = Mage::getModel('catalog/resource_eav_attribute');
$attr = $attr_model->loadByCode('catalog_product', $arg_attribute);
foreach($key_data as $key_value)
{   
    $option = array();
    $arg_value = trim($key_value);
    $attr_id = $attr->getAttributeId();
    $option['attribute_id'] = $attr_id;
    $option['value']['any_option_name'][0] = $arg_value;
    $setup->addAttributeOption($option);
}

'any_option_name' wäre ein color_name (zB: red) arg_value wäre eine ganzzahlige optionId afaik.

Das, was auch zuerst erworben werden müsste, ist die nächste nicht verwendete Options-ID. Wird für diese neue Attributoption verwendet.


6

Zum Beispiel wollen Sie hinzufügen MenWert genderOption.

Zuerst müssen Sie Ihr Upgrade-Skript im Modulverzeichnis erstellen, z app/code/local/MyCompany/MyModule/data/mymodule_setup/data-upgrade-0.1.0-0.1.1.php.

Dann füllen Sie es mit Code wie folgt:

<?php

$this->startSetup();

$genderAttribute = Mage::getModel('eav/entity_attribute')
    ->loadByCode('catalog_product', 'gender'); // 'gender' is your attribute code

$this->addAttributeOption([
    'attribute_id' => $genderAttribute->getId(),
    'value' => [[0 => 'Men', 1 => 'Men', 10 => 'Men']] // array indexes are store IDs
]);

$this->endSetup();

2

Der folgende Code fügt Attribute programmgesteuert magento 1 hinzu.

Ausführliche Erläuterungen zum Lesen aus CSV und zum Vergleichen mit vorhandenen Attributoptionen finden Sie unter https://www.pearlbells.co.uk/add-attribute-options-magento-scripts/.

function createAttribute( $options , $attributeCode) {
$option = array('attribute_id' => 
Mage::getModel('eav/entity_attribute')->getIdByCode(
     Mage_Catalog_Model_Product::ENTITY, 
     $attributeCode
    )
);

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
    echo 'Insert new option : '.$options[ $i ].PHP_EOL;

}

$setup = new Mage_Eav_Model_Entity_Setup('core_setup');
$setup->addAttributeOption($option);
}
Durch die Nutzung unserer Website bestätigen Sie, dass Sie unsere Cookie-Richtlinie und Datenschutzrichtlinie gelesen und verstanden haben.
Licensed under cc by-sa 3.0 with attribution required.