1

这是我正在使用的代码,它返回服务器错误。我不知道为什么或做什么才能让它工作。任何帮助将不胜感激。

<?php
//product = Mage::getModel('catalog/product');

error_reporting(E_ALL | E_STRICT);
$mageFilename = 'app/Mage.php';
require_once $mageFilename;
Mage::setIsDeveloperMode(true);
umask(0);
Mage::app();
Mage::app()->setCurrentStore(Mage::getModel('core/store')->load(Mage_Core_Model_App::ADMIN_STORE_ID));


$product = new Mage_Catalog_Model_Product();
    $product->setSku("ABC123");
    $product->setName("Type 7 Widget");
    $product->setDescription("This widget will give you years of trouble-free widgeting.");
    $product->setShortDescription("High-end widget.");
    $product->setPrice(70.50);
    $product->setTypeId('simple');
    $product->setAttributeSetId(9); // need to look this up
    $product->setCategoryIds("20,24"); // need to look these up
    $product->setWeight(1.0);
    $product->setTaxClassId(2); // taxable goods
    $product->setVisibility(4); // catalog, search
    $product->setStatus(1); // enabled

    // assign product to the default website
    $product->setWebsiteIds(array(Mage::app()->getStore(true)->getWebsite()->getId()));

    $product->save();

// for stock

$stockData = $product->getStockData();
$stockData['qty'] = 10;
$stockData['is_in_stock'] = 1;
$stockData['manage_stock'] = 1;
$stockData['use_config_manage_stock'] = 0;
$product->setStockData($stockData);
?>
4

1 回答 1

1

使用 Magento,我们可以有几种不同的方式将产品添加到目录中。您尝试实现的方式需要进行一些更改才能正常工作。请记住,您应该尽可能多地使用 Magento 的功能,例如模型、配置等。

您正在寻找的一个好方法是:

<?php 
...

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

$product->setSku("SKUPROD123");
$product->setName("Name of The Product");
$product->setDescription("Some description of the product.");
$product->setShortDescription("Short one.");
$product->setPrice(299.50);
$product->setTypeId('simple');
$product->setAttributeSetId(9); // need to look this up
$product->setCategoryIds("20,24"); // need to look these up
$product->setWeight(1.8);
$product->setTaxClassId(2); // taxable goods
$product->setVisibility(4); // sets visibility as 'catalog, search'
$product->setStatus(1); // enabled

// assign product to the default website
$product->setWebsiteIds(array(Mage::app()->getStore(true)->getWebsite()->getId()));

$product->save();

我希望它有帮助

于 2013-05-30T03:49:49.863 回答