Set image as base, small and thumbnail
-
16-10-2019 - |
Question
So I am constructing an import array with details and using AvS_FastSimpleImport to do the importing work. Here is the relevant code:
$fileName = Mage::getBaseDir()."/".Mage::getStoreConfig(C4B_XmlImport_Model_Catalog_Importer::XML_PATH_IMAGE_IMPORT_DIRECTORY).$imageName;
if (file_exists($fileName)) {
$addImage = $defaultImportEntry;
$addImage['sku'] = '';
$addImage['_media_image'] = $imageName;
$addImage['_media_attribute_id'] = Mage::getSingleton('catalog/product')->getResource()->getAttribute('media_gallery')->getAttributeId();
$addImage['_media_is_disabled'] = 0;
$addImage['_media_position'] = $imgCounter;
$addImage['_media_lable'] = $imageName;
if(!$isAssociatedProduct && $currentNumber == 1) {
$addImage['image'] = $fileName;
$addImage['small_image'] = $fileName;
$addImage['thumbnail'] = $fileName;
}
}
The second if
statement is entered only for specific images (as you can see). On these images I want the radio buttons of Base Image, Small Image, and Thumbnail to be set, but apparently it doesn't work.
Here's a tutorial document I was reading: http://www.integer-net.de/download/ImportExport_EN.pdf
Solution
The value in image
, small_image
and thumbnail
should be the same as in _media_image
. You should use $imageName
instead of $fileName
there.
OTHER TIPS
you have to do only one think
Just Set the Store Id when you save your product.
foreach($image_array as $single_image) {
$img_counter++;
if ($img_counter == 1) {
$img_save_mode = array('image','small_image','thumbnail');
} else {
$img_save_mode = array();
}
$upper_path = Mage::getBaseDir('media') . DS . 'import' . DS ;
$filepath = $upper_path . $single_image;
if ( file_exists($filepath) ) {
try {
$product->addImageToMediaGallery($filepath, $img_save_mode , false , false);
$product->setStoreId(0);
} catch (Exception $e) {
echo $e->getMessage();
}
} else {
echo "Product does not have an image or the path is incorrect. Path was: {$filepath}<br/>";
}
$product->save();
}
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange