Magento:以编程方式添加新产品

时间:2011-07-18 15:40:55

标签: magento magento-1.5

我正在尝试以编程方式将产品添加到Magento 1.5。我的脚本最终将成为一个cron作业,定期更新和添加由帐户系统提供的XML文件指定的产品。

我在创建新产品时遇到问题。我脚本中的相关代码段是:

    $attributeSetId = 4;

    //$newproduct = Mage::getModel('catalog/product');
    $newproduct = new Mage_Catalog_Model_Product();

    $newproduct->setTypeId('simple');
    $newproduct->setWeight($product->UnitWeight);       
    $newproduct->setVisibility(Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH); 
    $newproduct->setStatus(1);
    $newproduct->setSKU($SKU);
    $newproduct->setTaxClassId(0);
    $newproduct->setWebsiteIDs(array(0)); 
    $newproduct->setStoreIDs(array(1)); 
    $newproduct->setStockData(array( 
        'is_in_stock' => 1, 
        'qty' => $XMLproduct->QtyInStock,
        'manage_stock' => 1
    )); 

    $newproduct->setAttributeSetId(4);
    $newproduct->setName($product->Name);
    $newproduct->setCategoryIds(array(3)); // array of categories it will relate to

    $newproduct->setDescription($product->LongDescription);
    $newproduct->setShortDescription($product->Description);
    $newproduct->setPrice($XMLproduct->SalePrice);

    try {
        if (is_array($errors = $newproduct->validate())) {
            $strErrors = array();
            foreach($errors as $code=>$error) {
                $strErrors[] = ($error === true)? Mage::helper('catalog')->__('Attribute "%s" is invalid.', $code) : $error;
            }
            $this->_fault('data_invalid', implode("\n", $strErrors));
        }

        $newproduct->save();
    } catch (Mage_Core_Exception $e) {
        $this->_fault('data_invalid', $e->getMessage());
    }

产品是'half'创建的,但脚本放弃了抛出以下错误:

PHP 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 (`LittleDickyBird`.`catalog_category_product_index`, CONSTRAINT `FK_CATALOG_CATEGORY_PROD_IDX_CATEGORY_ENTITY` FOREIGN KEY (`category_id`) REFERENCES `catalog_category_entity` (`entity_id`) ON )' in /home/default/littledickybird.co.uk/user/htdocs/1.5/lib/Zend/Db/Statement/Pdo.php:228
Stack trace:
#0 /home/default/littledickybird.co.uk/user/htdocs/1.5/lib/Zend/Db/Statement/Pdo.php(228): PDOStatement->execute(Array)
#1 /home/default/littledickybird.co.uk/user/htdocs/1.5/lib/Zend/Db/Statement.php(300): Zend_Db_Statement_Pdo->_execute(Array)
#2 /home/default/littledickybird.co.uk/user/htdocs/1.5/lib/Zend/Db/Adapter/Abstract.php(479): Zend_Db_Statement->execute(Array)
#3 /home/default/littledickybird.co.uk/user/htdocs/1.5/lib/Zend/Db/Adapter/Pdo/Abstract.php(238): Zend_Db_Adapter_Abstract->query('INSERT INTO `ca...', Array)
#4 /home/default/littledic in /home/default/littledickybird.co.uk/user/htdocs/1.5/lib/Zend/Db/Statement/Pdo.php on line 234

任何人都可以,不管我错过或做错了什么。正如你可以从我的语气中说出的那样,我非常绝望,所以任何帮助都会非常感激。

谢谢

2 个答案:

答案 0 :(得分:21)

OP的{p> AMP已经自我回答了这个问题。

引用:

  很酷,我发现了问题,该行:   $newproduct->setWebsiteIDs(array(0));应该是:   $newproduct->setWebsiteIDs(array(1));令人惊讶的是最小的东西   可以浪费几个小时!

注意:这绝对是OK to self-answer你自己的问题。请将其作为真实答案发布,但在问题或评论中。发布真实答案有助于更清楚地保持“未答复”清单(避免让其他人浪费时间)。

答案 1 :(得分:1)

@Jurgen和@Amp的答案都很完美。

我认为这可以这样做,所以它变得更加动态

$newproduct->setWebsiteIds(array(Mage::app()->getStore(true)->getWebsite()->getId()));