Magento - re-importing of products

天大地大妈咪最大 提交于 2019-12-12 05:58:58

问题


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


回答1:


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




回答2:


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.




回答3:


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/



来源:https://stackoverflow.com/questions/5328763/magento-re-importing-of-products

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!