How to change price value dynamically in magento

How to change price value dynamically in magento

July 29, 2026 · Illustrated author avatarBy Stefan Bogdanescu
How to change price value dynamically in magento

Changing Magento Product Prices Dynamically Using AJAX

As a beginner developer exploring Magento for the first time, you’re likely looking for ways to enhance your store’s user experience through interactive features like dynamic pricing updates. One common scenario is allowing customers to adjust product options (like quantity or size) and see their updated total price without having to refresh the page.

Understanding the Current Setup

Let me start by analyzing your provided URL: https://dev.tangoprint.ch/magento/index.php/plakate/a1.html which demonstrates a basic Magento product detail page. The key element to note here is the hidden input field for quantity:

<input type="text" id="quantity" value="1">

This field is currently static, meaning the price displayed on the page won’t change until you manually refresh it after modifying the quantity.

Enabling Dynamic Pricing Updates

To achieve dynamic pricing updates without refreshing the entire page, we’ll need to implement AJAX functionality. Here are the high-level steps involved:

  1. Create an AJAX endpoint: In your Magento setup, create a new API or controller action that accepts the product ID and requested quantity as parameters. This endpoint will be responsible for calculating and returning the updated price.

    Example of such an endpoint: /magento/index.php/rest/V1/prices/update

  2. Handle form submission with AJAX: Modify your JavaScript to intercept the form submission event when the user adjusts the quantity input field. Instead of performing a regular page refresh, send an AJAX request to your newly created endpoint.

    Sample jQuery code:

    $('#quantity').on('change', function() {
        var productId = yourProductId;
        var quantity = $(this).val();
    
        $.ajax({
            url: '/magento/index.php/rest/V1/prices/update',
            data: {product_id: productId, quantity: quantity},
            type: 'POST',
            success: function(response) {
                // Update the price display with the response data
                $('#price-display').text(response.newPrice);
            }
        });
    });
    
  3. Update the price display: Once your AJAX request returns the updated price from the Magento backend, update the corresponding element on the frontend to reflect this new value.

Calculating Cart Totals

For updating the cart page with the new total price, you can follow a similar approach:

  1. Create an additional endpoint: Develop another API or controller action that retrieves the current contents of the customer’s shopping cart and returns the updated subtotal based on the latest pricing data.

    Example URL: /magento/index.php/rest/V1/carts/totals

  2. Update your cart page JavaScript: Implement an AJAX request to this new endpoint whenever you detect a change in the product quantities within the cart (e.g., using jQuery’s change event).

  3. Refresh the totals display: Once the AJAX response is received, update the subtotal and grand total fields on the cart page accordingly.

Conclusion

Implementing dynamic pricing updates in Magento requires a combination of backend API development and frontend JavaScript logic. By following the steps outlined above and leveraging Magento’s built-in REST APIs or custom controllers, you can create a seamless shopping experience for your customers with real-time price updates without page reloads.

For more detailed guides on Magento development best practices and common challenges faced by beginners like yourself, I highly recommend exploring the extensive tutorials available at https://laravelcompany.com/magento-tutorials. This resource offers clear, step-by-step instructions that can help you unlock your full potential as a Magento developer.

Catalog API Customers Pricing

Our network