shopwhizzy / magento2-swatchcache
Magento 2 module to cache swatch configurations for large configurable products
Installs: 2
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 0
Forks: 0
Open Issues: 0
Type:magento2-module
Requires
- php: >=7.4
- magento/framework: >=102.0.0
- magento/module-catalog: >=103.0.0
- magento/module-configurable-product: >=100.3.0
- magento/module-cron: >=100.3.0
- magento/module-swatches: >=100.3.0
README
Module Name: shopwhizzy/magento2-swatchcache
Version: 1.0.11
Description: Magento 2 module to cache swatch configurations for large configurable products, improving performance by reducing frontend rendering time.
License: Proprietary
Author: ShopWhizzy
Overview
The ShopWhizzy_SwatchCache
module enhances the performance of Magento 2 stores with large configurable products by caching swatch configurations (json_config
and json_swatch_config
) in the var/swatches
directory. It includes a console command, a cron job, an observer, and an admin configuration interface to manage cache generation and clearing. Suitable for Hyvä Themes shops.
Features
- Cache Generation Command: Run
bin/magento shopwhizzy:swatchcache:generate
to generate cache files for configurable products, with optional product ID filtering and force regeneration. - Cron Job: Automatically generates cache files for a configurable number of products (default: 2) every 2 minutes, skipping products with existing caches or errors.
- Observer: Clears cache files for a product when it is saved, if enabled in configuration.
- Admin Configuration:
- Enable/disable cache clearing on product save.
- Enable/disable cron job with customizable schedule, products per run, and delay between requests.
- AJAX-powered "Clear All Swatch Cache" button to delete all cache files in
var/swatches
.
- Performance Optimization: Skips products with existing caches and handles errors gracefully during cache generation.
Requirements
- PHP >= 7.4
- Magento 2.4.x or higher
- Required Magento modules:
Magento_Catalog
(>=103.0.0)Magento_ConfigurableProduct
(>=100.3.0)Magento_Swatches
(>=100.3.0)Magento_Cron
(>=100.3.0)
Installation
Via Composer
- Add the module to your Magento 2 project:
composer require shopwhizzy/magento2-swatchcache
- Enable the module and update Magento:
bin/magento setup:upgrade bin/magento cache:clean
- Deploy static content (if in production mode):
bin/magento setup:static-content:deploy
Manual Installation
- Download the module and place it in
app/code/ShopWhizzy/SwatchCache
. - Enable the module and update Magento:
bin/magento setup:upgrade bin/magento cache:clean
- Deploy static content (if in production mode):
bin/magento setup:static-content:deploy
Configuration
-
Navigate to Stores > Configuration > ShopWhizzy > Swatches Cache in the Magento admin panel.
-
Configure the following settings:
General
- Enable Cache Clearing on Product Save: Enable/disable the observer that clears cache files when a product is saved (default: Enabled).
Cron Settings
- Enable Cron: Enable/disable the cron job for cache generation (default: Enabled).
- Cron Schedule: Set the cron expression (default:
*/2 * * * *
for every 2 minutes). - Products per Cron Run: Number of valid products to process per cron run (default: 2).
- Delay Between Requests (ms): Delay between processing each product in milliseconds (default: 1000ms).
Cache Management
- Clear All Swatch Cache: Click the button to delete all files in
var/swatches
via AJAX (only visible when cron is enabled).
-
Save the configuration and clear the cache:
bin/magento cache:clean config
Usage
Generate Cache Manually
Run the console command to generate cache files for all configurable products or specific product IDs:
bin/magento shopwhizzy:swatchcache:generate [--force] [product_ids]
--force
: Regenerate cache files even if they exist.product_ids
: Space-separated list of product IDs (e.g.,bin/magento shopwhizzy:swatchcache:generate 1 2 3
).
Cron Job
The cron job runs every 2 minutes by default, processing 2 valid configurable products per run, skipping products with existing caches or errors. Logs are written to var/log/system.log
.
Clear Cache
- Via Admin Panel: Go to Stores > Configuration > ShopWhizzy > Swatches Cache > Cache Management and click "Clear All Swatch Cache".
- Via Observer: Cache files for a product are cleared automatically on save if the observer is enabled.
Troubleshooting
- Button Not Working: Ensure the cron is enabled (
Enable Cron
set to "Yes") and clear JavaScript/cache:bin/magento cache:clean js bin/magento setup:static-content:deploy
- Cron Not Running: Verify Magento's cron is configured:
crontab -l
Ensurevar/log/system.log
shows cron activity. - Permission Issues: Check that
var/swatches
is writable by the web server user. - JavaScript Errors: Open browser developer tools (F12) to check for errors in the Console or Network tabs. Ensure
ShopWhizzy_SwatchCache/js/clear-cache.js
loads correctly. - ACL Restrictions: Ensure the admin user has the
ShopWhizzy_SwatchCache::clear_cache
permission in System > Permissions > User Roles.
License
This module is licensed under a proprietary license. See the LICENSE
file or contact ShopWhizzy for details.
Support
For issues or feature requests, contact ShopWhizzy support at info@shopwhizzy.com or open an issue on the module's repository (if available).