使用php和mysql(以编程方式)将产品添加到magento

时间:2013-05-30 02:58:54

标签: magento magento-1.7

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

<?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);
?>

1 个答案:

答案 0 :(得分: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();

我希望它有所帮助