#StackBounty: #magento2 #magento2.3 #importexport #media-images #gallery-image Magento2 – fastest way to save product after several cal…

Bounty: 100

We had several issues when trying to build a catalog for a customer using native Import/Export functionality. Unfortunately it seems Magento2 has some difficult to download & process cloudfront.net images, so we wrote our own command to manage that

Process is working fine, problem here is we need to call about 10 times addImageToMediaGallery() for each product. That calls are processed in a reasonable time, but then, at the end when calling the product save… it takes a long time

We have other processes to update product information (text attributes, stock data, etc…) in this project, and they all run fast, so it seems clear there is something about image processing which kills the save process performance

Our relevant code would be this

foreach ($parseData as $row) {

    // I omit the part where we get $sku from $row...
    // $productRepository -> MagentoCatalogModelProductRepository
    $product = $productRepository->get($sku);

    // Delete existing Magento images from DB and filesystem
    $existingMedia = $product->getMediaGalleryEntries();
    if (is_array($existingMedia) && count($existingMedia) > 0) {
        foreach ($existingMedia as $existingMediaItem) {
            // $galleryProcessor -> MagentoCatalogModelProductGalleryProcessor
            $galleryProcessor->removeImage($product, $existingMediaItem['file']);

    // Import new images

    foreach ($row as $i => $remoteImage) {
        // I omit the part where we download, save to filesystem & get $image
        // and the logic to set the right attributes in $media variable
        $product->addImageToMediaGallery($image, $media, false, false);

    // and finally... the kill of performance... 
    // all previous code runs reasonable fine, no need to change anything

We have tried different ways, $product->save() was our first try (same kill of performance)

So, any tips to save the product in a decent time? Is there any way to update image attributes values using something like MagentoCatalogModelResourceModelProductAction::updateAttributes, which works perfect & fast?

This is CE 2.3.0 version, by the way

Get this bounty!!!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.