Magento - 产品重新导入
我有一个从第三方文件中提取数据的脚本。我的导入只是解析并插入行,工作正常。
问题来自图像。
当导入脚本运行时,它首先删除所有当前项目,然后开始导入,将所有产品和图像插入图库中。
第一次导入时,一切都很好,图像进入并且我在前端看到它们没有问题。每次我重新导入这些产品时,问题都会出现,它似乎并没有删除所有图像,因为当产品重新导入时,我看到,例如 4 个图像是正确的,然后加载空白行,例如图像应该在那里,但找不到。
我不想看到这些空行,但我不确定它们为什么在那里。
难道是因为该产品的图片已经在目录中了?
我真的不确定这是什么以及为什么这样做。
谢谢
编辑:
我的代码是:
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 并将所有图像添加到每个产品吗?
谢谢
I have a script that pulls in data from a 3rd party file. My import simply parses and inserts rows, which is working fine.
The problem comes with images.
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.
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.
Thanks
EDIT:
My code 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>";
}
}
Is this because the addImageToMediaGallery
is called on each iteration and adding all images to each product?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
好的,我解决了我的问题
在 foreach 中,我将调用移至 getSingleton 并添加了以下内容:
$product = Mage::getModel('目录/产品');
然后,我在每次迭代后取消设置以下内容:
这似乎修复了我的脚本,现在将每个产品图像导入到正确的产品中,而不是导入其他产品并添加随机空白条目
谢谢大家
Ok so I figured out my problem
Inside the
foreach
I moved the call to thegetSingleton
and I added the the following:$product = Mage::getModel('catalog/product');
I then, after each iteration, unset the following:
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
Thanks all
您需要检查一些文件来分解
addImageToMediaGallery
并确定它到底在做什么。app/code/core/Mage/Catalog/Model/Product.php
- 包含您使用的方法,将其分解更多您会发现...app/code/core/Mage /Catalog/Model/Product/Attribute/Backend/Media.php
- 包含更多addImageToMediaGallery
的“点滴”,例如addImage
等。一些可能性尝试的方法是
A) 确定文件是否已存在并附加到产品中,并在第二轮导入时忽略它们。也许正在寻找不同的文件标记。 Product.php 中的
getMediaGalleryImages
。B) 在再次导入之前清除与产品关联的媒体文件? Media.php 中的
clearMediaAttribute
和removeImage
。我还会尝试在
addImageToMediaGallery
调用中将 $move 选项设置为 true。另一种选择是尝试为第二个参数指定 null,如果您注意到 PHPDoc 注释,则将其留空将仅适用于听起来像您想要的图库...
让我知道这些是否有帮助。
A few files you'll want to examine to break down
addImageToMediaGallery
and determine what exactly its doing.app/code/core/Mage/Catalog/Model/Product.php
- Contains the method your using, breaking it down more you'll find...app/code/core/Mage/Catalog/Model/Product/Attribute/Backend/Media.php
- Contains more of the "bits and pieces" ofaddImageToMediaGallery
likeaddImage
etc.Some possibilities to try would be either
A) determine if the files already exist and are attached to the product and ignoring them upon a second round of import. Perhaps looking for different file stamp.
getMediaGalleryImages
within Product.php.B) clear the media files associated with the products BEFORE importing again?
clearMediaAttribute
andremoveImage
within Media.php.I would also try the $move option set to true within your
addImageToMediaGallery
call as well.Another option would be to try to specify null for the second param, if you note the PHPDoc comments, leaving it blank will only be for the gallery which sounds like what you are wanting...
Let me know if any of these help.
尝试这样的事情:
这篇博客文章也可能有帮助,因为我也从这里获得了代码片段表单:
http://www.sharpdotinc.com/mdost/2010/03/02/magento-import-multiple-images -或删除图像-durring-batch-import/
Give something like this a try:
This blog posting may also help, as its where I got the code snippet form also:
http://www.sharpdotinc.com/mdost/2010/03/02/magento-import-multiple-images-or-remove-images-durring-batch-import/