Magento2: Extending swatch functionality using javascript mixins does not work in Safari and MS Edge

Created on 17 Aug 2017  路  10Comments  路  Source: magento/magento2


Preconditions

Magento 2.1.8

Steps to reproduce

Create a custom module with mixin as describe in http://devdocs.magento.com/guides/v2.1/javascript-dev-guide/javascript/js_mixins.html for Magento_Swatches/js/swatch-renderer. Then go a category page that has products with swatches and reviews your console log in each browser to see if the "mixin" got executed. Please disable browser cache because sometimes after viewing multiple pages in Safari the mixins will get download/cached and start to work sporadically.

app/code/MagePal/SwatchExtend/view/frontend/requirejs-config.js

var config = {
    config: {
        mixins: {
            'Magento_Swatches/js/swatch-renderer': {
                'MagePal_SwatchExtend/js/swatch-renderer-mixin': true
            }
        }
    }
};

app/code/MagePal/SwatchExtend/view/frontend/web/js/swatch-renderer-mixin.js

define([
    'jquery'
], function ($) {
    'use strict';

    return function (widget) {
        console.log('Hello from SwatchExtend');

        $.widget('mage.SwatchRenderer', widget, {
            _Rebuild: function () {
                console.log('Hello from rebuild method');
                return this._super();
            }
        });

        return $.mage.SwatchRenderer;
    }
});

Full source code to replicate the issue - https://github.com/srenon/Magento2-SwatchExtend

Expected result


Console.log() will trigger in all browser consistently

Actual result


Only work consistently in Chrome and Firefox.
Work sporadically in Safari, Mobile Safari and MS Edge (I did not test on any other browsers).

Please note: if you are testing in an extremely slow environment (i.e local dev, cache disabled etc) then it may work in safari but once you move to a faster environment it will not work anymore. So if it working consistently in Safari and your environment is in default or developer mode then switch your environment into production mode.

image

image

Fixed in 2.1.x Fixed in 2.2.x Fixed in 2.3.x Clear Description Confirmed Format is valid Ready for Work Reproduced on 2.1.x Reproduced on 2.2.x Reproduced on 2.3.x bug report

Most helpful comment

Same issue for me. Firefox and Safari can't see the extended file

All 10 comments

Having the same issue on Firefox, on Chrome it works perfectly, but Firefox seems to ignore the extended file

@lduivenvoorde ... our solution was to rewrite https://github.com/magento/magento2/blob/2.1/app/code/Magento/Swatches/view/frontend/templates/product/listing/renderer.phtml in our local theme

(should work for M2 < 2.2)

app/design/frontend/{COMPANY}/{THEME}/Magento_Swatches/templates/product/listing/renderer.phtml

<div class="swatch-opt-<?php /* @escapeNotVerified */ echo $block->getProduct()->getId() ?>" data-role="swatch-option-<?php /* @escapeNotVerified */ echo $block->getProduct()->getId() ?>"></div>

<script type="text/x-magento-init">
    {
        "[data-role=swatch-option-<?php /* @escapeNotVerified */ echo $block->getProduct()->getId() ?>]": {
            "Magento_Swatches/js/swatch-renderer": {
                "selectorProduct": ".product-item-details",
                "onlySwatches": true,
                "enableControlLabel": false,
                "numberToShow": <?php /* @escapeNotVerified */ echo $block->getNumberSwatchesPerProduct(); ?>,
                "jsonConfig": <?php /* @escapeNotVerified */ echo $block->getJsonConfig(); ?>,
                "jsonSwatchConfig": <?php /* @escapeNotVerified */ echo $block->getJsonSwatchConfig(); ?>,
                "mediaCallback": "<?php /* @escapeNotVerified */ echo $block->getMediaCallback() ?>"
            }
        }
    }
</script>

Same issue for me. Firefox and Safari can't see the extended file

Thanks @srenon!! your solution works nice also in M2.0 if you change the name of the resource

<div class="swatch-opt-<?php /* @escapeNotVerified */ echo $block->getProduct()->getId() ?>" data-role="swatch-option-<?php /* @escapeNotVerified */ echo $block->getProduct()->getId() ?>"></div>
<script type="text/x-magento-init">
    {
        "[data-role=swatch-option-<?php /* @escapeNotVerified */ echo $block->getProduct()->getId() ?>]": {
            "SwatchesRenderer": {
                "selectorProduct": ".product-item-details",
                "onlySwatches": true,
                "enableControlLabel": false,
                "numberToShow": <?php /* @escapeNotVerified */ echo $block->getNumberSwatchesPerProduct(); ?>,
                "jsonConfig": <?php /* @escapeNotVerified */ echo $block->getJsonConfig(); ?>,
                "jsonSwatchConfig": <?php /* @escapeNotVerified */ echo $block->getJsonSwatchConfig(); ?>,
                "mediaCallback": "<?php /* @escapeNotVerified */ echo $block->getMediaCallback() ?>"
            }
        }
    }
</script>

@srenon - Thanks for the fix!
My issue was the same as @lduivenvoorde: It worked perfectly in Chrome, sporadically in Safari, and not at all in Firefox. Using x-magento-init worked for me in M2.2, although I left the other 2.2 functionality as it was, so my product/listing/renderer.phtml ended up looking like this:

<div class="swatch-opt-<?= $id ?>"></div>
<script type="text/x-magento-init">
    {
        ".swatch-opt-<?= $id ?>": {
            "Magento_Swatches/js/swatch-renderer": {
                "selectorProduct": ".product-item-details",
                "onlySwatches": true,
                "enableControlLabel": false,
                "numberToShow": <?= /* @escapeNotVerified */ $block->getNumberSwatchesPerProduct() ?>,
                "jsonConfig": <?= /* @escapeNotVerified */ $block->getJsonConfig() ?>,
                "jsonSwatchConfig": <?= /* @escapeNotVerified */ $block->getJsonSwatchConfig() ?>,
                "mediaCallback": "<?= /* @escapeNotVerified */ $block->getMediaCallback() ?>"
            }
        }
    }
</script>
<script>
    require([
        'jquery',
        'jquery/ui',
        'Magento_Swatches/js/swatch-renderer',
        'Magento_Swatches/js/catalog-add-to-cart',
        'priceBox'
    ], function ($) {
        var jsonConfig = <?= /* @escapeNotVerified */ $block->getJsonConfig() ?>,
            dataPriceBoxSelector = '[data-role=priceBox]',
            dataProductIdSelector = '[data-product-id=<?= $id ?>]',
            priceBoxes = $(dataPriceBoxSelector + dataProductIdSelector);

        priceBoxes.priceBox({
            'priceConfig': {
                priceFormat: jsonConfig.priceFormat,
                prices: jsonConfig.prices
            }
        });
    });
</script>

@Brengineer ... take a look at PR to fix 2.2 https://github.com/magento/magento2/pull/12929/files

@srenon, thank you for your report.
We've acknowledged the issue and added to our backlog.

Hi @srenon. Thank you for your report.
The issue has been fixed in magento/magento2#12928 by @srenon in 2.1-develop branch
Related commit(s):

The fix will be available with the upcoming 2.1.14 release.

Hi @srenon. Thank you for your report.
The issue has been fixed in magento/magento2#14247 by @rostyslav-hymon in 2.3-develop branch
Related commit(s):

The fix will be available with the upcoming 2.3.0 release.

Was this page helpful?
0 / 5 - 0 ratings