从php创建magento(麦进斗)产品

 

问题描述:

我朋友最近在帮一个供货商创建一个magento(麦进斗)网上商城,它大概的货品数量有三千个,他叫供货商将这些商品的信息提供给他,但是供货商并能(或者是不想)将这些信息提供给他,叫他自己在网上面找。所以,我写了个爬虫在网上面给他爬了这些信息,但是如何将这些信息录入进去就很麻烦了。有人告诉我不用mysql去录入,只用php脚本和magento(麦进斗)自带的api也可以。

我尝试着用php脚本(下图贴出来的)和magento(麦进斗)的api去解决,但是还是有问题,我该怎么解决?

<?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'), falsefalse);

}catch(Mage_Core_Exception $e)

{

echo $e->getMessage();

}

try

{

    $product->save();

}catch(Exception $e)

{

    echo $e->getMessage();

}

?>

Well if i execute this one it throws the following exception:

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)

 

 

解决方案:

$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())

...

 

更多信息请关注麦进斗(www.maijindou.com)

转载于:https://my.oschina.net/u/3350137/blog/866454

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值