Magento 2 calculate product price against user input

Magento 2 calculate product price against user input

October 17, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Magento 2 calculate product price against user input

Calculating Product Price Based on User Input in Magento 2

As an e-commerce developer new to Magento 2, calculating product prices based on user input can be a daunting task. You’ve already created the necessary attributes in the admin panel, but updating the price on the frontend seems to be causing some issues.

Understanding the Current Implementation

Let’s first analyze your current JavaScript code:

require([
        'jquery','Magento_Catalog/js/price-utils','jquery/ui','Magento_Catalog/js/price-box'
    ], function($,priceUtils,priceBox){

        "use strict";
        var priceBoxes = $('[data-role=priceBox]');
        $('.product-custom-optionval').change(function () {
            var value = $(this).val();

            if (value === '') { 
                $(this).val(0);
                updatePriceOnChange(0, $(this).attr('data-attr-id'));
                return;
            }
            disableAddToCartBtn('#product_addtocart_form');
            calculatePrice($(this));
            enableAddToCartBtn('#product_addtocart_form');
        });

    ****** But after calculating the price what will my approach to update product price, please help *******
</code></pre>

The code you’ve shared is correctly setting up event listeners for the change events on your custom option input fields. It disables the “Add to Cart” button during calculations and enables it afterward.

However, you’re rightfully concerned about how to actually update the product price after performing the calculation. The updatePriceOnChange function call seems to be missing the necessary parameters or logic to apply the calculated price to the product.

Updating the Product Price

To update the product price, we need to make use of Magento 2’s pricing API and event system. Here’s a step-by-step approach:

  1. Calculate the new price: Use your custom calculation logic in the calculatePrice function. This could involve multiplying or adding the input values with the base price.
function calculatePrice(context) {
    var value = context.val();
    // Your calculation goes here, e.g., new_price = base_price + (value * multiplier);
}
  1. Trigger a pricing event: After calculating the new price, trigger a pricing update event in Magento 2. This will ensure that the updated price is reflected across the platform.
Magento_Catalog/js/price-utils:updatePrice(
    selector,
    value,
    [eventName='pricing:apply', data={}]
);

In this example, we’re using the updatePrice function from the Magento_Catalog/js/price-utils module to trigger a pricing event with the calculated price.

  1. Update the product price in the cart: If you want the updated price to appear in the shopping cart as well, you’ll need to create an observer that listens for the pricing update event and updates the cart item accordingly.

For more details on creating observers in Magento 2, refer to our guide: Extending Magento 2 with Observers.

Remember, any custom pricing logic should be thoroughly tested to ensure it aligns with your business rules and does not introduce any security vulnerabilities.

Conclusion

Calculating product prices based on user input in Magento 2 requires a strong understanding of the platform’s JavaScript API and event system. By following the approach outlined above—calculating the price, triggering a pricing update event, and potentially updating the cart items—you can achieve your desired functionality.

Don’t hesitate to explore more about Magento 2’s pricing APIs and events in the official documentation or by reaching out to our team of experienced developers at Laravel Company.

Magento 2 Checkout Catalog Database

Our network