Magento-重新导入产品 [英] Magento - re-importing of products

查看:100
本文介绍了Magento-重新导入产品的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个脚本,可以从第3方文件中提取数据.我的导入只是解析并插入行,效果很好.

I have a script that pulls in data from a 3rd party file. My import simply parses and inserts rows, which is working fine.

问题来自图像.

运行导入脚本时,它将首先删除所有当前项目,然后开始导入,将所有产品和图像插入图库.

When the import script runs, it first deletes all the current items and then the import begins, inserting all products and images into the gallery.

在第一次导入时,一切都很好,图像进入了,我在前端看到它们没问题.问题出在每次我重新导入这些产品时,它似乎并没有删除所有图像,因为当产品重新导入时,我看到例如4张图像正确,然后加载了空白行,例如图像应该在那里,但找不到.

On the first import, everything is fine, the images go in and I see them on the frontend no problem. The problem comes with everytime I then re-import these products, it doesn't seem to delete all images, as when the products re-import, I see, for example the 4 images correct, then then loads of blank rows, like images should be there, but can't be found.

我不想看到这些空行,但是我不确定为什么会出现这些空行.

I don't want to see these blank lines, but I'm not sure why they are there.

是因为产品的图像已经在目录中了吗?

Could it be because the images for the product are already in the catalogue?

我真的不确定这是什么,为什么这样做.

I am really unsure what and why this is doing what it is.

谢谢

我的代码是:

require_once('app/Mage.php');
$app = Mage::app('default');
$product = Mage::getSingleton('catalog/product');

$txt_file    = file_get_contents('test.txt');
$rows        = explode("\n", $txt_file);
array_shift($rows);

foreach($rows as $row => $data)
{
//get row data
$row_data = explode('^', $data);

$info[$row]['uniqueid']         = $row_data[0];
$info[$row]['client']           = $row_data[1];
$info[$row]['make']             = $row_data[2];
$info[$row]['model']            = $row_data[3];
$info[$row]['adtext']           = $row_data[4];

//display images
$row_images = explode(',', $info[$row]['picturereference']);

foreach($row_images as $row_image)
{
    $product->addImageToMediaGallery(Mage::getBaseDir('media') . DS . 'import/' . $row_image, array('image', 'small_image','thumbnail'), false, false);
}

$product->setStoreId(Mage::app()->getStore(true)->getWebsite()->getId());
$product->setWebsiteIds(array(Mage::app()->getStore(true)->getWebsite()->getId()));
$product->setId($info[$row]['id']); 
$product->setSku(strtolower($info[$row]['make']).'-'.strtolower($info[$row]['model'])); 
$product->setName($info[$row]['make']); 
$product->setDescription($info[$row]['adtext']);

    try {
    $product->save();
  echo "Saved";
    }
    catch (Exception $ex) {
      echo "<pre>".$ex."</pre>";
   }

}

这是因为每次迭代都会调用addImageToMediaGallery并将所有图像添加到每个产品中吗?

Is this because the addImageToMediaGallery is called on each iteration and adding all images to each product?

谢谢

推荐答案

好的,所以我找出了问题所在

Ok so I figured out my problem

foreach内,我将呼叫移至getSingleton,并添加了以下内容:$product = Mage::getModel('catalog/product');

Inside the foreach I moved the call to the getSingleton and I added the the following: $product = Mage::getModel('catalog/product');

然后,在每次迭代之后,我取消设置以下内容:

I then, after each iteration, unset the following:

    unset($product);
    unset($info);
    unset($stockData);
    unset($row_images);

这似乎修复了我的脚本,现在将每个产品图像导入到适当的产品中,而不是导入其他产品并添加随机的空白条目

This seemed to fix my script and now imports each products images into the proper product rather than importing other and adding random blank entries

谢谢

这篇关于Magento-重新导入产品的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