Sometimes you want to change product image without changing image name for many reason.
Problem : if changed images was already generated in pub/media/catalog/product/cache/
folders you need to delete all
generated cached images to have your new image in front and in cache.
Solution : this module which clean product image generated cache on product save.
How It Works • Installation • Add your own clean cache strategy • Support • Contact • License
When you save a product in the back-office it will delete all generated image cache file from the saved product in pub/media/catalog/product/cache/*
composer require blackbird/magento-2-clean-product-image-cache
php bin/magento setup:upgrade
In production mode, do not forget to recompile and redeploy the static resources.
If you use a CDN like Cloudflare, you maybe need to use her API to clean the CDN cache. We give you two way to do that :
- create your own strategy class
- create your observer on our event
blackbird_image_cache_clean_after
If you are using Cloudflare we have an extension of this module which call Cloudflare API to purge cache : blackbird/magento-2-clean-cloudflare-image-cache
Create your own strategy service implementing Blackbird\CleanProductImageCache\Api\CleanCacheStrategyInterface
class MyCleanCacheStrategy implements CleanCacheStrategyInterface
{
/**
* {@inheritDoc}
*/
public function clean(ProductInterface $product): void
{
//Do what you need to do
}
}
...
<type name="Blackbird\CleanProductImageCache\Model\Service\CleanCacheStrategyPool">
<arguments>
<argument name="cleanCacheStrategies" xsi:type="array">
<item name="my_clean" xsi:type="array">
<item name="class" xsi:type="object">Vendor\Module\Model\Service\MyCleanCacheStrategy</item>
<item name="sortOrder" xsi:type="number">10</item>
<item name="enabled" xsi:type="boolean">true</item>
</item>
</argument>
</arguments>
</type>
...
You can use sortOrder and enabled to change execution order or disable strategies.
class AfterCleanMagentoImageCache implements ObserverInterface
{
/**
* {@inheritDoc}
*/
public function execute(Observer $observer): void
{
//Get absolute path off all cached images for the cleaned product
$paths = $observer->getPaths();
//Do what you need to do
}
}
...
<event name="blackbird_image_cache_clean_after">
<observer name="event_custom_name" instance="Vendor\Module\Observer\AfterCleanMagentoImageCache" />
</event>
...
- If you have any issue with this code, feel free to open an issue.
- If you want to contribute to this project, feel free to create a pull request.
For further information, contact us:
- by email: hello@bird.eu
- or by form: https://black.bird.eu/en/contacts/
This project is licensed under the MIT License - see the LICENSE file for details.
That's all folks !