KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
I am trying to add products to Magento 1.5 programmatically. My script will ultimately be a cron job, regularly updating and adding products as dictated by an XML file supplied by the accounts system. I have a problem in creating new products. The relevant code segment from my script is: $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 (Mage_Core_Exception $e) { $this->_fault('data_invalid', $e->getMessage()); } The product is 'half' created, but the script gives up throwing the following error:</
Tags (comma-separated)
Save Edits
Cancel