0

I am trying to update the product SKU based on the product selection chosen..

I have the following code that works, but only works if the configurable product is in stock (eg at least one of the configurations is marked as "stock")

define([
    'jquery',
    'mage/utils/wrapper'
], function ($, wrapper) {
    'use strict';

    return function (targetModule) {

        var updatePrice = targetModule.prototype._UpdatePrice;
        targetModule.prototype.dynamic = {};

        $('[data-dynamic]').each(function () {
            var code = $(this).data('dynamic');
            var value = $(this).html();

            targetModule.prototype.dynamic[code] = value;
        });

        var updatePriceWrapper = wrapper.wrap(updatePrice, function (original) {

            var dynamic = this.options.jsonConfig.dynamic;

            for (var code in dynamic) {
                if (dynamic.hasOwnProperty(code)) {
                    var value = "";
                    var $placeholder = $('[data-dynamic=' + code + ']');
                    var allSelected = true;

                    if (!$placeholder.length) {
                        continue;
                    }

                    for (var i = 0; i < this.options.jsonConfig.attributes.length; i++) {
                        if (!$('div.product-info-main .product-options-wrapper .swatch-attribute.' + this.options.jsonConfig.attributes[i].code).attr('option-selected')) {
                            allSelected = false;
                        }
                    }
                    if (allSelected) {
                        // DO SOMETHING HERE

                    } else {
                        value = this.dynamic[code];
                    }
                    $placeholder.html(value);
                }
            }
            return original();
        });
        targetModule.prototype._UpdatePrice = updatePriceWrapper;
        return targetModule;
    };
});

If all of the configurations are marked as "Out of stock" then this code does not work, no errors, just simply doesn't work.

Can any one help?

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.