Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extending swatch functionality using javascript mixins does not work in Safari and MS Edge #10559

Closed
srenon opened this issue Aug 16, 2017 · 10 comments
Labels
bug report Fixed in 2.1.x The issue has been fixed in 2.1 release line Fixed in 2.2.x The issue has been fixed in 2.2 release line Fixed in 2.3.x The issue has been fixed in 2.3 release line Issue: Clear Description Gate 2 Passed. Manual verification of the issue description passed Issue: Confirmed Gate 3 Passed. Manual verification of the issue completed. Issue is confirmed Issue: Format is valid Gate 1 Passed. Automatic verification of issue format passed Issue: Ready for Work Gate 4. Acknowledged. Issue is added to backlog and ready for development Reproduced on 2.1.x The issue has been reproduced on latest 2.1 release Reproduced on 2.2.x The issue has been reproduced on latest 2.2 release Reproduced on 2.3.x The issue has been reproduced on latest 2.3 release

Comments

@srenon
Copy link
Member

srenon commented Aug 16, 2017

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

@magento-engcom-team magento-engcom-team added G1 Passed bug report Issue: Format is valid Gate 1 Passed. Automatic verification of issue format passed and removed G1 Passed labels Sep 5, 2017
@magento-engcom-team magento-engcom-team added the Issue: Clear Description Gate 2 Passed. Manual verification of the issue description passed label Sep 13, 2017
@lduivenvoorde-zz
Copy link

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

@srenon
Copy link
Member Author

srenon commented Nov 10, 2017

@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>

@nuovecode
Copy link

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

@nuovecode
Copy link

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>

@Brengineer
Copy link

Brengineer commented Feb 1, 2018

@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>

@srenon
Copy link
Member Author

srenon commented Feb 2, 2018

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

@magento-engcom-team
Copy link
Contributor

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

@magento-engcom-team magento-engcom-team added Issue: Ready for Work Gate 4. Acknowledged. Issue is added to backlog and ready for development Issue: Confirmed Gate 3 Passed. Manual verification of the issue completed. Issue is confirmed Reproduced on 2.1.x The issue has been reproduced on latest 2.1 release Reproduced on 2.2.x The issue has been reproduced on latest 2.2 release Reproduced on 2.3.x The issue has been reproduced on latest 2.3 release labels Mar 9, 2018
@magento-engcom-team magento-engcom-team added the Fixed in 2.2.x The issue has been fixed in 2.2 release line label Mar 16, 2018
@magento-engcom-team
Copy link
Contributor

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

The fix will be available with the upcoming 2.1.14 release.

@magento-engcom-team magento-engcom-team added the Fixed in 2.1.x The issue has been fixed in 2.1 release line label Mar 17, 2018
magento-engcom-team added a commit that referenced this issue Mar 17, 2018
 - Merge Pull Request #12928 from srenon/magento2:patch-7
 - Merged commits:
   1. 155f91c
@vkublytskyi
Copy link

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

The fix will be available with the upcoming 2.3.0 release.

@vkublytskyi vkublytskyi added the Fixed in 2.3.x The issue has been fixed in 2.3 release line label Mar 29, 2018
magento-engcom-team added a commit that referenced this issue Mar 29, 2018
… (M2.2) #14247

 - Merge Pull Request #14247 from rostyslav-hymon/magento2:2.3-develop-PR-port-12929
 - Merged commits:
   1. 3b27605
   2. 7d51276
   3. 0044fef
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug report Fixed in 2.1.x The issue has been fixed in 2.1 release line Fixed in 2.2.x The issue has been fixed in 2.2 release line Fixed in 2.3.x The issue has been fixed in 2.3 release line Issue: Clear Description Gate 2 Passed. Manual verification of the issue description passed Issue: Confirmed Gate 3 Passed. Manual verification of the issue completed. Issue is confirmed Issue: Format is valid Gate 1 Passed. Automatic verification of issue format passed Issue: Ready for Work Gate 4. Acknowledged. Issue is added to backlog and ready for development Reproduced on 2.1.x The issue has been reproduced on latest 2.1 release Reproduced on 2.2.x The issue has been reproduced on latest 2.2 release Reproduced on 2.3.x The issue has been reproduced on latest 2.3 release
Projects
None yet
Development

No branches or pull requests

7 participants