我正在尝试以编程方式将产品添加到Magento 1.5.我的脚本最终将成为一个cron作业,定期更新和添加由帐户系统提供的XML文件指定的产品.
我在创建新产品时遇到问题.我脚本中的相关代码段是:
$attributeSetId = 4;
//$newproduct = Mage::getModel('catalog/product');
$newproduct = new Mage_Catalog_Model_Product();
$newproduct->setTypeId('simple');
$newproduct->setWeight($product->UnitWeight);
$newproduct->setVisibility(Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH);
$newproduct->setStatus(1);
$newproduct->setSKU($SKU);
$newproduct->setTaxClassId(0);
$newproduct->setWebsiteIDs(array(0));
$newproduct->setStoreIDs(array(1));
$newproduct->setStockData(array(
'is_in_stock' => 1,
'qty' => $XMLproduct->QtyInStock,
'manage_stock' => 1
));
$newproduct->setAttributeSetId(4);
$newproduct->setName($product->Name);
$newproduct->setCategoryIds(array(3)); // array of categories it will relate to
$newproduct->setDescription($product->LongDescription);
$newproduct->setShortDescription($product->Description);
$newproduct->setPrice($XMLproduct->SalePrice);
try {
if (is_array($errors = $newproduct->validate())) {
$strErrors = array();
foreach($errors as $code=>$error) {
$strErrors[] = ($error === true)? Mage::helper('catalog')->__('Attribute "%s" is invalid.', $code) : $error;
}
$this->_fault('data_invalid', implode("\n", $strErrors));
}
$newproduct->save();
} catch …Run Code Online (Sandbox Code Playgroud)