2015-12-09 121 views
0

我想在磁grammatically.My代碼來保存產品添加產品如下: -添加產品在Magento編程

 $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。,約束FK_CAT_PRD_ENTT_ATTR_SET_ID_EAV_ATTR_SET_ATTR_SET_ID外鍵(attribute_set_id )在/ var/WWW參考文獻eav_attribute_setattribute_set_id)O)」/XXX/LIB /的Zend/DB /報表/ PDO .PHP:228

堆棧跟蹤:

0 /var/www/xxx/lib/Zend/Db/Statement/Pdo.php(228):PDOStatement- >執行(陣列)

1 /無功/網絡/ XXX/LIB /瓦瑞恩/ DB /聲明/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

(陣列)

3 /無功/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->查詢('INSERT

INTO`CA ...',陣列)

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

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

回答

0

我得到了答案: - 我用

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

設置類別ID和現在我用

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

其中$類= '2,3,5';