0

I have imported the products and product images for my new website. There are more than 5000 products have added. When assigning product image , backside of the product image was added to main image like below. Manually doing is obviously a nightmare.

enter image description here

How can i easily swap this for all products? Any idea or scripts highly appreciated.

Thanks in advance.

2 Answers 2

1

You may be able to do it with database queries. the image attribute value is stored in the catalog_product_entity_varchar table. you can get the image attribute ID with select from eav_attribute_where attribute_code = 'image' and entity_type_id = (select wntity_type_id from eav_entity_type where entity_type_code = 'catalog_product').

With this id you can identify the image attributes values with select * from catalog_product_entity_varchar where attribute_id = {id from above}.

If you have a special name format for the images that should be set in the image attribute you can try updating this table.

1
  • thank you, i will try this.
    – Mujahidh
    Commented Nov 17, 2022 at 13:57
0

I come up with running below script on pub directory to achieve this task. This may help some one in future.

image_swap.php file

<?php
use \Magento\Framework\App\Bootstrap;
require __DIR__ . '/../app/bootstrap.php';
$params = $_SERVER;
$bootstrap = Bootstrap::create(BP, $params);

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$state = $objectManager->get('Magento\Framework\App\State');
$state->setAreaCode('frontend');

$productRepository = $objectManager->get('Magento\Catalog\Model\ProductRepository');
$csv = "sku_vs_image.csv";
if (!empty($argv) && sizeof($argv) > 1) { 
  $csv = $argv[1];
}
if (($handle = fopen($csv, "r")) !== FALSE) {
while (($data = fgetcsv($handle, 2000, "\t")) !== FALSE) {
    $num = count($data);
    if ($num < 1) {
        continue;
    }
    $sku = explode(',',trim($data[0])); 
    
    $imagePath = $sku[1];

    try {
        $product = $productRepository->get($sku[0]);
        
        $productId = $product->getId();
        $productRepository = $objectManager->create('Magento\Catalog\Api\ProductRepositoryInterface');
        
        $product->addImageToMediaGallery('catalog/product'.$imagePath, array('image', 'small_image', 'thumbnail'), false, false);
        $product->save();

        //Get existing image 
        $existingMediaGalleryEntries = $product->getMediaGalleryEntries();
        
        foreach ($existingMediaGalleryEntries as $key => $entry) {
            //Remove the duplicate image after assigning roles
            if($entry->getFile() === $imagePath ){
                unset($existingMediaGalleryEntries[$key]);
            }
        }
        $product->setMediaGalleryEntries($existingMediaGalleryEntries);
        $productRepository->save($product);
    } catch (\Exception $e) {
        echo $sku[0].$e->getMessage()."</br>";
        continue;
    }
}
fclose($handle);
}

sku_vs_image.csv file sample

sku_1,/1/6/16-02-08651_1.jpeg
sku_2,/1/6/16-02-05659_1.jpeg
sku_3,/1/6/16-02-08651_1_1.jpeg

Update the csv and run the script from root. script first update the image role and create the image, and remove the existing image which assigned the role after.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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