2013-10-06 15 views
6

Ich habe ziemlich große Import-Skript erstellt, die Produkte von CSV zu Magento importiert. Ich habe noch ein Problem zu lösen.Einstellung Magento Dropdown (Auswahl) Wert für Produkt

Ich verwende Dropdown-Felder für Attribute. Leider kann ich für diese Attribute keine Werte für ein einzelnes Produkt festlegen. Was ich tat:

  • erstellt Attribut [php],
  • hinzugefügt Drop-Down-Attribut mit Werten zu diesem Set [php],
  • neues Produkt in der richtigen Attribute hinzugefügt und versuchte, Wert für das Attribut festlegen Ich habe erschaffen.

Ich habe versucht, einige Methoden, hier ist die one gut für mich suchen:

private function setOrAddOptionAttribute($product, $arg_attribute, $arg_value) { 
    $attribute_model = Mage::getModel('eav/entity_attribute'); 
    $attribute_options_model = Mage::getModel('eav/entity_attribute_source_table'); 

    $attribute_code = $attribute_model->getIdByCode('catalog_product', $arg_attribute); 

    $attribute = $attribute_model->load($attribute_code); 

    $attribute_options_model->setAttribute($attribute); 
    $options = $attribute_options_model->getAllOptions(false); 

    // determine if this option exists 
    $value_exists = false; 
    foreach($options as $option) { 
     if ($option['label'] == $arg_value) { 
      $value_exists = true; 
      break; 
     } 
    } 

    // if this option does not exist, add it. 
    if (!$value_exists) { 
     $attribute->setData('option', array(
      'value' => array(
       'option' => array($arg_value,$arg_value) 
      ) 
     )); 
     $attribute->save(); 
    } 


    $product->setData($arg_attribute, $arg_value); 
    $product->save(); 
} 

Leider funktioniert es nicht. Irgendwelche Ideen? Ich bin mit Magento 1.7.0.2

+0

überprüfen diese, vielleicht hilft es. http://magento.stackexchange.com/a/7146/146 – Marius

+0

Versuchen Sie, die 2 Operationen zu trennen: Erstellen Sie zuerst alle erforderlichen Attributoptionen, und weisen Sie dem Produkt das Optionsattribut zu. – OSdave

Antwort

11

Sie können es wie folgt tun:

$attr_id = $this->attributeValueExists('manufacturer', 'Samsung'); 
$product->setData('manufacturer', $attr_id); 
$product->save(); 

public function attributeValueExists($arg_attribute, $arg_value) 
{ 
    $attribute_model  = Mage::getModel('eav/entity_attribute'); 
    $attribute_options_model= Mage::getModel('eav/entity_attribute_source_table') ; 

    $attribute_code   = $attribute_model->getIdByCode('catalog_product', $arg_attribute); 
    $attribute    = $attribute_model->load($attribute_code); 

    $attribute_table  = $attribute_options_model->setAttribute($attribute); 
    $options    = $attribute_options_model->getAllOptions(false); 

    foreach($options as $option) 
    { 
     if ($option['label'] == $arg_value) 
     { 
      return $option['value']; 
     } 
    } 

    return false; 
} 

Lassen Sie mich wissen, wenn Sie Fragen haben.

Herzlich.

+0

Funktioniert wie ein Charme –

3

ich die ursprüngliche Funktion neu geschrieben, so dass es für Text und Feldern arbeitet:

public function setOrAddOptionAttribute($product, $arg_attribute, $arg_value) { 
    $attribute_model = Mage::getModel('eav/entity_attribute'); 
    $attribute_options_model = Mage::getModel('eav/entity_attribute_source_table'); 

    $attribute_code = $attribute_model->getIdByCode('catalog_product', $arg_attribute); 
    $attribute = $attribute_model->load($attribute_code); 
    $attributeType = $attribute->getFrontendInput(); 
    if ($attributeType == 'select') { 

     $attribute_options_model->setAttribute($attribute); 
     $options = $attribute_options_model->getAllOptions(false); 

     // determine if this option exists 
     $value_exists = false; 
     foreach ($options as $option) { 
      if ($option['label'] == $arg_value) { 
       $value_exists = true; 
       break; 
      } 
     } 

     // if this option does not exist, add it. 
     if (!$value_exists) { 
      $attribute->setData('option', array(
       'value' => array(
        'option' => array($arg_value, $arg_value) 
       ) 
      )); 
      $attribute->save(); 
      // Now get the option value for this newly created attribute value 
      $_product = Mage::getModel('catalog/product'); 
      $attr = $_product->getResource()->getAttribute($arg_attribute); 
      if ($attr->usesSource()) { 
       $option['value'] = $attr->getSource()->getOptionId($arg_value); 
      } 

     } 
     $product->setData($arg_attribute, $option['value']); 
    } else { 
     $product->setData($arg_attribute, $arg_value); 
    } 
} 
Verwandte Themen