I have some products created, but they do not have images when i see them in frontend or backend. I have these missing images in one folder with the format of productSku.png. I want to build image updater service which run through all created products and add missing images to product
2 Answers
Quick solution - use the Magento Product Images import functionality:
- Write a simple php script which collects all your SKUs from the filenames from your images folder and creates a CSV file with the product SKU on first column, then the base_image, small_image, thumbnail_image, or additional_images on the other columns filled with the image filename.
- Move all your images in the <project_root>/var/import/images/ folder.
- Use the Magento product images importer guideline to import the CSV file previously created.
- 5,137
- 1
- 11
- 26
I have done this before with an external bootstrap script. It would also be possible with a custom module cli command.
You need to give consideration to the type of product (simple, configurable, grouped etc), and the image roles you require i.e swatch images. If you have a multi store setup you also need to check if your image role attributes are set to store view or global - i.e. do you want the same images for all store views.
You will need to be able to correlate your images in your folder with your product stock-keeping unit (SKU). This is best achieved by consistently renaming the images. For example if the product sku is ABC-01 and your image is product-image-abc-01.jpg then you will need to determine the image filename from the sku.
To find your missing images load a product collection (all products) and parse each products media gallery data
$_existingMediaGalleryEntries = $_product->getMediaGalleryEntries();
$_imageRoles=array();
foreach ($_existingMediaGalleryEntries as $entry) {
$_imageRoles[]=$entry->getTypes();
}
if $_imageRoles[] is empty, the product (probably) has no image.
Set your store view
$_store = $this->_storeManagerInterface->getStore($_storeId);
$this->_storeManagerInterface->setCurrentStore($_store);
then loop through the products with missing images, loading them using the product repository
$_product = $this->_productRepository->get($_sku);
Clear and then set the image data depending on the product type and the roles you require. The folder with your images in $_imageImportPath must be relative to the Magento pub/media/import folder.
$_product->setMediaGallery (array('images'=>array (), 'values'=>array ()));
if ($_productType === 'simple')
{
$_product->addImageToMediaGallery($_imageImportPath, array('swatch_image','image', 'small_image', 'thumbnail'), false, false);
} else {
$_product->addImageToMediaGallery($_imageImportPath, array('image', 'small_image', 'thumbnail'), false, false);
}
When the image data is set you can save the product.
try {
$this->_productRepository->save($_product);
}
catch(\Exceptionagento\Framework\Exception\NoSuchEntityException $e) {
// catch NoSuchEntityException
}
- 5,785
- 5
- 21
- 43