update cart from view page using ajax in magento 1.9

update cart from view page using ajax in magento 1.9

September 19, 2026 · Illustrated author avatarBy Stefan Bogdanescu
update cart from view page using ajax in magento 1.9

Optimizing Magento 1.9 Cart Update: A Developer’s Guide

Magento 1.9 provides robust e-commerce functionality out-of-the-box, but sometimes you may encounter limitations that hinder the user experience. One such issue is the delayed update of the cart header when updating quantities on the product view page.

The Problem: Stale Header Data

When you add an item to your cart or update its quantity, Magento’s default behavior refreshes the entire page, causing a noticeable delay and disrupting the shopping experience. This delay occurs because Magento relies on server-side rendering for the header data. As a result, the header cart items remain stale until the next full page load.

The Solution: Real-time Cart Update with AJAX

To solve this issue, we’ll employ AJAX to update the cart in real-time without refreshing the entire page. By using Magento’s built-in JSON API and some JavaScript magic, we can achieve a smoother user experience while minimizing server requests.

Step 1: Modify the Add-to-Cart Form

First, let’s modify the checkout/cart/item/configure/updatecart.phtml template to include an AJAX loader element that will be displayed during the update process. This addition is crucial for providing visual feedback to the user:

<div class="add-to-cart" ...
    <span id='ajax_loader' style='display:none'>
        <img src='<?php echo $this->getSkinUrl('images/opc-ajax-loader.gif') ?>'/>
    </span>
...
</div>

Step 2: Update the Product Add-to-Cart JavaScript

Next, we’ll update the productAddToCartForm JavaScript in your view.phtml file to send an AJAX request instead of a regular form submission. Here’s how you can achieve this:

  1. Replace the default submit function with our custom version.
  2. Modify the form action URL to point to Magento’s JSON API endpoint (ajax/index).
  3. Add the serialized form data along with an additional parameter (isAjax=1) to signify an AJAX request.

Here’s the updated JavaScript code:

productAddToCartForm.submit = function(button, url) {
    if (this.validator.validate()) {
        var form = this.form;
        var oldUrl = form.action;

        // Start of our new ajax code
        if (!url) {
            url = jQuery('#product_addtocart_form').attr('action');
        }
        url = url.replace("checkout/cart", "ajax/index"); // New Code
        var data = jQuery('#product_addtocart_form').serialize();
        data += '&amp;isAjax=1';

        jQuery('#ajax_loader').show();

        try {
            jQuery.ajax({
                url: url,
                dataType: 'json',
                type : 'post',
                data: data,
                success: function(data){
                    console.log(data);
                    jQuery('#ajax_loader').hide();
                    alert(data.message);

                    // Update header cart items and other relevant elements
                    if(jQuery('#gd_total')){
                        jQuery("#gd_total").html( jQuery(data).find('#gd_total'));
                    }

                    if(jQuery('.count')){
                        var count=jQuery(data.toplink).find('.count').text();
                        jQuery(".count").html(count);
                        jQuery(".skip-cart").removeClass("no-count");
                    }
                    
                    if(jQuery('.minicart-wrapper')){
                        // jQuery('.header-minicart').replaceWith(data.toplink);
                        var cart=jQuery(data.toplink).find('.minicart-wrapper').html();
                        jQuery(".minicart-wrapper").html(cart);
                    }
                }
            });
        } catch (e) {
        }

        this.form.action = oldUrl;
    }
}.bind(productAddToCartForm);

Step 3: Update the Cart JSON API

Finally, ensure that Magento’s JSON API (ajax/index) is properly configured to return the updated cart data in a format compatible with our JavaScript code. By default, this endpoint should be enabled and functional for handling AJAX requests.

Conclusion

By following these steps, you’ll have successfully implemented real-time cart updates using AJAX on your Magento 1.9 store. This optimization not only enhances the user experience but also improves performance by reducing unnecessary server loads.

Remember to thoroughly test your implementation across different browsers and scenarios before deploying it to a production environment. Regularly review and update your code as needed to ensure compatibility with future Magento releases or security patches.

For more advanced Magento development guidance, feel free to explore our Magento 1.x resources at Laravel Company. We provide comprehensive tutorials, best practices, and expert insights to help you get the most out of your e-commerce platform.

Magento 1 Checkout Catalog Performance

Our network