infrangible / m2-catalog-product-swatches
Groundwork for other Infrangible modules: shows variant-specific product information on configurable product pages as soon as the shopper selects a variant.
Package info
github.com/infrangible-dev/m2-catalog-product-swatches
Type:magento2-module
pkg:composer/infrangible/m2-catalog-product-swatches
Requires
- php: >=8.1
- fewedev/base: ~2.4.1
- infrangible/m2-core: ~5.0.4
- infrangible/m2-foundation: ~3.0.1
- magento/framework: ~103.0.6
- magento/module-catalog: ~104.0.0
- magento/module-configurable-product: ~100.4.0
- magento/module-eav: ~102.1.0
- magento/module-swatches: ~100.4.0
- psr/log: ~2.0.0||~3.0.0
Requires (Dev)
- infrangible/phpstan-magento: ~1.2.0
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
Base of modules that show values of the selected variant on the page of a configurable product: the page knows which variant the selected swatches (or options) match and updates the values whenever the selection changes. The module changes nothing in the store on its own.
Usage
An observer of the event catalog_product_swatch_attributes (data: product, swatch_attributes) adds the attributes
whose values are shown:
/** @var \Infrangible\CatalogProductSwatches\Model\SwatchAttributes $swatchAttributes */ $swatchAttributes = $observer->getEvent()->getData('swatch_attributes'); $swatchAttributes->addAttribute( $observer->getEvent()->getData('product'), 'pos', // attribute code '.catalog-product-pos', // CSS selector of the element that shows the value 'catalog-product-pos-template', // ID of the element with the template (mage/template) ', ' // separator of the labels of multiselect attributes );
- The values are the labels of the options in the store of the product, or the plain values of attributes without options; only enabled variants count.
createAndAddSwatchAttribute()adds an attribute with values of another source (product ID => value).- The template gets
attributeId,attributeCode,attributeCodeClass,attributeValueandattributeValueClass(code and value in lower case with_for other characters than letters and digits). - When the selection changes,
.column.maintriggers the eventswatch.changedwith the ID of the selected variant (or null) and the selected option IDs by attribute ID.
The Hyvä theme needs Infrangible Catalog Product Swatches Hyvä.
Requirements
- Magento Open Source or Adobe Commerce 2.4.6 or newer
- PHP 8.1 or newer
Installation
composer require infrangible/m2-catalog-product-swatches bin/magento module:enable Infrangible_CatalogProductSwatches bin/magento setup:upgrade
In production mode, also run bin/magento setup:di:compile and bin/magento setup:static-content:deploy.
License
Infrangible Catalog Product Swatches is licensed under the MIT License - see the LICENSE file for details.