I'm importing my products from a .csv and I have some trouble setting the custom attribute values. The attribute types are dropdown since they are used for configurable products.
error_reporting(E_ALL);
include_once "../app/Mage.php";
include_once "../downloader/Maged/Controller.php";
Mage::init();
// Set the store id
$app = Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
$Simpleproduct = Mage::getModel('catalog/product');
try{
$Simpleproduct
->setWebsiteIds(array(1))
->setAttributeSetId(4)
->setTypeId('simple')
->setCreatedAt(strtotime('now'))
->setSku('testsku61') //SKU
->setName('test product21')
->setWeight(4.0000)
->setPrice(11.22) //price in form 11.22
->setDescription('This is a long description')
->setShortDescription('This is a short description')
->setStockData(array(
'use_config_manage_stock' => 0,
'manage_stock'=>1,
'min_sale_qty'=>1, //Minimum Qty Allowed in Shopping Cart
'max_sale_qty'=>2, //Maximum Qty Allowed in Shopping Cart
'is_in_stock' => 1, //Stock Availability
'qty' => 999 //qty
)
)
->setCategoryIds(array(3, 11))
$data=array(
'afmeting' => '180 x 100',
'afwerkingblad '=> 'Havanna',
'afwerkingonderstel '=> 'Blank');
//Mage::getSingleton('catalog/product_action')->updateAttributes(array($Simpleproduct->getId()), $data, $storeId); // Found out this is only for updating
$Simpleproduct->setData($data); // this doesn't work either
if($Simpleproduct->save()){
echo "test";
}
}catch(Exception $e){
Mage::log($e->getMessage());
echo $e;
echo "faal";
}
Whenever I run the code I get the following error:
exception 'PDOException' with message 'SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`db90927_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_id)'
Best Answer
Your code looks like there are PHP syntax errors. For example:
Also, when you call
you're replacing the data on the product instance. I think you want to do:
As for the SQL exception, it's a foreign key exception to the eav_attribute_set table. Can you double check that
there's an attribute set with id 4?