Magento Tutorials

How to Programmatically Add Images to Product Gallery in Magento 2

Hello Magento Friends,

In this Magento Tutorial, I will illustrate How to Programmatically Add Images to Product Gallery in Magento 2.

Images play an important part in attracting customers to the store. E-commerce stores selling products like apparel need more than one product image so that the customer gets a clear idea about the product from every angle.

With Magento 2, the store merchants have to add all the images manually for each product. This occupies a lot of your time and is a tedious task. Instead, you can add images to the product gallery in bulk via a CSV file in Magento 2.

Adding images to the product gallery in Magento 2 has the following benefits:

  • Get more SERPs rankings
  • Better shopping experience
  • Makes admin task easy
  • Get more views
  • Expand social media engagement
  • Improved your business SEO value

Let’s find out How to Programmatically Add Images to Product Gallery in Magento 2.

Steps to Programmatically Add Images to Product Gallery in Magento 2:

Step 1: Create a PHP file in the Magento Root folder and add the below code.

<?php

use Magento\Framework\App\Bootstrap;

require __DIR__ . '/app/bootstrap.php';

$bootstrap = Bootstrap::create(BP, $_SERVER);
$obj = $bootstrap->getObjectManager();

$state = $obj->get('Magento\Framework\App\State');
$state->setAreaCode('frontend');

try
{
    $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
    $importDir = __DIR__ . '/pub/media/catalog/product'; //Directory path of the images

    $product_id = '12'; //Product Id

    $product = $objectManager->get('Magento\Catalog\Model\Product')->load($product_id);

    $img_url = $importDir . $product->getImage();
    $product->addImageToMediaGallery($img_url, array('image', 'small_image', 'thumbnail'), true, false);
    $product->save();
    echo "Your Product id: $product_id is saved Succefully";

}
catch (\Exception $e)
{
    echo $e->getMessage();
    exit;
}
?>

Conclusion:

Accordingly, you can Programmatically Add Images to Product Gallery in Magento 2. If you face any difficulty with the above steps, feel free to mention them in the comment and I will be happy to help you out. Share the article and keep in touch with us!

Happy Reading!

Click to rate this post!
[Total: 4 Average: 3.3]
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

How to Integrate ChatGPT with Laravel Application?

In this guide, we'll explore how to integrate ChatGPT, an AI-powered chatbot, with a Laravel…

9 hours ago

What are Net Sales? How to Calculate Your Net Sales?

In the world of business, understanding financial metrics is crucial for making informed decisions and…

3 days ago

Magento 2 Extensions Digest April 2024 (New Release & Updates)

Welcome to the MageComp Monthly Digest, where we bring you the latest updates, releases, and…

3 days ago

The ABCs of Geofencing: Definition, Features and Uses

In this era, businesses are always on the lookout for ways to engage with their…

3 days ago

How to Delete Product Variant in a Shopify Remix App using GraphQL Mutations?

Managing a Shopify store efficiently involves keeping your product catalog organized. This includes removing outdated…

4 days ago

6 Innovative Tools Revolutionizing E-Commerce Operations

E-commerce has transformed the way consumers shop for products and services and interact with businesses.…

7 days ago