Question

During edit product from front-end I need to remove images of the product gallery which are checked by the user to remove it.

No correct solution

OTHER TIPS

We can use \Magento\Catalog\Model\ProductFactory to get media gallery. We also need to declare the \Magento\Catalog\Api\ProductRepositoryInterface class which helps us to save the existing product.

We can use Object Manager directly, however, this way isn't good. It's better that we should inject these class in the constructor.

/**@var \Magento\Catalog\Model\ProductFactory **/
protected $product;

/**@var \Magento\Catalog\Api\ProductRepositoryInterface **/
protected $productRepository;

public function __construct(
   ......
   \Magento\Catalog\Model\ProductFactory $product,
   \Magento\Catalog\Api\ProductRepositoryInterface $productRepository
   ......
)
{
  $this->product = $product;
  $this->productRepository = $productRepository;
}

In your custom method:

//Product id
$productId = 12;
$product = $this->product->create();

$existingMediaGalleryEntries = $product->load($productId)->getMediaGalleryEntries();

foreach ($existingMediaGalleryEntries as $key => $entry) {
    //We can add your condition here
    unset($existingMediaGalleryEntries[$key]);
}
$product->setMediaGalleryEntries($existingMediaGalleryEntries);
$this->productRepository->save($product);

Should take a look at the Unit test:

--vendor/magento/module-catalog/Test/Unit/Model/ProductTest.php
--vendor/magento/module-catalog/Test/Unit/Model/ProductRepositoryTest.php
--vendor/magento/module-catalog/Test/Unit/Model/Product/Gallery/GalleryManagementTest.php

Below code working fine for me to remove Image Magento 2.

Using Product ID:

$product = $objectManager->create('Magento\Catalog\Model\Product')->load($productNewId);
$imageProcessor = $objectManager->create('\Magento\Catalog\Model\Product\Gallery\Processor');
$images = $product->getMediaGalleryImages();
foreach($images as $child){
    $imageProcessor->removeImage($product, $child->getFile());
}   

Hope it helps to you

I was working on similar task, I am sharing my code, hope it would help others too.

Fist initialization in constructor

protected $productModel;
protected $imageProcessor;

public function __construct(
    ...
    \Magento\Catalog\Model\Product $productModel,
    \Magento\Catalog\Model\Product\Gallery\Processor $imageProcessor,
    ...
)
{
    ...
    $this->productModel = $productModel;
    $this->imageProcessor = $imageProcessor;
    ...
}

Now you can use it in your function

$product = $this->productModel->load($productId);
$gallery = $product->getMediaGalleryImages();
foreach($gallery as $image){
    $this->imageProcessor->removeImage($product,$image->getFile());
}
$product->save();

This should work

For me it finally worked based on this article: https://www.mageplaza.com/devdocs/add-images-to-product-programmatically-magento-2.html

    $gallery = $_product->getMediaGalleryImages();
    if (count($gallery) > 0) {
        foreach($gallery as $image){
            $this->productGallery->deleteGallery($image->getValueId());
        }
        $_product->setMediaGalleryEntries([]);
        $_product->save();
    }

Where productGallery is an instance of \Magento\Catalog\Model\ResourceModel\Product\Gallery

I hope it helps someone

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top