Showing posts with label save. Show all posts
Showing posts with label save. Show all posts

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