Import produktów z zdjęciami
Często konieczne jest zaimportowanie listy produktów wraz z ich zdjęciami. Jeżeli posiadamy plik z listą naszych zdjęć ich zaimportowanie jest bardzo poste za pomocą System -> Import/Export -> Data Flow Profile
Jednak wcześniej konieczne będzie dokonanie pewnej modyfikacji pliku
/app/code/core/Mage/Catalog/Model/Convert/Adapter/Product.php
W powyższym pliku odnajdujemy linię :
foreach ($importData as $field => $value) {
znajduje się ona w okolicy wiersza 707.
Następnie poniżej wstawiamy :
if(($field=="Image1" || $field=="Image2" || $field=="Image3") && $value != "" )
{
/** EXTERNAL IMAGE IMPORT - START **/
$image_url = $value; //get external image url from csv
$image_type = substr(strrchr($image_url,"."),1); //find the image extension
$filename = md5($image_url . $importData['sku']).'.'.$image_type; //give a new name, you can modify as per your requirement
$filepath = Mage::getBaseDir('media') . DS . 'import'. DS . $filename; //path for temp storage folder: ./media/import/
file_put_contents($filepath, file_get_contents(trim($image_url))); //store the image from external url to the temp storage folder
if($field=="Image1")
{
/**
* BEGIN REMOVE EXISTING MEDIA GALLERY
*/
$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'] );
}
}
$product->save ();
}
/**
* END REMOVE EXISTING MEDIA GALLERY
*/
$mediaAttribute = array (
'thumbnail',
'small_image',
'image'
);
}
else $mediaAttribute = array ();
/**
* 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
*/
$product->addImageToMediaGallery($filepath, $mediaAttribute, false, false);
/** EXTERNAL IMAGE IMPORT - END **/
}
Od tej chwili nasz plik importu może posiadać kolumny Image1, Image2, Image3 z adresami url do obrazków.
Jeżeli plik będzie posiadał kolumnę Image1 stare obrazki produktu zostaną usunięte. Natomiast kolumny Image2 i Image3 jedynie dogrywają kolejne obrazy do produktu.
Modyfikacja była testowana w wersji Magento 1.6.1.0 i działa tam wyśmienicie, rozwiązując kłopotliwy problem dodawania obrazów do produktów w Magento.



