Magento Update and Clean Shoping Cart using Ajax

Magento Update and Clean Shoping Cart using Ajax

October 11, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Magento Update and Clean Shoping Cart using Ajax

Clearing Magento Cart After AJAX Update: A Comprehensive Guide

Hello developers! I recently encountered an issue while working on a Magento project that involved updating a shopping cart via AJAX without refreshing the entire page. The code seemed to work perfectly during the request, but upon reloading or navigating away from the page, the updated cart quantities remained unchanged.

Let me walk you through the process of resolving this common problem using best practices in Magento development.

Understanding the Issue

The given code correctly sets up an AJAX request that updates the product quantity in the shopping cart. However, the issue arises from how Magento handles session data and caching mechanisms.

When a page is refreshed or navigated away from after an AJAX update, Magento may not always update its cache with the latest changes immediately, leading to discrepancies between what you see on the screen and what’s actually stored in the database.

Step-by-Step Solution

  1. Ensure you’re using Magento 2: The provided code assumes a Magento 1 environment. For Magento 2, the approach is slightly different due to changes in the session management system.

  2. Update your PHP script (updateCart.php)

Replace the initial require_once line with this:

require_once __DIR__ . '/app/bootstrap.php';
Mage::init();

This ensures that you’re properly initializing the Magento 2 bootstrap and getting an instance of the application.

  1. Clear Magento cache after the update

Add the following lines to your PHP script right after $quote->save():

$writer = new \Zend_Log_Writer_Stream(BP_LOG_DIR . '/cart.log');
$logger = new \Zend_Log();
$logger->addWriter($writer);
$logger->info('Clearing Magento cache...');
$cacheTypeList = ['config', 'layout', 'block_html', 'collections', 'reflection', 'translate'];
foreach ($cacheTypeList as $cacheType) {
    Mage::app()->getCacheInstance()->cleanType($cacheType);
}

This code clears multiple types of Magento cache that might be preventing your updated cart from being reflected on the page.

  1. Update your AJAX request

Ensure you’re sending a POST request with the productId and qty parameters:

function updateCart(productId, qty) {
    jQuery.ajax({
        type: "POST",
        dataType: "HTML",
        data: { productId : productId, qty : qty },
        url: "updateCart.php?productId=" + productId + "&qty=" + qty,
        success: function (data) {
            // alert("Success");
            window.location.reload();
        },
    error: function(data){
                alert("Failure");
             }
         });
     }

Adding window.location.reload() to the success callback forces a full page reload after the AJAX update, ensuring the updated cart is displayed.

  1. Ensure your cache is enabled

In your Magento 2 backend, navigate to System > Cache Management and make sure all caches are enabled, especially those mentioned in step 3 (Config, Layout, Block HTML, Collections).

Conclusion

Clearing the Magento cache after an AJAX update is crucial for ensuring that the changes are immediately visible to users. By following this comprehensive guide and updating your code accordingly, you should be able to resolve the issue successfully.

Remember, always test thoroughly on a staging environment before deploying these changes to a live site.

If you found this solution helpful or have any other Magento-related questions, feel free to explore our blog at [https://laravelcompany.com] where we regularly publish detailed tutorials and guides for developers like you.

Magento 2 Magento 1 Checkout Catalog

Our network