Magento: Bulk Update Product Default Image to First Gallery Image
As an experienced developer, I understand the challenges that arise when dealing with large-scale product imports in Magento. One common issue is ensuring that the default product image (base, small, and thumbnail) is correctly set from the gallery images after an import.
Identifying the Problem
In your scenario, the addToImageGallery function sometimes fails to work as expected during the import process. This leads to products having “No Image” as the base image and other gallery images not selected as defaults. To resolve this issue, we need to create a script that iterates through all products and sets the first image from the media gallery as the default.
Magento Script for Bulk Update
Here’s an example of how you can achieve this using a custom Magento module. I’ll provide step-by-step instructions along with the necessary code snippets.
- Create a new module: In your app/code directory, create a new folder structure like
Company/UpdateDefaultImage. Inside theetcfolder, add amodule.xmlfile:
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Company_UpdateDefaultImage" setup_version="1.0.0">
<sequence>
<module name="Magento_Catalog"/>
</sequence>
</module>
</config>
- Create the update script: Inside the
app/code/Company/UpdateDefaultImage/Controller/Adminhtml/Index/Index.phpfile, add the following code:
<?php
namespace Company\UpdateDefaultImage\Controller\Adminhtml\Index;
use Magento\Framework\App\Action\Action;
use Magento\Framework\App\Action\Context;
use Magento\Framework\View\Result\PageFactory;
class Index extends Action
{
protected $pageFactory;
public function __construct(Context $context, PageFactory $pageFactory)
{
$this->pageFactory = $pageFactory;
parent::__construct($context);
}
public function execute()
{
$resultPage = $this->pageFactory->create();
$post = $this->getRequest()->getPostValue();
if ($post && isset($post['massaction'])) {
foreach (array_keys($post['massaction']) as $productId) {
$product = $this->_objectManager->create('Magento\Catalog\Model\Product')->load($productId);
// Get the first gallery image
$galleryImages = $product->getMediaGalleryEntries();
if (!empty($galleryImages)) {
$firstImage = reset($galleryImages);
$imageId = $firstImage['file'];
// Set the first image as default
$product->setImage($imageId);
$product->setSmallImage($imageId);
$product->setThumbnail($imageId);
try {
$product->save();
} catch (\Exception $e) {
// Handle exceptions if needed
}
}
}
// Add a success message here or redirect to a confirmation page
}
return $resultPage;
}
}
- Create the admin grid action: In your
app/code/Company/UpdateDefaultImage/etc/adminhtml/menu.xmlfile, add the following code to create a menu item for this functionality in the Magento backend:
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Adminhtml:etc/menu.xsd">
<menu>
<add id="Company_UpdateDefaultImage::index"
title="Update Default Image"
module="Company_UpdateDefaultImage"
sortOrder="10"
parent="Magento_Catalog::catalog_products"
action="adminhtml/index/index"/>
</menu>
</config>
Conclusion
This custom Magento module provides a solution to bulk update the default product images (base, small, and thumbnail) for all products in your catalog. It iterates through each product, retrieves the first image from the media gallery, and sets it as the default image.
Remember to clear your cache after installing this module and run the script from the Magento admin menu under “Catalog” -> “Update Default Image”.
For further guidance on customizing Magento’s core functionality, I recommend checking out our comprehensive tutorials at Laravel Company. They provide step-by-step walkthroughs for various Magento development topics.