Magento2 core javascript object in custom javascript












0















i would like to access the javascript object for the product custom options and need to change the price of the custom option.
I have found below data for the custom options.



<script type="text/x-magento-init">
{
"#product_addtocart_form": {
"priceOptions": {
"optionConfig": {"1":{"1":{"prices":{"oldPrice":{"amount":1,"adjustments":},"basePrice":{"amount":1},"finalPrice":{"amount":1}},"type":"fixed","name":"Option1-1"},"2":{"prices":{"oldPrice":{"amount":2,"adjustments":},"basePrice":{"amount":2},"finalPrice":{"amount":2}},"type":"fixed","name":"Option1-2"},"3":{"prices":{"oldPrice":{"amount":3,"adjustments":},"basePrice":{"amount":3},"finalPrice":{"amount":3}},"type":"fixed","name":"Option1-3"},"4":{"prices":{"oldPrice":{"amount":4,"adjustments":},"basePrice":{"amount":4},"finalPrice":{"amount":4}},"type":"fixed","name":"Option1-4"}}},
"controlContainer": ".field",
"priceHolderSelector": "[data-role=priceBox]"
}
}
}
</script>


My concern is that how can i access the optionConfig from the above object. So I can modified it before magento change update price and then magento will update price accordingly.



I have created the below javascript widget.



define([
'jquery',
'underscore',
'mage/template',
'priceUtils',
'priceBox',
'priceOptions',
], function ($, _, mageTemplate, utils) {
'use strict';
var globalOptions = {
qtyFieldSelector: 'input.qty',
};
$.widget('mage.fixedprices', $.mage.priceOptions, {
options: globalOptions,
/**
* @private
*/
_create: function() {
console.log('hey, fixedprices is loaded!')
//bind click event of elem id
this.element.on('change', function(e){
console.log('change ME!');
});
this.element.on('change', this._onQtyFieldChanged.bind(this));
},

_onQtyFieldChanged: function onQtyFieldChanged(event) {
console.log("Magentoins fixedPrices", this.options);
}
});

return $.mage.fixedprices;

});


I am getting optionConfig as null.










share|improve this question
















bumped to the homepage by Community 4 mins ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
















  • In your _create function: console.log(this.options.optionConfig)?

    – Khoa TruongDinh
    Sep 1 '16 at 7:07













  • this.options.optionConfig is null

    – Ajay
    Sep 1 '16 at 7:38











  • You can add your full code lines of your custom module?

    – Khoa TruongDinh
    Sep 2 '16 at 4:52











  • Correct approach is to use mix-ins. See working solution here : magento.stackexchange.com/a/172920/53806

    – Jimmy Beats
    May 4 '17 at 0:37
















0















i would like to access the javascript object for the product custom options and need to change the price of the custom option.
I have found below data for the custom options.



<script type="text/x-magento-init">
{
"#product_addtocart_form": {
"priceOptions": {
"optionConfig": {"1":{"1":{"prices":{"oldPrice":{"amount":1,"adjustments":},"basePrice":{"amount":1},"finalPrice":{"amount":1}},"type":"fixed","name":"Option1-1"},"2":{"prices":{"oldPrice":{"amount":2,"adjustments":},"basePrice":{"amount":2},"finalPrice":{"amount":2}},"type":"fixed","name":"Option1-2"},"3":{"prices":{"oldPrice":{"amount":3,"adjustments":},"basePrice":{"amount":3},"finalPrice":{"amount":3}},"type":"fixed","name":"Option1-3"},"4":{"prices":{"oldPrice":{"amount":4,"adjustments":},"basePrice":{"amount":4},"finalPrice":{"amount":4}},"type":"fixed","name":"Option1-4"}}},
"controlContainer": ".field",
"priceHolderSelector": "[data-role=priceBox]"
}
}
}
</script>


My concern is that how can i access the optionConfig from the above object. So I can modified it before magento change update price and then magento will update price accordingly.



I have created the below javascript widget.



define([
'jquery',
'underscore',
'mage/template',
'priceUtils',
'priceBox',
'priceOptions',
], function ($, _, mageTemplate, utils) {
'use strict';
var globalOptions = {
qtyFieldSelector: 'input.qty',
};
$.widget('mage.fixedprices', $.mage.priceOptions, {
options: globalOptions,
/**
* @private
*/
_create: function() {
console.log('hey, fixedprices is loaded!')
//bind click event of elem id
this.element.on('change', function(e){
console.log('change ME!');
});
this.element.on('change', this._onQtyFieldChanged.bind(this));
},

_onQtyFieldChanged: function onQtyFieldChanged(event) {
console.log("Magentoins fixedPrices", this.options);
}
});

return $.mage.fixedprices;

});


