Skip to content

Magento2 Image Roles does not work sometimes

I am using below codes for the image save. It is working usually, but it doesn’t work sometimes, it doesn’t select image roles. How can I find the errors? Because Magento2 doesn’t give an error.

$product = $this->repository->getById($productId, true, 0, true);

$types = ['images'];
if($image['is_default']) {
   $types = ['small_image', 'thumbnail', 'image'];
}
try {
   $product->setStoreId(0);
   $product->addImageToMediaGallery($media_folder . $internal_url, $types, true, false);
   $product->save();
} catch (Throwable $exception) {
   return false;
}

I see the image in product detail. But I cannot see thumbnail image, because it doesn’t select.