0

I have around ~10k of products to create programmatically. I also have around 20 custom attributes to each product. Saving the stock, the price, special price, those 20 custom attributes it takes around < 50 min. Once I added the image part, from 50 mins its jumps to 8 hours. The images are big indeed, but 8 hours its a lot of time.

This is my script of saving the images :

//$productData contains the attributes, prices, etc 
$product = $this->productInterfaceFactory->create(['data' => $productData]);
$product = $this->productRepository->save($product);
$images = $this->getImages(); // its an array contains the image path
if (count($images) > 0) {
    for ($i = 0; $i < count($images); $i++) {
        if ($i === 0) {
            $product->addImageToMediaGallery($images[$i], ['image', 'small_image', 'thumbnail'], false, false);
        } else {
            $product->addImageToMediaGallery($images[$i], ['image'], false, false);
        }
        $product->save();
    }
}

Is there a way to reduce the execution time? I was thinking to keep saving the products and the attributes in one cronjob and the images to put into another cronjob, but I am opened to other suggestions as well. Please advice.

Thank you

2
  • Why $product->save();?
    – Sohel Rana
    Commented Apr 25 at 2:15
  • @SohelRana with using the resourceModel, it does NOT work. only with $product->save(). IF you have other suggestion to try, please let me know. Thanks Commented Apr 25 at 7:35

0

Your Answer

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