0

I seem to have an issue deleting all images programatically from configurable products. I know the bad practice way that works fine on simple products

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

                        try{
                            $existingMediaGalleryEntries = $p->getMediaGalleryEntries();
                            if($existingMediaGalleryEntries != null) {
                                foreach ($existingMediaGalleryEntries as $key => $entry) {
                                    unset($existingMediaGalleryEntries[$key]);
                                }

                                $p->setMediaGalleryEntries($existingMediaGalleryEntries);
                                $p = $this->productRepository->save($p);
                                $p->save();
                            }
                        }catch(\Exception $e)
                        {
                            $message = array('type' => 'error', 'message' => 'Error  >> cant save product '.$e->getMessage());
                            $this->getLogger()->error(print_r($message, true));
                        }

Using the above on a configurable product however is not working so i tried the below, in matching the mock tests in catalog product

   $imageProcessor = $objectManager->create('\Magento\Catalog\Model\Product\Gallery\GalleryManagement');
        foreach($p->getMediaGalleryEntries() as $key => $entry)
        {
            $imageProcessor->remove($p->getSku(), $entry->getId());
        }

        try {

            $p = $this->productRepository->save($p);
            $p->save();

        }
        catch(\Exception $e)
        {
            $message = array('type' => 'error', 'message' => 'Error  >> cant save product '.$e->getMessage());
            $this->getLogger()->error(print_r($message, true));
        }

this is causing lock waits on the database and just stopping the crons from continuing to execute.

UPDATE:

Still no joy, im going to have to assume this is a bug

Any ideas?

1 Answer 1

1

to delete the images i ended up doing the following. Unsetting the gallery image and also removing the imgae by file. then physically removing the image from the filesystem.

$existingMediaGalleryEntries = $p->getMediaGalleryEntries();
                        if(is_array($existingMediaGalleryEntries)) {
                            foreach ($existingMediaGalleryEntries as $key => $entry) {
                                unset($existingMediaGalleryEntries[$key]);
                                $image = $entry->getFile();
                                $imageProcessor->removeImage($p, $image);
                                $image = 'pub/media/catalog/product' . $image;
                                if(file_exists($image)) {
                                    unlink($image);
                                }
                            }
                        }

                        $p->setMediaGalleryEntries($existingMediaGalleryEntries);
                        try {

                            $p = $this->productRepository->save($p);
                        }catch(\Exception $e)
                        {
                            $message = array('type' => 'error', 'message' => 'Falied Delete Image Error: ' . $e->getMessage() . ' line ' . $e->getLine());
                            $this->getLogger()->error(print_r($message, true));
                        }

And then to create images programatically

protected function getContentObject($fileName)
    {

        if ($this->io->fileExists($fileName)) {
            $content = $this->imageContentFactory->create();
            $content->setName(strtolower(basename($fileName)));
            $content->setBase64EncodedData(base64_encode(file_get_contents($fileName)));
            $content->setType($this->mime->getMimeType($fileName));

            return $content;
        }

        return null;
    }

 public function importSingle(array $item)
    {
        if ($content = $this->getContentObject($item['image'])) {
            try {
                $product = $this->productRepository->get($item['sku']);
            }catch(\Exception $e)
            {
                return false;
            }

            $entries = $product->getMediaGalleryEntries();
            $type = $item['types'];
            foreach ($entries as $lentry) {
                if (basename($lentry->getFile()) === $content->getName()) {
                   $entry = $lentry;
                   $type = array_unique(array_merge($type, $lentry->getTypes()));
                }
            }

            if (!isset($entry)) {
                $entry = $this->attributeMediaGalleryEntryFactory->create();
            }

            $entry->setContent($content);
            $entry->setMediaType('image');
            $entry->setDisabled(false);
            $entry->setTypes($type);

            $entries[] = $entry;
            $product->setMediaGalleryEntries($entries);

            try {

                $p = $this->productRepository->save($product);

            } catch (\Exception $e) {
                $message = array('type' => 'error', 'message' => 'Falied newname Product Image Error: ' . $e->getMessage() . ' line ' . $e->getLine());
                $this->getLogger()->info(print_r($message, true));

                $message = array('type' => 'error', 'message' => 'Error  >> File Newname Doesnt Exist: ' . $item['image']);
                $this->getLogger()->info(print_r($message, true));
                return false;
            }

            return $p;

        }else {
            $message = array('type' => 'error', 'message' => 'Error  >> File Newname Doesnt Exist: ' . $item['image']);
            $this->getLogger()->info(print_r($message, true));
            return false;
        }

        return false;


    }

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.