我正在尝试从 magento 前端创建产品,但是在执行 php 代码时我收到此错误:
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)
我在这里找到:Create a product from PHP - Magento这应该是属性集 ID 的问题,但我尝试从 eav_attribute_set 表中强制 ID,然后使用函数:
Mage::getModel('catalog/config')->getAttributeSetId('catalog_product','Set_evento')
并使用上面的此功能,但具有“默认”值。没变化。所以也许问题不在于属性集 ID?
这是我的代码:
$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();
谢谢!