I need to extend Magento_Catalog/js/price-box.js. I have used the 'mixins' feature, but it's not working for price-box.js.
requirejs-config.js:
var config = {
config: {
mixins: {
'Magento_Catalog/js/price-box': {
'My_Module/js/price-box/pluggin': true
}
}
}
};
My_Module/view/frontend/web/js/price-box/pluggin.js
define(function () {
'use strict';
return function (target) {
// modify target
var reloadPrice = target.reloadPrice;
target.reloadPrice = function() {
cosole.log("hello");
};
return target;
};
});
-
Yogesh, Give some more information about this.Codrain Technolabs Pvt Ltd– Codrain Technolabs Pvt Ltd2016年04月22日 08:41:22 +00:00Commented Apr 22, 2016 at 8:41
1 Answer 1
Specify PriceBox js file in your custom module's
requirejs-config.jswith the same name by which its already declared in core modules. in our case it ispriceBoxlike below. Your modulesrequirejs-config.jswould be something likevar config = { map: { '*': { priceBox:'namespace_modulename/js/custompricebox', } } };Now, create the file
custompricebox.jsto the path specified above. i am assuming you want to extendreloadPricemethod in price-box. so yourcustompricebox.jswould be like below.define( [ 'jquery', 'Magento_Catalog/js/price-utils', 'underscore', 'mage/template', 'mage/priceBox', 'jquery/ui' ], function (,ドル utils, _, mageTemplate) { 'use strict'; $.widget('yournamespace.custompriceBox', $.mage.priceBox, { /** * Render price unit block. */ reloadPrice: function reDrawPrices() { var priceFormat = (this.options.priceConfig && this.options.priceConfig.priceFormat) || {}, priceTemplate = mageTemplate(this.options.priceTemplate); _.each(this.cache.displayPrices, function (price, priceCode) { price.final = _.reduce(price.adjustments, function(memo, amount) { return memo + amount; }, price.amount); // you can put your custom code here. price.formatted = utils.formatPrice(price.final, priceFormat); $('[data-price-type="' + priceCode + '"]', this.element).html(priceTemplate({data: price})); }, this); }, }); return $.yournamespace.custompriceBox; } );Please note this code is not tested. there might be some syntex errors. let me know if you need more help on this.
-
Hi Yagnesh, Can we achieve it via Mixin ? Instead of override it, can we extend it too ?Praful Rajput– Praful Rajput2016年04月22日 09:28:09 +00:00Commented Apr 22, 2016 at 9:28
-
@PrafulRajput, I don't use mixin yet, i will surely update you on this once i do it.Codrain Technolabs Pvt Ltd– Codrain Technolabs Pvt Ltd2016年04月22日 09:33:58 +00:00Commented Apr 22, 2016 at 9:33
-
2somehow this does not work for me (ver. 2.1.2). Also mage/priceBox does give me some script error.TrytoFly– TrytoFly2017年01月04日 08:58:41 +00:00Commented Jan 4, 2017 at 8:58
-
1Did someone succeeded to rewrite it through Mixin ?Pol Ravalitera– Pol Ravalitera2017年09月04日 08:21:34 +00:00Commented Sep 4, 2017 at 8:21
-
any update on this?Kowsigan Atsayam– Kowsigan Atsayam2020年02月25日 10:08:49 +00:00Commented Feb 25, 2020 at 10:08