Magento2: core js module price-box.js dosyasını nasıl geçersiz kılabilirim


15

Uzatmam gerek Magento_Catalog/js/price-box.js. 'Mixins' özelliğini kullandım, ama işe yaramıyor 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, Bu konuda biraz daha bilgi ver.
— Codrain Technolabs Pvt Ltd

Yanıtlar:


12
  1. Özel modüllerinizde PriceBox js dosyasını requirejs-config.js, çekirdek modüllerde zaten belirtilen adla belirtin . bizim durumumuzda priceBoxaşağıdaki gibidir. Modülleriniz requirejs-config.jsşöyle bir şey olurdu:

    var config = {
        map: {
             '*': {
                    priceBox:'namespace_modulename/js/custompricebox',
             }
        }
    };
  2. Şimdi, dosyayı custompricebox.jsyukarıda belirtilen yola oluşturun . Ben reloadPricefiyat kutusunda yöntemini uzatmak istediğiniz varsayıyorum . yani custompricebox.jsaşağıdaki gibi olurdu.

    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;
        }
    );
  3. Bu kodun test edilmediğini lütfen unutmayın. bazı syntex hataları olabilir. Bu konuda daha fazla yardıma ihtiyacınız olursa bana bildirin.


Merhaba Yagnesh, Mixin ile başarabilir miyiz? Geçersiz kılmak yerine, genişletebilir miyiz?
— Praful Rajput

@PrafulRajput, henüz mixin kullanmıyorum, bunu yaptıktan sonra sizi kesinlikle güncelleyeceğim.
— Codrain Technolabs Pvt Ltd

2
bir şekilde bu benim için işe yaramıyor (ver. 2.1.2). Ayrıca mage / priceBox bana bazı script hatası veriyor.
— TrytoFly

1
Birisi Mixin aracılığıyla yeniden yazmayı başardı mı?
— Pol Ravalitera
Sitemizi kullandığınızda şunları okuyup anladığınızı kabul etmiş olursunuz: Çerez Politikası ve Gizlilik Politikası.
Licensed under cc by-sa 3.0 with attribution required.