首页 文章

从PHP创建产品 - Magento

提问于
浏览
1

我的一个朋友最近创建了一个magento网上商店 . 他希望在他的商店里有几千种产品,并要求制造商给他一些他可以简单地导入magento的数据 . 制造商说他不拥有这样的东西或者不想放弃它(我不是100%肯定这个) . 但他被允许使用他网站上的所有产品信息来完成他的网店 . 所以我写了一个抓取所有信息的webcrawler . 经过一番研究,人们告诉我不要使用SQL插入所有信息,而是使用PHP脚本和Magento API .

我有以下脚本的一些问题 . 它应该创建一个产品以添加到Magento数据库中 . 插入值是测试值 .

<?php
error_reporting(E_ALL | E_STRICT);
ini_set('display_errors', 1);
require("app/Mage.php");
echo "Test \n";
Mage::init();

$product = Mage::getModel('catalog/product');
$product->setName('Peter Parker');
$product->setDescription('Peter Parker Description');

$stock_data=array(
'use_config_manage_stock' => 0,
'qty' => 0,
'min_qty' => 0,
'use_config_min_qty'=>0,
'min_sale_qty' => 1,
'use_config_min_sale_qty'=>1,
'max_sale_qty' => 9999,
'use_config_max_sale_qty'=>1,
'is_qty_decimal' => 0,
'backorders' => 0,
'notify_stock_qty' => 0,
'is_in_stock' => 0
);
$product->setData('stock_data',$stock_data);
$product->setTaxClassId(2);     // default tax class
$product->setAttributeSetId(9); //9 is for default

$product->setWebsiteIds(array(1));
$product->setCategoryIds(array(9));
$product->setStatus(1);//1=Enabled; 2=Disabled;
$product->setVisibility(4);//4 = catalog &amp; search.
$image_name = 'blabla.JPEG';
$url = 'http://www.rasch-tapeten.de/shop/media/catalog/product/cache/1/image/bf8bb7ab75fe41b467eed88aa79f7917/1/3/133806.JPEG';
$img = 'media/'.$image_name;
echo $img."\n";
function save_image($inPath,$outPath){
    //Download images from remote server
    $in=    fopen($inPath, "rb");
    $out=   fopen($outPath, "wb");
    while ($chunk = fread($in,8192))
    {
    fwrite($out, $chunk, 8192);
    }
    fclose($in);
    fclose($out);
}
save_image($url, $img);

try
{
    $product->addImageToMediaGallery($img, array('image', 'small_image', 'thumbnail'), false, false);
}catch(Mage_Core_Exception $e)
{
echo $e->getMessage();
}
try
{
    $product->save();
}catch(Exception $e)
{
    echo $e->getMessage();
}
?>

好吧,如果我执行这个,它会抛出以下异常:

SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`magento`.`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`) ON DE)

有谁知道我做错了什么?

3 回答

  • 2
    $product->setAttributeSetId(9); //9 is for default
    

    你确定默认值是9吗?

    在我们导入产品的方法中,这就是我们所做的:

    protected $defaultAttributeSetId;   // Default attribute set
    
    private function getDefaultAttributeSetId()
    {
        if (!isset($this->defaultAttributeSetId))
        {
            $categoryModel = Mage::getModel("catalog/category");            
            $this->defaultAttributeSetId = $categoryModel->getDefaultAttributeSetId();
        }
        return $this->defaultAttributeSetId;
    }
    ...
    ->setAttributeSetId($this->getDefaultAttributeSetId())
    ...
    
  • 2

    使用此脚本,您可以使用php脚本创建产品

    $product = Mage::getModel('catalog/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);
    
  • 0

    此图片代码:

    $imagesdata = // import all images in array;<br>
    $getnumimages = sizeof($imagesdata);<br>
    for($i=0;$i<$getnumimages;$i++)<br>
    $product->addImageToMediaGallery ($imagesdata[$i], array('image','small_image','thumbnail'), false, false);
    

相关问题