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.

4
  • In your _create function: console.log(this.options.optionConfig)? Commented Sep 1, 2016 at 7:07
  • this.options.optionConfig is null Commented Sep 1, 2016 at 7:38
  • You can add your full code lines of your custom module? Commented Sep 2, 2016 at 4:52
  • 1
    Correct approach is to use mix-ins. See working solution here : magento.stackexchange.com/a/172920/53806 Commented May 4, 2017 at 0:37

1 Answer 1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.