0

I have a single price on the page so i create a method on the priceBox to collect it via an API.

The problem i am having is that from my other JS module i can not access this widget or method. Every time i do so it will execute the _create or _init on that price-box which triggers a second call (the _create call).

Is there a way to to retrieve the actual instance of the widget and just call that refresh method?

There are different things i already tried though:

# Unknown function
const priceBox = $('.price-box', '.product-info-price').priceBox();
priceBox.collectPricesForProduct(sku);

# New create is invoked
var priceBox = $.mage.priceBox;
priceBox().collectPricesForProduct(sku);

Below is the example code of the actual price-box. Hope someone can help!

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

    return function (widget) {
        $.widget('mage.priceBox', widget, {
            _create: function initPriceBox() {
                this._super();
                var sku = $('div.product-info-main .product-info-price .product.attribute.sku div').html();
                this.collectPricesForProduct(sku);
            },
            collectPricesForProduct: function(sku, language){
                $('.price-box').hide();
                jQuery('.price-box').parent().prepend('<i class="fa fa-circle-o-notch fa-2x fa-spin"></i>');

                var self = this;
                $('#product-addtocart-button').attr('disabled', 'disabled');
                request.done(function (response) {
                    const prices = {
                        baseOldPrice: {
                            amount: response.prices.excl
                        },
                        oldPrice: {
                            amount: response.prices.incl
                        },
                        basePrice: {
                            amount: response.prices.excl
                        },
                        finalPrice: {
                            amount: response.prices.incl
                        }
                    };
                    self.options.prices = prices;
                    self.element.trigger('updatePrice');
                    $('.price-box').show().parent().find('.fa-spin').remove();
                    $('#product-addtocart-button').removeAttr('disabled');
                });
                request.error(function () {
                    alert('fatal error, no price');
                    $('.price-box').show().parent().find('.fa-spin').remove();
                    $('#product-addtocart-button').removeAttr('disabled');
                });
            }
        });
        return $.mage.priceBox;
    }
});

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.