Magento 2 – How to Dynamically Change Price on Product Page

custom-optionsmagento2priceproduct

I am trying to update price of product on product page. So I am updating the span of price by getting product price + my additional price, but problem is when we use custom options.

Is there any event or plugin available using which I can add our price at final even after custom option.

Thank you,

Best Answer

OK ! I found one solution to change the price on product page itself using Javascript:

require(["jquery","Magento_Catalog/js/price-utils"], function(jQuery,priceUtils) {
    var productPrice = parseFloat(jQuery('span.price-final_price > span[data-price-type="finalPrice"]').attr('data-price-amount'));
    var finalPrice=100;
    formatedPrice = priceUtils.formatPrice(parseInt(finalPrice));
    jQuery('span.price-final_price > span.price-wrapper > span.price').text(formatedPrice);
    var finalPrice=productPrice+(pricePerText*textObjects)+(pricePerImage*imageObjects);
    formatedPrice = priceUtils.formatPrice(parseInt(finalPrice));
    jQuery('span.price-final_price > span.price-wrapper > span.price').text(formatedPrice);
}); // Required