I am getting optionConfig as null.










share|improve this question
















bumped to the homepage by Community 4 mins ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
















  • In your _create function: console.log(this.options.optionConfig)?

    – Khoa TruongDinh
    Sep 1 '16 at 7:07













  • this.options.optionConfig is null

    – Ajay
    Sep 1 '16 at 7:38











  • You can add your full code lines of your custom module?

    – Khoa TruongDinh
    Sep 2 '16 at 4:52











  • Correct approach is to use mix-ins. See working solution here : magento.stackexchange.com/a/172920/53806

    – Jimmy Beats
    May 4 '17 at 0:37














0












0








0








i would like to access the javascript object for the product custom options and need to change the price of the custom option.
I have found below data for the custom options.



<script type="text/x-magento-init">
{
"#product_addtocart_form": {
"priceOptions": {
"optionConfig": {"1":{"1":{"prices":{"oldPrice":{"amount":1,"adjustments":},"basePrice":{"amount":1},"finalPrice":{"amount":1}},"type":"fixed","name":"Option1-1"},"2":{"prices":{"oldPrice":{"amount":2,"adjustments":},"basePrice":{"amount":2},"finalPrice":{"amount":2}},"type":"fixed","name":"Option1-2"},"3":{"prices":{"oldPrice":{"amount":3,"adjustments":},"basePrice":{"amount":3},"finalPrice":{"amount":3}},"type":"fixed","name":"Option1-3"},"4":{"prices":{"oldPrice":{"amount":4,"adjustments":},"basePrice":{"amount":4},"finalPrice":{"amount":4}},"type":"fixed","name":"Option1-4"}}},
"controlContainer": ".field",
"priceHolderSelector": "[data-role=priceBox]"
}
}
}
</script>


My concern is that how can i access the optionConfig from the above object. So I can modified it before magento change update price and then magento will update price accordingly.



I have created the below javascript widget.



define([
'jquery',
'underscore',
'mage/template',
'priceUtils',
'priceBox',
'priceOptions',
], function ($, _, mageTemplate, utils) {
'use strict';
var globalOptions = {
qtyFieldSelector: 'input.qty',
};
$.widget('mage.fixedprices', $.mage.priceOptions, {
options: globalOptions,
/**
* @private
*/
_create: function() {
console.log('hey, fixedprices is loaded!')
//bind click event of elem id
this.element.on('change', function(e){
console.log('change ME!');
});
this.element.on('change', this._onQtyFieldChanged.bind(this));
},

_onQtyFieldChanged: function onQtyFieldChanged(event) {
console.log("Magentoins fixedPrices", this.options);
}
});

return $.mage.fixedprices;

});


I am getting optionConfig as null.










share|improve this question
















i would like to access the javascript object for the product custom options and need to change the price of the custom option.
I have found below data for the custom options.



<script type="text/x-magento-init">
{
"#product_addtocart_form": {
"priceOptions": {
"optionConfig": {"1":{"1":{"prices":{"oldPrice":{"amount":1,"adjustments":},"basePrice":{"amount":1},"finalPrice":{"amount":1}},"type":"fixed","name":"Option1-1"},"2":{"prices":{"oldPrice":{"amount":2,"adjustments":},"basePrice":{"amount":2},"finalPrice":{"amount":2}},"type":"fixed","name":"Option1-2"},"3":{"prices":{"oldPrice":{"amount":3,"adjustments":},"basePrice":{"amount":3},"finalPrice":{"amount":3}},"type":"fixed","name":"Option1-3"},"4":{"prices":{"oldPrice":{"amount":4,"adjustments":},"basePrice":{"amount":4},"finalPrice":{"amount":4}},"type":"fixed","name":"Option1-4"}}},
"controlContainer": ".field",
"priceHolderSelector": "[data-role=priceBox]"
}
}
}
</script>


My concern is that how can i access the optionConfig from the above object. So I can modified it before magento change update price and then magento will update price accordingly.



I have created the below javascript widget.



define([
'jquery',
'underscore',
'mage/template',
'priceUtils',
'priceBox',
'priceOptions',
], function ($, _, mageTemplate, utils) {
'use strict';
var globalOptions = {
qtyFieldSelector: 'input.qty',
};
$.widget('mage.fixedprices', $.mage.priceOptions, {
options: globalOptions,
/**
* @private
*/
_create: function() {
console.log('hey, fixedprices is loaded!')
//bind click event of elem id
this.element.on('change', function(e){
console.log('change ME!');
});
this.element.on('change', this._onQtyFieldChanged.bind(this));
},

_onQtyFieldChanged: function onQtyFieldChanged(event) {
console.log("Magentoins fixedPrices", this.options);
}
});

return $.mage.fixedprices;

});


