Solved: Category Image Deletes After Initial Save in Magento 2
Magento 2 migration is recommended by experts owing to the security issues in Magento 1. Moreover, the store owner can leverage features like page builder, enhanced GraphQL, better admin panel, etc. with the latest Magento 2.4.3
However, sometimes, store owners face issues like category image deletes after initial save in Magento 2 after the site migration. Either the category image uploaded cannot be seen in the frontend or editing the image does not work.
The solution to this issue is given below:
Solution for Category Image Deletes After Initial Save in Magento 2 Issue:
Open vendor\magento\module-catalog\Model\Category\Attribute\Backend\Image.php
Edit function afterSave and put below code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
public function afterSave($object) { $value = $object->getData($this->additionalData . $this->getAttribute()->getName()); if ($this->isTmpFileAvailable($value) && $imageName = $this->getUploadedImageName($value)) { try { $this->getImageUploader()->moveFileFromTmp($imageName); } catch (\Exception $e) { $this->_logger->critical($e); } } return $this; } |
That’s it.
I suggest that you override this file in any extension, do not directly change it in the vendor folder.
Any doubts in the above solution can be mentioned in the Comments section below. I’d be happy to help.
Please share the solution with the Magento Community via social media.
Thank you.
Jignesh Parmar
An expert in his field, Jignesh is the team leader at Meetanshi and a certified Magento developer. His passion for Magento has inspired others in the team too. Apart from work, he is a cricket lover.
2 Comments
When I tried to edit aftersave file, it shows an error in code.
Hello Farzeen,
The above code is working properly from our end.
What error you are facing from your end?
Thank You