简体   繁体   English

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

[英]Add product in magento Programmatically

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 致命错误 :消息为“ SQLSTATE [23000]”的未捕获异常“ PDOException”:完整性约束

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 违规:1452不能添加或更新子行,外键约束失败( DBNAMEcatalog_product_entity ,约束FK_CAT_PRD_ENTT_ATTR_SET_ID_EAV_ATTR_SET_ATTR_SET_ID外键( attribute_set_id )参考eav_attribute_setattribute_set_id )O)”在/ var / WWW / XXX / lib中/的Zend / DB /声明/ Pdo .php:228

Stack trace: 堆栈跟踪:

0 /var/www/xxx/lib/Zend/Db/Statement/Pdo.php(228): PDOStatement->execute(Array) 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 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 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) 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 4 /var/www/xxx/lib/Zend/Db/Adapter/Pdo/Abstract.php(238):Zend_Db_Adapter_Abstract-> query('INSERT

INTO `ca...', Array) INTO`ca ...',数组)

5 /var/www/xxx/lib/Varien/Db/Adapter/Pdo/Mysql.php(428): Zend_Db_Adapter_Pdo_Abstract->qu in 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 /var/www/xxx/lib/Zend/Db/Statement/Pdo.php,第234

I got the answer:- I set the category id by using 我得到了答案:-我通过使用设置类别ID

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

And now I used 现在我用

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM