2014-04-14 34 views
1

我有這樣的代碼:magento自動產品添加工程,但如何添加下拉菜單值?

require_once('app/Mage.php'); 

Mage::app(); 

$product = Mage::getModel('catalog/product'); 


$product->setSku("productSKU"); 
$product->setAttributeSetId(9); 
$product->setTypeId('simple'); 
$product->setName("Product name"); 
$product->setCategoryIds("2,3,4,5,6"); 
$product->setWebsiteIDs(array(0,1)); 
$product->setDescription("Mydesc"); 
$product->setShortDescription("mydesc2"); 
$product->setPrice(100); 
$product->setWeight(5.00); 
$product->setVisibility(4); 
$product->setStatus(1); 
$product->setTaxClassId(1); 
$product->setStockData(array(
    'is_in_stock' => 1, 
    'qty' => 10 
)); 
$product->setCreatedAt(strtotime('now')); 
$product->save(); 

和工作完美。

對接,現在我需要在這裏添加一個屬性,稱爲「tyretype」。 我在該屬性中添加了幾行下拉菜單,例如「100」,「101」,「102」。

現在,如果產品有tyretype = 101,我如何在該代碼中添加該值?

請幫忙,謝謝。

而我用magento 1.7運行。

回答

0

首先,取option選項值,並使用下面的代碼

$attributeBasictyretype = Mage::getModel('eav/config')->getAttribute('catalog_product', 'tyretype'); 

     if ($attributeBasictyretype->usesSource()) { 
     $optionsBasic = $attributeBasictyretype->getSource()->getAllOptions(false,true); 
     } 

$ optionsBasic選項ID和選項名稱(標籤)的陣列格式給予列表屬性的選項標識。

如果你想添加選項的產品,那麼你需要 附加設置選項ID產品。

/* Suppose: 100 option id is 15,101 option id is 17,102 option id is 13,then */ 

    /*if attribute tyretype is multi select */ 
    $product->setTyretype(array(15,17,13)); 
//if attribute is dropdown.you can add only option 
    $product->setTyretype(15);