Magento - 产品重新导入

发布于 2024-10-22 18:14:40 字数 1828 浏览 2 评论 0原文

我有一个从第三方文件中提取数据的脚本。我的导入只是解析并插入行,工作正常。

问题来自图像。

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

第一次导入时,一切都很好,图像进入并且我在前端看到它们没有问题。每次我重新导入这些产品时,问题都会出现,它似乎并没有删除所有图像,因为当产品重新导入时,我看到,例如 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

鹊巢 2024-10-29 18:14:40

好的,我解决了我的问题

在 foreach 中,我将调用移至 getSingleton 并添加了以下内容: $product = Mage::getModel('目录/产品');

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

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

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

谢谢大家

Ok so I figured out my problem

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

Thanks all

滥情空心 2024-10-29 18:14:40

您需要检查一些文件来分解 addImageToMediaGallery 并确定它到底在做什么。

  1. app/code/core/Mage/Catalog/Model/Product.php - 包含您使用的方法,将其分解更多您会发现...
  2. app/code/core/Mage /Catalog/Model/Product/Attribute/Backend/Media.php - 包含更多 addImageToMediaGallery 的“点滴”,例如 addImage 等。

一些可能性尝试的方法是

A) 确定文件是否已存在并附加到产品中,并在第二轮导入时忽略它们。也许正在寻找不同的文件标记。 Product.php 中的getMediaGalleryImages

B) 在再次导入之前清除与产品关联的媒体文件? Media.php 中的 clearMediaAttributeremoveImage

我还会尝试在 addImageToMediaGallery 调用中将 $move 选项设置为 true。

/**
 * Add image to media gallery
 *
 * @param string        $file              file path of image in file system
 * @param string|array  $mediaAttribute    code of attribute with type 'media_image',
 *                                         leave blank if image should be only in gallery
 * @param boolean       $move              if true, it will move source file
 * @param boolean       $exclude           mark image as disabled in product page view
 */
public function addImageToMediaGallery($file, $mediaAttribute=null, $move=false, $exclude=true)
{

另一种选择是尝试为第二个参数指定 null,如果您注意到 PHPDoc 注释,则将其留空将仅适用于听起来像您想要的图库...

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

让我知道这些是否有帮助。

A few files you'll want to examine to break down addImageToMediaGallery and determine what exactly its doing.

  1. app/code/core/Mage/Catalog/Model/Product.php - Contains the method your using, breaking it down more you'll find...
  2. app/code/core/Mage/Catalog/Model/Product/Attribute/Backend/Media.php - Contains more of the "bits and pieces" of addImageToMediaGallery like addImage 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 and removeImage within Media.php.

I would also try the $move option set to true within your addImageToMediaGallery call as well.

/**
 * Add image to media gallery
 *
 * @param string        $file              file path of image in file system
 * @param string|array  $mediaAttribute    code of attribute with type 'media_image',
 *                                         leave blank if image should be only in gallery
 * @param boolean       $move              if true, it will move source file
 * @param boolean       $exclude           mark image as disabled in product page view
 */
public function addImageToMediaGallery($file, $mediaAttribute=null, $move=false, $exclude=true)
{

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...

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

Let me know if any of these help.

丑丑阿 2024-10-29 18:14:40

尝试这样的事情:

//Check if gallery attribute exists then remove all images if it exists
//Get products gallery attribute
$attributes = $product->getTypeInstance()->getSetAttributes();          
if (isset($attributes['media_gallery'])) {
  $gallery = $attributes['media_gallery'];
  //Get the images
  $galleryData = $product->getMediaGallery();                               
  foreach($galleryData['images'] as $image){
    //If image exists
    if ($gallery->getBackend()->getImage($product, $image['file'])) {
      $gallery->getBackend()->removeImage($product, $image['file']);
    }
  }             
}

这篇博客文章也可能有帮助,因为我也从这里获得了代码片段表单:

http://www.sharpdotinc.com/mdost/2010/03/02/magento-import-multiple-images -或删除图像-durring-batch-import/

Give something like this a try:

//Check if gallery attribute exists then remove all images if it exists
//Get products gallery attribute
$attributes = $product->getTypeInstance()->getSetAttributes();          
if (isset($attributes['media_gallery'])) {
  $gallery = $attributes['media_gallery'];
  //Get the images
  $galleryData = $product->getMediaGallery();                               
  foreach($galleryData['images'] as $image){
    //If image exists
    if ($gallery->getBackend()->getImage($product, $image['file'])) {
      $gallery->getBackend()->removeImage($product, $image['file']);
    }
  }             
}

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/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文