delgraf / module-option-image-scope
Store view scoped option value images for MageWorx Advanced Product Options on Magento 2: a different set of images (main image replacement, overlay, swatch, tooltip) for each store view, with fallback to the default set
Package info
github.com/Delgraf/Magento2-OptionImageScope
Type:magento2-module
pkg:composer/delgraf/module-option-image-scope
Requires
- php: >=8.2
- magento/framework: *
- magento/module-store: *
- mageworx/module-optionbase: >=2.21.3
- mageworx/module-optionfastedit: *
- mageworx/module-optionfeatures: >=2.35.0
- mageworx/module-optiontemplates: *
Requires (Dev)
None
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
Delgraf_OptionImageScope lets you upload a different set of option value images for each store view in MageWorx Advanced Product Options: the images that replace the main product image, overlay images, option gallery images, swatches and tooltips.
It is an add-on: it does not change the MageWorx files in vendor/, and all MageWorx settings keep working.
The problem
MageWorx Advanced Product Options saves the images of an option value only once, for all store views. When you open a product in a store view and change the images in "Manage Images", the new images replace the images of every store view. You cannot, for example, show a different overlay or a different main image for the Italian and the English store view.
Features
- One image set per store view: switch the product form to a store view, open "Manage Images" on an option value and save. The images apply to that store view only.
- Default images with fallback: the images saved in "All Store Views" are the default set. A store view without its own images uses them, option value by option value.
- "Use Default Value" without a checkbox: when a store view is saved with exactly the same images as the default set, no copy is created, and an existing copy is removed. Removing every image in a store view also goes back to the default images.
- Everywhere the images are shown: main product image replacement and overlay on the product page, option gallery, swatches, tooltips, cart, mini cart, checkout summary and GraphQL (with the
Storeheader). - Admin form: the "Manage Images" modal shows the images of the store view selected in the scope switcher, with "Lazy Load" on or off.
- Product duplication copies the images of every store view.
- Safe automatic flows: REST/SOAP API, import/export, Option Templates and Fast Edit only read and write the default images, and never delete the images of a store view.
- Existing images are kept: after the installation they become the default set.
- No patches to
vendor/. An uninstaller removes the store view images and the database column withbin/magento module:uninstall --remove-data.
Requirements
- Magento Open Source / Adobe Commerce 2.4.x or Mage-OS, PHP 8.2+
- MageWorx Advanced Product Options with
mageworx/module-optionbase2.21.3 or later andmageworx/module-optionfeatures2.35 or later, including the Option Templates and Option Fast Edit modules - Written against
mageworx/module-optionfeatures2.35.4 on Magento 2.4.9 with the Swissup Argento Luxury theme
Installation
Run all commands from the Magento root directory, as the user that owns the Magento files. Back up your code and database first, and try the module on a staging site before production.
The installation adds a store_id column to the mageworx_optionfeatures_option_type_image table. The existing images get store_id = 0 and become the default images.
Option 1: Composer (recommended)
-
Put the store in maintenance mode (production stores only):
bin/magento maintenance:enable
-
Download the module:
composer require delgraf/module-option-image-scope
-
Enable the module and update Magento.
setup:upgradeadds thestore_idcolumn:bin/magento module:enable Delgraf_OptionImageScope bin/magento setup:upgrade
-
In production mode, compile the code:
bin/magento setup:di:compile
The module has no templates, styles or scripts, so the static content does not need to be deployed again. If
setup:upgraderemoved it, deploy it as usual withbin/magento setup:static-content:deploy. -
Clear the cache and leave maintenance mode:
bin/magento cache:flush bin/magento maintenance:disable
Option 2: Manual (app/code)
- Download the latest version as a ZIP from the Tags page and extract it.
- Copy its contents to
app/code/Delgraf/OptionImageScope/, so thatregistration.phpis atapp/code/Delgraf/OptionImageScope/registration.php. - Run steps 1 and 3 to 5 of the Composer installation.
Check the installation
-
Check that the module is enabled:
bin/magento module:status Delgraf_OptionImageScope
-
Check that the column was added, running this query on the Magento database (add your table prefix if you use one):
SHOW COLUMNS FROM mageworx_optionfeatures_option_type_image LIKE 'store_id';
-
Open a product with option images on the storefront: the images are the same as before the installation.
-
Follow Using it on one option value, then open the product in that store view and in another one: each shows its own images.
Update
Read the CHANGELOG before updating, and back up your code and database. The module follows Semantic Versioning: minor and patch versions (1.x) are backward compatible, while a new major version (2.0) may need changes on your side.
Installed with Composer
-
Check the installed version and whether a newer one is available:
composer show delgraf/module-option-image-scope composer outdated delgraf/module-option-image-scope
-
Put the store in maintenance mode (production stores only):
bin/magento maintenance:enable
-
Update the package. This stays within the version range in your
composer.json(for example^1.0), so it never jumps to a new major version:composer update delgraf/module-option-image-scope
To move to a new major version, change the constraint on purpose, for example
composer require delgraf/module-option-image-scope:^2.0. -
Update Magento and recompile (production mode), then clear the cache and leave maintenance mode:
bin/magento setup:upgrade bin/magento setup:di:compile bin/magento cache:flush bin/magento maintenance:disable
Installed manually (app/code)
-
Download the new version as a ZIP from the Tags page and extract it.
-
Put the store in maintenance mode (step 2 above).
-
Delete the old files and copy in the new ones, so that no file from the old version is left behind:
rm -rf app/code/Delgraf/OptionImageScope mkdir -p app/code/Delgraf/OptionImageScope cp -R /path/to/extracted/Magento2-OptionImageScope-x.y.z/. app/code/Delgraf/OptionImageScope/
-
Run step 4 above.
After updating
- Check the version:
composer show delgraf/module-option-image-scope, or theCHANGELOG.mdin the module folder for a manual installation. - Check a product with store view images as described in Check the installation.
When you update MageWorx Advanced Product Options, check a product with store view images again, in the admin and on the storefront. The module relies on the MageWorx classes listed in How it works.
Using it
The module has no settings: it works as soon as it is enabled.
Images for a store view
- Go to Catalog → Products and open a product with custom options.
- In the Scope switcher at the top left, choose the store view.
- In Customizable Options, click Manage Images on an option value. The modal shows the images of that store view, or the default images if the store view has none yet.
- Upload, remove or reorder the images and set their roles as usual: "Replace Main Gallery Image", "Overlay Image", "Base Image", "Tooltip Image".
- Save the images and save the product.
The other store views are not changed.
Default images
Choose All Store Views in the scope switcher and manage the images as usual. These images are used by every store view that has no images of its own for that option value.
Going back to the default images
In the store view, open Manage Images and either make the images the same as the default ones, or remove them all. Save the product: the store view images are deleted and the store view uses the default images again.
What is always on the default images
| Flow | Behaviour |
|---|---|
| REST/SOAP API | Reads and writes the default images, whatever the store code in the URL. |
| Product import and export (MageWorx) | Exports and imports the default images. |
| Option Templates | Templates write the default images of the linked products. Store view images are kept. |
| Fast Edit | Shows and edits the default images. Store view images are kept. |
| CLI and cron | Read and write the default images. |
How it works
etc/db_schema.xmladds thestore_idcolumn tomageworx_optionfeatures_option_type_image, with a foreign key to thestoretable (images of a deleted store view are deleted too) and an index onoption_type_id+store_id.Model\StoreContextdecides which store view is used: in the admin, the store view of the scope switcher (storerequest parameter); on the storefront and in GraphQL, the current store view; in every other area, 0.Model\ScopeConditionbuilds the SQL condition that, for each option value, selects the rows of the store view if there are any, and the rows withstore_id = 0otherwise.- Save:
Plugin\Attribute\ImageAttributePlugin(onMageWorx\OptionFeatures\Model\Attribute\OptionValue\Image) adds thestore_idto the saved rows, deletes only the rows of the store view being saved, skips the rows that are the same as the default images (Model\DefaultImageSet) and copies the images of every store view when a product is duplicated. - Storefront, cart and admin modal:
Plugin\Collection\ImageCollectionPluginadds the condition toMageWorx\OptionFeatures\Model\ResourceModel\Image\Collection, used by the product page (SwatchMediaData), the cart renderer, the mini cart, the checkout summary and the "Manage Images" modal. - Option value collection:
Plugin\CollectionUpdater\RoleImageJoinPluginadds the condition to the joins of theBaseImage,TooltipImageandOverlayImagecollection updaters, andPlugin\CollectionUpdater\ImagesDataPluginto theimages_datasubquery of theImagesupdater. Option Templates have their own table and are not changed. - Admin form with Lazy Load:
Model\Loader\ScopedImagesLoaderreplacesMageWorx\OptionFeatures\Model\Loader\ImagesLoader(preference) and loads the images of the store view of the scope switcher. - Fast Edit:
Plugin\FastEdit\ImageWriterPluginandPlugin\FastEdit\ProjectionReaderPluginlimit Fast Edit to the default images. Setup\Uninstalldeletes the store view images and removes thestore_idcolumn when the module is removed withbin/magento module:uninstall --remove-data.
Limitations
- When you add a new option value while editing a store view, the images you upload for it apply to that store view only, and the other store views show no images for it. Upload the default images from "All Store Views" first.
- If an option template re-creates the option values of a product (new value IDs), the store view images of those values are deleted together with the values, like the rest of the Advanced Product Options data of the values.
- In the Fast Edit lists, the image count of an option value also includes the images of the store views.
Troubleshooting
A store view still shows the default images
-
Check that you saved the product with that store view selected in the scope switcher, and not in "All Store Views".
-
If the images are the same as the default ones, the module does not save a copy: this is the expected behaviour.
-
Clear the cache: product pages are stored in the full page cache.
-
Check the rows of the option value, running this query on the Magento database (replace
123with the option value ID,option_type_id):SELECT option_type_image_id, store_id, value, replace_main_gallery_image, overlay_image FROM mageworx_optionfeatures_option_type_image WHERE option_type_id = 123 ORDER BY store_id, sort_order;
The images of a store view changed after an import or a template update
Imports, the API and Option Templates write the default images only. If the default images changed, the store views without images of their own show the new default images: this is the expected behaviour. Store views with their own images are not changed.
"Manage Images" shows the default images in a store view that has its own
- Clear the cache and reload the product page in the admin.
- In production mode, check that
setup:di:compilewas run after the installation: the module replaces the MageWorx images loader with a preference. - If another module also replaces
MageWorx\OptionFeatures\Model\Loader\ImagesLoader, only one of the two preferences is used. Please open an issue with the name of the other module.
Uninstall
The module adds no database tables of its own. It adds the store_id column to the MageWorx table mageworx_optionfeatures_option_type_image, and the rows of that table with a store_id other than 0 are the images of the store views.
Important: without this module, MageWorx shows all the rows of an option value together. Always delete the store view images before disabling or removing the module, as described below. The default images (
store_id = 0) are kept and MageWorx goes on using them. The image files inpub/mediaare never deleted.
Installed with Composer (recommended: with the uninstaller)
Magento's module:uninstall command removes the package and, with --remove-data, runs the module's uninstaller (Setup/Uninstall.php). The uninstaller deletes the store view images, then removes the foreign key, the index and the store_id column.
-
Uninstall the module and its data. The command puts the store in maintenance mode while it runs:
bin/magento module:uninstall Delgraf_OptionImageScope --remove-data
Add
--backup-dbto back up the database first. Do not leave out--remove-data, unless you delete the store view images by hand first (step 2 of the step-by-step uninstall). -
In production mode, recompile, then clear the cache:
bin/magento setup:di:compile bin/magento cache:flush
Installed with Composer, step by step
Use these steps if you prefer not to use module:uninstall. The uninstaller does not run, so the store view images must be deleted by hand (step 2).
-
Put the store in maintenance mode (production stores only):
bin/magento maintenance:enable
-
Delete the store view images, running this query on the Magento database (add your table prefix if you use one). This step is required:
DELETE FROM mageworx_optionfeatures_option_type_image WHERE store_id <> 0;
-
Disable the module:
bin/magento module:disable Delgraf_OptionImageScope
-
Remove the package:
composer remove delgraf/module-option-image-scope
-
Update Magento and recompile (production mode), then clear the cache and leave maintenance mode:
bin/magento setup:upgrade bin/magento setup:di:compile bin/magento cache:flush bin/magento maintenance:disable
-
Remove the
store_idcolumn ifsetup:upgradedid not remove it. Check it with the query of Check the installation; if it is still there, run these queries. The column does nothing without the module, so this step is optional:ALTER TABLE mageworx_optionfeatures_option_type_image DROP FOREIGN KEY <foreign key name>, DROP INDEX <index name>, DROP COLUMN store_id;
Find the names with
SHOW CREATE TABLE mageworx_optionfeatures_option_type_image;: the foreign key is theCONSTRAINTonstore_idthat references thestoretable, the index is theKEYonoption_type_id,store_id.
Installed manually (app/code)
Magento's module:uninstall command only works for modules installed with Composer, so the uninstaller cannot run here.
-
Run steps 1 to 3 of the step-by-step Composer uninstall.
-
Delete the module folder:
rm -rf app/code/Delgraf/OptionImageScope
If
app/code/Delgraf/is now empty, you can delete it too. -
Run steps 5 and 6 of the step-by-step Composer uninstall.
Disabling the module for a while
To turn the module off without uninstalling it, delete the store view images first (step 2 of the step-by-step uninstall), then run bin/magento module:disable Delgraf_OptionImageScope and bin/magento setup:upgrade. The store view images cannot be restored afterwards, unless you saved a backup of the table.
License
OSL-3.0 (Open Software License 3.0)