2016-10-26 12 views

答えて

2
Use following code to add/remove image from product in Magento2. 
// Instance of object manager 
$objectManager = \Magento\Framework\App\ObjectManager::getInstance(); 
*Remove Images From Product*/ 
$productId = ; // Id of product 
$product = $objectManager->create('Magento\Catalog\Model\Product')->load($productId); 
$productRepository = $objectManager->create('Magento\Catalog\Api\ProductRepositoryInterface'); 
$existingMediaGalleryEntries = $product->getMediaGalleryEntries(); 
foreach ($existingMediaGalleryEntries as $key => $entry) { 
    unset($existingMediaGalleryEntries[$key]); 
} 
$product->setMediaGalleryEntries($existingMediaGalleryEntries); 
$productRepository->save($product); 
/*Add Images To The Product*/ 
$imagePath = "sample.png"; // path of the image 
$product->addImageToMediaGallery($imagePath, array('image', 'small_image', 'thumbnail'), false, false); 
$product->save(); 
+1

私は上記のコードに従って変更しようとしています。お返事ありがとうございます。 – Nitesh

+1

ありがとう。私の問題は解決されました。 – Nitesh

+0

私はこの解決策を試しています私のイメージは良い仕事を取り除き、画像の追加もうまくいきますが、基本的な小さなものとサムネイルとしては設定されていません – Newbie

関連する問題