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:
Let’s find out How 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; } ?>
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!
Hello Magento Friends, In today’s blog, I will explain How to Add Tooltip in Checkout…
MongoDB is a popular NoSQL database that offers flexibility and scalability when handling modern web…
In NodeJS, callbacks empower developers to execute asynchronous operations like reading files, handling requests, and…
Hello Magento Friends, In today’s blog, we will learn How to Show SKU in Order…
The "Buy Now" and "Add to Cart" buttons serve as the primary call-to-action (CTA) elements…
Hello Magento Friends, In Magento 2, the checkout process allows customers to choose multiple shipping…
View Comments
how we can import the addtinal images in mageto 2 through script
and how we can get the addtional images path in maegtno 2 through script
no