i'm trying to Create a Product from magento Frontend, but when execute the php code i receive this error:
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_id`) ON DE)
I found here: Create a product from PHP - Magento that should be a problem with attribute set ID, but i tried force the ID from eav_attribute_set table, then with the function:
Mage::getModel('catalog/config')->getAttributeSetId('catalog_product','Set_evento')
And with this function above but with "Default" value. Nothing change. So maybe the problem is not Attribute Set ID?
This is my code:
$product = Mage::getModel('catalog/product');
$product->setSku(time());
$product->setName("Evento senza nome");
$product->setDescription("123");
$product->setShortDescription("1234");
$product->setPrice(0.00);
$product->setTypeId('virtual');
$attributeSetId = Mage::getModel('catalog/config')->getAttributeSetId('catalog_product','Set_evento');
$product->setAttributeSetId($attributeSetId);
$product->setCategoryIds(array($cat_id));
$product->setVisibility(4); // catalog, search
$product->setStatus(1); // enabled
// assign product to the default website
$product->setWebsiteIds(array(Mage::app()->getStore(true)->getWebsite()->getId()));
// for stock
$stockData = $product->getStockData();
$stockData['qty'] = 1;
$stockData['is_in_stock'] = 1;
$product->setStockData($stockData);
$product->setCreatedAt(strtotime('now'));
Mage::app()->getStore()->setId(Mage_Core_Model_App::ADMIN_STORE_ID);
$product->save();
Thanks!