我想用magto语法添加产品。我保存产品的代码如下: -
$product = Mage::getModel('catalog/product');
$product
->setAttributeSetId(4)
->setTypeId('simple')
->setCreatedAt(strtotime('now'))
->setSku($product_sku)
->setName($product_name)
->setWeight($product_weight)
->setStatus($Status)
->setTaxClassId(2)
->setManufacturer($product_manufacturer)
->setCountryOfManufacture('US')
->setPrice($product_price)
->setCost($product_price)
->setSpecialFromDate(strtotime('now'))
->setSpecialToDate() */
->setMsrpEnabled(4)
->setMsrpDisplayActualPriceType(4)
->setMsrp($wholesale_price)
->setDescription($product_short_desc)
->setShortDescription($product_short_desc)
->setVisibility(4);
$mediaArray = array(
'thumbnail' => $imgpath,
//'small_image' => $imagepath,
// 'image' => $imagepath,
);
$product->setStockData(array(
'use_config_manage_stock' => 1, //'Use config settings' checkbox
'manage_stock' => 1, //manage stock
'min_sale_qty' => 1, //Minimum Qty Allowed in Shopping Cart
'max_sale_qty' => 200, //Maximum Qty Allowed in Shopping Cart
'is_in_stock' => 1, //Stock Availability
'qty' => $product_quantity //qty
)
)
->setCategoryIds($categories); //assign product to categories
if ($product->save())
{
$_product = Mage::getModel('catalog/product')
->loadByAttribute('sku', $product_sku);
echo 'Added successfully';
}
}
执行上面的代码后,我收到以下错误: -
致命错误:未捕获的异常'PDOException',消息为'SQLSTATE [23000]:完整性约束
违规:1452无法添加或更新子行:外键约束失败(DBNAME
。catalog_product_entity
,CONSTRAINT FK_CAT_PRD_ENTT_ATTR_SET_ID_EAV_ATTR_SET_ATTR_SET_ID
FOREIGN KEY(attribute_set_id
)/ var / www / xxx / lib / Zend / Db / Statement / Pdo中的参考eav_attribute_set
(attribute_set_id
)O)'
.PHP:228
堆栈追踪:
)
(阵列)
INTO`ca ...',Array)
5 /var/www/xxx/lib/Varien/Db/Adapter/Pdo/Mysql.php(428):Zend_Db_Adapter_Pdo_Abstract-> qu in第 234 行 /xx/www/xxx/lib/Zend/Db/Statement/Pdo.php
答案 0 :(得分:0)
我得到了答案: - 我使用
设置了类别ID ->setCategoryIds($categories); //assign product to categories
现在我用了
->setCategoryIds(array($categories)); //assign product to categories
其中$ categories =' 2,3,5&#39 ;;