0
votes

I want to import product in magento. Here is the script:

<?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();     

?>

The problem is the result:

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 INTOca...', Array) #5 /home/zelewe3k/pu in /home/zelewe3k/public_html/lib/Zend/Db/Statement/Pdo.php on line 234

What can I do? How to fix it!?

1
If this is a new product, does attribute set 9 actually exist? If it's an existing product you should be able to change the attribute set - it's set to CASCADE on DELETE or UPDATE but I dread the think of the kind of mess that'll leave in the database. You'd probably be better off using System -> Import/Export -> Dataflow Profiles.CD001

1 Answers

0
votes

Its looks like attribute set id 9 does not exist .Please check you attribute set id is exists in Magento or try

$product->setAttributeSetId(4);