0

Overriding 'Magento_Catalog/js/price-box' via mixin works in only Chrome and I can't understand why. Other jQuery widgets overriding works fine (breadcrumb, swatch-renderer)

$path-to-theme / requirejs-config.js

var config = {
  config: {
    mixins: {
      'Magento_Catalog/js/price-box': {
        'Magento_Catalog/js/custompricebox': true
      },
    }
  }
};

$path-to-theme / Magento_Catalog/web/js/custompricebox.js

define([
  'jquery',
  'Magento_Catalog/js/price-utils',
  'underscore',
  'mage/template',
  'jquery/ui'
], function ($, utils, _, mageTemplate) {
  'use strict';

  return function (priceBox) {
    return $.widget('mage.priceBox', priceBox, {
      _init: function () {
        this._super();
        console.log('Hello theme!');
      }
    });
  }
});

And it's works only in Chrome. Screenshots:

Chrome:

enter image description here

Firefox:

enter image description here

1

2 Answers 2

3

This should fix the filtering issue

<script type="text/x-magento-init">
    {
        "[data-role=priceBox][data-price-box=product-id-<?= $block->escapeHtml($_product->getId()) ?>]": {
            "Magento_Catalog/js/price-box": {
                "priceConfig":  <?= /* @escapeNotVerified */ $block->getJsonConfig() ?>
            }
        }
    }
</script>

See /app/code/Magento/Swatches/view/frontend/templates/product/listing/renderer.phtml

1
  • also check this file Magento_Catalog/templates/product/view/form.phtml here you have add same code if there is any old code of dataPriceBoxSelector = '[data-role=priceBox]' Commented Jun 29, 2022 at 7:52
2

The problem was that the price-box widget was called this way in the module-catalog/view/frontend/templates/product/view/form.phtml:

priceBoxes.priceBox({'priceConfig': <?= /* @escapeNotVerified */ $block->getJsonConfig() ?>});

in this script section:

var dataPriceBoxSelector = '[data-role=priceBox]',
    dataProductIdSelector = '[data-product-id=<?= $block->escapeHtml($_product->getId()) ?>]',
    priceBoxes = $(dataPriceBoxSelector + dataProductIdSelector);

priceBoxes = priceBoxes.filter(function(index, elem){
    return !$(elem).find('.price-from').length;
});

priceBoxes.priceBox({'priceConfig': <?= /* @escapeNotVerified */ $block->getJsonConfig() ?>});

If you override this template and call the priceBox widget this way, it works:

<script type="text/x-magento-init">
    {
        "[data-role=priceBox]": {
            "Magento_Catalog/js/price-box": {
                "priceConfig":  <?= /* @escapeNotVerified */ $block->getJsonConfig() ?>
            }
        }
    }
</script>

With this way, you can't filter the price boxes.

1
  • THANK YOU. Still did not work with that selector, but with .price-box, it works. Did you ever found a proper solution ? I mean without rewriting anything but JS and not phtml ? Commented Jul 5, 2019 at 15:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.