dshu1235 2015-12-09 05:58
浏览 31

以编程方式在magento中添加产品

I want to add product in magneto grammatically.My code to save product is below:-

       $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';
        }
    }

After executing the above code,I got the following error:-

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 (DBNAME.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) O)' in /var/www/xxx/lib/Zend/Db/Statement/Pdo .php:228

Stack trace:

0 /var/www/xxx/lib/Zend/Db/Statement/Pdo.php(228): PDOStatement->execute(Array)

1 /var/www/xxx/lib/Varien/Db/Statement/Pdo/Mysql.php(110): Zend_Db_Statement_Pdo->_execute(Array

)

2 /var/www/xxx/app/code/core/Zend/Db/Statement.php(291): Varien_Db_Statement_Pdo_Mysql->_execute

(Array)

3 /var/www/xxx/lib/Zend/Db/Adapter/Abstract.php(479): Zend_Db_Statement->execute(Array)

4 /var/www/xxx/lib/Zend/Db/Adapter/Pdo/Abstract.php(238): Zend_Db_Adapter_Abstract->query('INSERT

INTO `ca...', Array)

5 /var/www/xxx/lib/Varien/Db/Adapter/Pdo/Mysql.php(428): Zend_Db_Adapter_Pdo_Abstract->qu in

/var/www/xxx/lib/Zend/Db/Statement/Pdo.php on line 234

  • 写回答

1条回答 默认 最新

  • dsa89029 2015-12-09 06:59
    关注

    I got the answer:- I set the category id by using

        ->setCategoryIds($categories); //assign product to categories
    

    And now I used

        ->setCategoryIds(array($categories)); //assign product to categories
    

    where $categories = '2,3,5';

    评论

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大