2

Try to Remove Products Images but got error in magento2

Error : "Product with SKU '12553' is not linked to product with SKU '12552'" in

My code is:

$get_ids = array(12551,12552,12553,12554);

foreach($get_ids as $ids){
    $product = $objectManager->get('Magento\Catalog\Model\Product')->load($ids);
    $productRepository = $objectManager->create('Magento\Catalog\Api\ProductRepositoryInterface');

    $existingMediaGalleryEntries = $product->getMediaGalleryEntries();
    foreach ($existingMediaGalleryEntries as $key => $entry) {
        unset($existingMediaGalleryEntries[$key]);
    }
    $product->setMediaGalleryEntries($existingMediaGalleryEntries);
    $productRepository->save($product);
}
1
  • Please confirm whether the products belongs to simple ? Jul 13, 2017 at 11:29

3 Answers 3

6
<?php
use Magento\Framework\App\Bootstrap;
require __DIR__ . '/app/bootstrap.php';
$bootstrap = Bootstrap::create(BP, $_SERVER);
$obj = $bootstrap->getObjectManager();
$state = $obj->get('Magento\Framework\App\State');
$state->setAreaCode('frontend');

$productArray = array("2011", "2012", "2013");
//Instance of object manager
$objectManager = \Magento\Framework\App\ObjectManager::getInstance(); 
foreach($productArray as $key=>$productId)
{    
$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);

echo "Product Image has been deleted for".$productId."</br>";

}
echo "Yahooooo";

?>
0

i want to remove through sku but i could not be delete i just want to first read the file then remove product images accordingly csv file skus anyone can help me for this solution

<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
ini_set('memory_limit', '-1');
ini_set('max_execution_time', 0); 
set_time_limit(0);
error_reporting(E_ALL);
use Magento\Framework\App\Bootstrap;
require __DIR__ . '/app/bootstrap.php';
$bootstrap = Bootstrap::create(BP, $_SERVER);
$obj = $bootstrap->getObjectManager();
$state = $obj->get('Magento\Framework\App\State');
$state->setAreaCode('frontend');

//Instance of object manager
$objectManager = \Magento\Framework\App\ObjectManager::getInstance(); 
//skus
// $productArray = array("7750087","7750100");
$file = fopen('skus.csv', 'r', '"'); // set path to the CSV file
$header = fgetcsv($file); // get data headers and skip 1st row
while ( $row = fgetcsv($file, 40000, ",") ) {

    $data = array();
    $data = array_combine($header, $row);

    $productSkus=$data["sku"];

   foreach($productArray as $key=>$productSkus){

   $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);

echo "Product Image has been deleted for".$productSkus."</br>";

}
}
echo "Yahooooo";

 ?>
0

The other solutions did not work for me. This one did, tested on M2.4.3.

private $gallery_rm
    
public function __construct(
  \Magento\Catalog\Model\ResourceModel\Product\Gallery $gallery_rm
) {
    $this->gallery_rm = $gallery_rm;
}

public function removeImagesFromProduct($product_object) {
    // get all images from product object
    $media_gallery_entries = $product_object->getMediaGalleryEntries();

    foreach($media_gallery_entries as $product_img) {
        $this->gallery_rm->deleteGallery($product_img->getId());
    }
}

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.