Friday 23 May 2014

import product image from external url in magento 1.8

Hi in some project i need to import the csv of products in that i have he values of product images is like

http://www.example.com/image.jpg

i need to import that by default there is no mechanism in magento to import external image.

for doinmg this we have to edit the import function in below file

app/code/core/Mage/Catalog/Model/Convert/Adapter/Product.php

around line 754 you will see some code like this

  foreach ($product->getMediaAttributes() as $mediaAttributeCode => $mediaAttribute) {
             if (isset($importData[$mediaAttributeCode])) {
                 $file = trim($importData[$mediaAttributeCode]);
                 if (!empty($file) && !$mediaGalleryBackendModel->getImage($product, $file)) {
                    $arrayToMassAdd[] = array('file' => trim($file), 'mediaAttribute' => $mediaAttributeCode);
                 }
            }
         }


you simply need to replace this dunction with the below one 

foreach ($product->getMediaAttributes() as $mediaAttributeCode => $mediaAttribute) {
                        if (isset($importData[$mediaAttributeCode])) {
                            $file = trim($importData[$mediaAttributeCode]);
                            if (!empty($file) && !$mediaGalleryBackendModel->getImage($product, $file)) {
                                // Start Of Code To Import Images From Urls
                                if (preg_match('%http?://[a-z0-9\-./]+\.(?:jpe?g|png|gif)%i', $file)) {
                                    $path_parts = pathinfo($file);
                                    $html_filename = DS . $path_parts['basename'];
                                    $fullpath = Mage::getBaseDir('media') . DS . 'import' . $html_filename;
                                    if(!file_exists($fullpath)) {
                                        file_put_contents($fullpath, file_get_contents($file));}
                                    $arrayToMassAdd[] = array('file' => trim($html_filename), 'mediaAttribute' => $mediaAttributeCode);
                                }
                                else
                                {
                                    $arrayToMassAdd[] = array('file' => trim($file), 'mediaAttribute' => $mediaAttributeCode);
                                }
                                // End Of Code To Import Images From URLs
                            }
                        }
                        }

Hope this will help you. thanks





7 comments:

  1. You have not specified that after doing this change which Import feature i have to use, is it System >> Dataflow Profiles >> Import/Export >> Import All Products or the System >> Import/Export ?

    ReplyDelete
    Replies
    1. Dataflow Profiles >> Import/Export

      worked for me

      Delete
    2. but i will suggest you to use magmi now , it imports the image from the live url and its very fast as compare to the magento default data profile.

      Delete
  2. Here is the article how to import images from external URLs without codes (for non-technical people like me) -
    https://www.mag-manager.com/useful-articles/how-to/how-to-import-images-from-external-urls-to-magento/

    ReplyDelete
  3. HI Maria but in this we need to use the store manager to do this.

    thanks

    ReplyDelete
  4. not working in 1930. I have tried

    ReplyDelete