2017-05-24 35 views
0

我想导入magento中的产品。下面是该脚本:无法使用脚本导入Magento中的产品

<?php 

$mageFilename = 'app/Mage.php'; 
require_once $mageFilename; 
ini_set('display_errors', 1); 
umask(0); 
Mage::app('admin'); 
Mage::register('isSecureArea', 1); 

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

$product->setSku("ABC123"); 
$product->setName("Name"); 
$product->setDescription("Desc."); 
$product->setShortDescription("Desc2."); 
$product->setPrice(70.50); 
$product->setTypeId('simple'); 
$product->setAttributeSetId(9); 
$product->setCategoryIds("20,24"); 
$product->setWeight(1.0); 
$product->setTaxClassId(2); 
$product->setVisibility(4); 
$product->setStatus(1); 

$product->setWebsiteIds(array(Mage::app()->getStore(true)->getWebsite()->getId())); 

$product->save();  

?> 

的问题是结果:

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 (magento . catalog_product_entity , CONSTRAINT FK_CAT_PRD_ENTT_ATTR_SET_ID_EAV_ATTR_SET_ATTR_SET_ID FOREIGN KEY (attribute_set_id) REFERENCES eav_attribute_set (attribute_set_i)' in /home/zelewe3k/public_html/lib/Zend/Db/Statement/Pdo.php:228 Stack trace: #0 /home/zelewe3k/public_html/lib/Zend/Db/Statement/Pdo.php(228): PDOStatement->execute(Array) #1 /home/zelewe3k/public_html/lib/Varien/Db/Statement/Pdo/Mysql.php(110): Zend_Db_Statement_Pdo->_execute(Array) #2 /home/zelewe3k/public_html/app/code/core/Zend/Db/Statement.php(291): Varien_Db_Statement_Pdo_Mysql->_execute(Array) #3 /home/zelewe3k/public_html/lib/Zend/Db/Adapter/Abstract.php(480): Zend_Db_Statement->execute(Array) #4 /home/zelewe3k/public_html/lib/Zend/Db/Adapter/Pdo/Abstract.php(238): Zend_Db_Adapter_Abstract->query('INSERT INTO ca...', Array) #5 /home/zelewe3k/pu in /home/zelewe3k/public_html/lib/Zend/Db/Statement/Pdo.php on line 234

我能做些什么? 如何解决它!?

+0

如果这是一个新产品,它的属性设置'9'实际上存在吗?如果它是现有的产品,您应该*能够更改属性集 - 它在'DELETE'或'UPDATE'上设置为'CASCADE',但是我担心会留在数据库中的那种混乱。你可能会更好使用*系统 - >导入/导出 - >数据流配置文件*。 – CD001

回答

0

它看起来像属性设置ID 9不存在。请检查您的属性集ID是存在在Magento或尝试

$product->setAttributeSetId(4);