3

How can I add one image to ALL the products in Magento 2 ? I found an answer for Magento 1.9, but I need the code for Magento2.

Not a placeholder, I already have several images on each product and I need to add one image for each product.

  • Simply add placeholder image and you will get this image for all product. This is right way @KishanSavaliya – Chirag Parmar Nov 7 '19 at 11:14
3

You just need to create one root file and paste below code in that file and run file in browser

<?php
ini_set('memory_limit', '512M');
set_time_limit(0);
require __DIR__ . '/app/bootstrap.php';
$bootstrap     = \Magento\Framework\App\Bootstrap::create(BP, $_SERVER);
$objectManager = $bootstrap->getObjectManager();
$appState = $objectManager->get('\Magento\Framework\App\State');
$appState->setAreaCode('frontend');
$dir = $objectManager->get('\Magento\Framework\Filesystem\DirectoryList');

$productCollectionFactory = $objectManager->get('\Magento\Catalog\Model\ResourceModel\Product\CollectionFactory');
$collection = $productCollectionFactory->create();
$imagePath = $dir->getPath('media')."/yourimage.jpg"; // Your image should be on pub/media path
$imageType = ['image', 'small_image', 'thumbnail'];

if($collection->getSize()) {
    foreach ($collection as $product) {
        if($product->getId()) {
            if(file_exists($imagePath)) {
                $product = $objectManager->create('Magento\Catalog\Model\Product')->load($product->getId());
                $product->addImageToMediaGallery($imagePath, $imageType, false, false);
                $product->save();
            }
        }
    }
} 

Hope this will help you!

|improve this answer|||||
  • Thank you, I have used your code and create a new module – Andreea Onica Nov 7 '19 at 14:01
2

This will help you.

To upload placeholder images in Magento 2

  • Step 1: Login to your Magento 2 backend
  • Step 2: Upload placeholder images
  • Step 3: Save the configuration

Step 1: Login to your Magento 2 backend

On the Admin sidebar,

Stores > Settings > Configuration

In the panel, under Catalog, choose Catalog.

Step 2: Upload placeholder images

Expand the Product Image Placeholders section. Click on Choose File to upload the placeholder image. You can use the same or the different images for each role.

enter image description here

Step 3: Save the configuration

Clear all catch and test it.

I hope this will help you.

|improve this answer|||||
1

You can go to this path in backend and add you Placeholder image in Magento 2. This placeholder image will appear on all products.

Store > Configuration > Catalog > Catalog > Product Image Placeholders

enter image description here

Upload image here and clear your cache.

Hope this will help you!

|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.