I am getting optionConfig as null.







magento2 price custom-options






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Sep 1 '16 at 6:50







Ajay

















asked Jul 4 '16 at 14:16









AjayAjay

619210




619210





bumped to the homepage by Community 4 mins ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.







bumped to the homepage by Community 4 mins ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.















  • In your _create function: console.log(this.options.optionConfig)?

    – Khoa TruongDinh
    Sep 1 '16 at 7:07













  • this.options.optionConfig is null

    – Ajay
    Sep 1 '16 at 7:38











  • You can add your full code lines of your custom module?

    – Khoa TruongDinh
    Sep 2 '16 at 4:52











  • Correct approach is to use mix-ins. See working solution here : magento.stackexchange.com/a/172920/53806

    – Jimmy Beats
    May 4 '17 at 0:37



















  • In your _create function: console.log(this.options.optionConfig)?

    – Khoa TruongDinh
    Sep 1 '16 at 7:07













  • this.options.optionConfig is null

    – Ajay
    Sep 1 '16 at 7:38











  • You can add your full code lines of your custom module?

    – Khoa TruongDinh
    Sep 2 '16 at 4:52











  • Correct approach is to use mix-ins. See working solution here : magento.stackexchange.com/a/172920/53806

    – Jimmy Beats
    May 4 '17 at 0:37

















In your _create function: console.log(this.options.optionConfig)?

– Khoa TruongDinh
Sep 1 '16 at 7:07







In your _create function: console.log(this.options.optionConfig)?

– Khoa TruongDinh
Sep 1 '16 at 7:07















this.options.optionConfig is null

– Ajay
Sep 1 '16 at 7:38





this.options.optionConfig is null

– Ajay
Sep 1 '16 at 7:38













You can add your full code lines of your custom module?

– Khoa TruongDinh
Sep 2 '16 at 4:52





You can add your full code lines of your custom module?

– Khoa TruongDinh
Sep 2 '16 at 4:52













Correct approach is to use mix-ins. See working solution here : magento.stackexchange.com/a/172920/53806

– Jimmy Beats
May 4 '17 at 0:37





Correct approach is to use mix-ins. See working solution here : magento.stackexchange.com/a/172920/53806

– Jimmy Beats
May 4 '17 at 0:37










1 Answer
1






active

oldest

votes


















0














You can create the file view/frontend/requirejs-config.js in your module with this contents:



var config = {
map: {
'*': {
priceOptions: 'Your_Module/js/your-script'
}
}
};


This will cause 'priceOptions' to refer to your script instead of Magento_Catalog/js/price-options. You should then have access to the options array from inside your script.






share|improve this answer























    Your Answer








    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "479"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: false,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f124152%2fmagento2-core-javascript-object-in-custom-javascript%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    You can create the file view/frontend/requirejs-config.js in your module with this contents:



    var config = {
    map: {
    '*': {
    priceOptions: 'Your_Module/js/your-script'
    }
    }
    };


    This will cause 'priceOptions' to refer to your script instead of Magento_Catalog/js/price-options. You should then have access to the options array from inside your script.






    share|improve this answer




























      0














      You can create the file view/frontend/requirejs-config.js in your module with this contents:



      var config = {
      map: {
      '*': {
      priceOptions: 'Your_Module/js/your-script'
      }
      }
      };


      This will cause 'priceOptions' to refer to your script instead of Magento_Catalog/js/price-options. You should then have access to the options array from inside your script.






      share|improve this answer


























        0












        0








        0







        You can create the file view/frontend/requirejs-config.js in your module with this contents:



        var config = {
        map: {
        '*': {
        priceOptions: 'Your_Module/js/your-script'
        }
        }
        };


        This will cause 'priceOptions' to refer to your script instead of Magento_Catalog/js/price-options. You should then have access to the options array from inside your script.






        share|improve this answer













        You can create the file view/frontend/requirejs-config.js in your module with this contents:



        var config = {
        map: {
        '*': {
        priceOptions: 'Your_Module/js/your-script'
        }
        }
        };


        This will cause 'priceOptions' to refer to your script instead of Magento_Catalog/js/price-options. You should then have access to the options array from inside your script.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Sep 1 '16 at 7:52









        Aaron AllenAaron Allen

        6,5442927




        6,5442927






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Magento Stack Exchange!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f124152%2fmagento2-core-javascript-object-in-custom-javascript%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            What other Star Trek series did the main TNG cast show up in?

            Berlina muro

            Berlina aerponto