How To

Magento 2: Regenerate Catalog Product Cache Images Programmatically

Hello Magento Folks?,

Welcome to Magento How-To Blog. Today I will illustrate Magento 2 Regenerate Catalog Product Cache Images Programmatically. Lastly, we learned How To Get Formatted Price With Currency In Magento 2. Let’s Get In?.

Introduction:

Basically, Magento 2 generates different types of catalog images while creating some products from admin. e.g : small_image , thumbnail_image , base_image etc. It also generates some images with a cache folder too. But in some cases, many cache images are not generated e.g. while upgrading the Magento version, server transfer and especially use third party media storage. The reason is Magento 2 does not generate product page cache images while we are using third party media storage hence for that we need to run the resize command. But it is a very time-consuming process. And to overcome this problem here we have come up with an easy solution. You can regenerate catalog product cache images programmatically as per your need.

Steps to  Regenerate Catalog Product Cache Images Programmatically:

Step 1: Firstly, we require to just update the gallery.phtml file at the following path,

vendor\magento\module-catalog\view\frontend\templates\product\view and add below code at the First line of file.

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();

$resizeImage = $objectManager->get('\Magento\MediaStorage\Service\ImageResize');

$_product = $block->getProduct();

$galleryImages = $_product->getMediaGalleryImages();

if ($galleryImages) 

{

foreach ($galleryImages as $image) {

$resizeImage->resizeFromImageName($image->getFile());

}

}

Note: One can update the file path based on your theme.

Step 2: After that refresh the cache.

That’s It.

Over to you:

Hopefully, all have implemented successfully the above steps for Regenerate Catalog Product Cache Images Programmatically in Magento 2. But if you face any problems in this do write it in the comment section below. If you find the article helpful then share it with your Magento developer friends. Let’s meet in the next blog.

Happy Developing?

Click to rate this post!
[Total: 8 Average: 5]
Dhiren Vasoya

Dhiren Vasoya is a Director and Co-founder at MageComp, Passionate ?️ Certified Magento Developer?‍?. He has more than 9 years of experience in Magento Development and completed 850+ projects to solve the most important E-commerce challenges. He is fond❤️ of coding and if he is not busy developing then you can find him at the cricket ground, hitting boundaries.?

View Comments

Recent Posts

Improving Error Handling and Transition Management in Remix with useRouteError and useViewTransitionState

In modern web development, seamless navigation and state management are crucial for delivering a smooth…

6 days ago

Magento Open Source 2.4.8-Beta Release Notes

Magento Open Source 2.4.8 beta version released on October  8, 2024. The latest release of…

1 week ago

How to Create Catalog Price Rule in Magento 2 Programmatically?

Hello Magento Friends, Creating catalog price rules programmatically in Magento 2 can be a valuable…

1 week ago

Top 10 Tips to Hire Shopify Developers

As the world of eCommerce continues to thrive, Shopify has become one of the most…

2 weeks ago

Managing Browser Events and Navigation in Shopify Remix: useBeforeUnload, useHref, and useLocation Hooks

Shopify Remix is an innovative framework that provides a streamlined experience for building fast, dynamic,…

2 weeks ago

Ultimate Guide to Hiring a Top Shopify Development Agency

Building a successful eCommerce store requires expertise, and for many businesses, Shopify has become the…

2 weeks ago