我正嘗試以編程方式將產品添加到Magento 1.5。我的腳本最終將成爲一個cron工作,定期更新和添加由帳戶系統提供的XML文件指定的產品。Magento:以編程方式添加新產品
我在創建新產品時遇到問題。從我的腳本中的相關代碼段爲:
$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());
}
該產品是「半壁江山」創建,但腳本放棄拋出以下錯誤:
PHP Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`LittleDickyBird`.`catalog_category_product_index`, CONSTRAINT `FK_CATALOG_CATEGORY_PROD_IDX_CATEGORY_ENTITY` FOREIGN KEY (`category_id`) REFERENCES `catalog_category_entity` (`entity_id`) ON)' in /home/default/littledickybird.co.uk/user/htdocs/1.5/lib/Zend/Db/Statement/Pdo.php:228
Stack trace:
#0 /home/default/littledickybird.co.uk/user/htdocs/1.5/lib/Zend/Db/Statement/Pdo.php(228): PDOStatement->execute(Array)
#1 /home/default/littledickybird.co.uk/user/htdocs/1.5/lib/Zend/Db/Statement.php(300): Zend_Db_Statement_Pdo->_execute(Array)
#2 /home/default/littledickybird.co.uk/user/htdocs/1.5/lib/Zend/Db/Adapter/Abstract.php(479): Zend_Db_Statement->execute(Array)
#3 /home/default/littledickybird.co.uk/user/htdocs/1.5/lib/Zend/Db/Adapter/Pdo/Abstract.php(238): Zend_Db_Adapter_Abstract->query('INSERT INTO `ca...', Array)
#4 /home/default/littledic in /home/default/littledickybird.co.uk/user/htdocs/1.5/lib/Zend/Db/Statement/Pdo.php on line 234
任何人都可以,請拋出任何光照射到我錯過了什麼或者做錯了什麼。正如你從我的口氣中可以看出的那樣,我非常絕望,所以任何幫助都會非常感激。
謝謝
當初,是由MySQL的打開InnoDB的解決類似的問題。可能想在那裏檢查。希望有所幫助! – Nic
不幸的是,這似乎不是問題,因爲InnoDB已經處於活動狀態。但是謝謝你的幫助。 – AMP
錯誤提示您嘗試使用不存在的類別標識。你確定有一個類別3? – clockworkgeek