我有这段代码:
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 :(得分:0)
首先,使用下面的代码
获取选项选项值和属性的选项ID$attributeBasictyretype = Mage::getModel('eav/config')->getAttribute('catalog_product', 'tyretype');
if ($attributeBasictyretype->usesSource()) {
$optionsBasic = $attributeBasictyretype->getSource()->getAllOptions(false,true);
}
$ optionsBasic 以数组格式提供选项ID列表和选项名称(标签) 。
如果您想为产品添加选项,则需要 将set option id添加到product。
/* 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);