1

I want to tidy up this answer i gave here:

How to disable a swatch, once it is been selected in Magento2.2?

Instead of completely overriding the function i would like to improve it by wrapping the original function call and adding my custom code after the original function like so:

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

    return function(swatchRenderer){

        var OnClickWrapper = wrapper.wrap(swatchRenderer.prototype._OnClick, function(originalSwatchRenderer){

            var result = originalSwatchRenderer();

            /*Custom code start*/
            console.log(this);
            //.css({"pointer-events":"none","opacity":"0.6"});
            alert("test");
            /*Custom code end*/

            return result;
        });

        swatchRenderer.prototype._OnClick = OnClickWrapper;
        return swatchRenderer;
    };
});

This is mostly functioning ok however want to access the parameters passed to the original function so i can grab the parent of the clicked element with $this:

_OnClick: function ($this, $widget, eventName)

Is it possible to access these from within the wrapper and and how to do so?

2

The arguments variable seemed to contain what i needed. After logging the object the specific element i needed seemed to be stored in: arguments[1][0]

Final code:

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

    return function(swatchRenderer){

        var OnClickWrapper = wrapper.wrap(swatchRenderer.prototype._OnClick, function(originalSwatchRenderer){
            var result = originalSwatchRenderer();

            /*Custom code start*/
            $(arguments[1][0]).parent().css({"pointer-events":"none","opacity":"0.6"});
            /*Custom code end*/
            
            return result;
        });

        swatchRenderer.prototype._OnClick = OnClickWrapper;
        return swatchRenderer;
    };
});

Your Answer

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

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