How can I get Magento add to cart ajax push the right url and add the product to cart?

How can I get Magento add to cart ajax push the right url and add the product to cart?

September 9, 2026 · Illustrated author avatarBy Stefan Bogdanescu
How can I get Magento add to cart ajax push the right url and add the product to cart?

Implementing Add to Cart AJAX Push for Magento

As a senior developer specializing in e-commerce platforms like Magento, I often encounter challenges in implementing custom functionality such as the add-to-cart feature using AJAX push. The Magento platform provides a robust set of tools and templates that can sometimes limit our flexibility when dealing with complex frontend interactions.

You’re facing an issue where you want to perform an AJAX post request with the right URL from your form, but you’re currently limited by Magento’s template setup which only allows for <a> tags. Let me guide you through a practical solution that leverages the existing framework while addressing your specific requirements.

Understanding the Current Setup

Your current implementation uses a combination of Magento’s default form submission and custom AJAX handling. The primary issue lies in the way you’re trying to replace the URL for the AJAX post request. When you use form.action = url, it overwrites the entire action attribute, which includes the necessary product data Magento expects.

A Step-by-Step Solution

  1. Preserve the Form’s Initial State Before modifying the form’s action, store its initial state somewhere in your JavaScript code. You can create a global variable to hold this value:
var originalFormAction = null;

// When the document is ready
$(document).ready(function() {
    // Store the initial action of the 'product_addtocart_form'
    originalFormAction = $('#product_addtocart_form').attr('action');
});
  1. Find the Correct URL To find the right URL from the clicked <a> tag, you can use a simple jQuery selector that targets the parent form:
var clickedUrl = null;

// Attach an event listener to the 'js-add-to-cart' class
$('.js-add-to-cart').click(function(e) {
    e.preventDefault(); // Prevent the default link behavior

    // Find the parent form and get its action attribute
    var form = $(this).closest('form');
    clickedUrl = form.attr('action');

    // Continue with your AJAX logic here...
});
  1. Construct the AJAX URL Since you’re already capturing the initial form’s action, you can construct the new AJAX URL by simply appending ajax/index to this base URL:
var ajaxUrl = clickedUrl.replace("checkout/cart", "ajax/index");
  1. Modify the Form Action Temporarily Instead of overwriting the entire action attribute, you can create a new hidden input field that will hold the specific product URL needed for the AJAX post:
// Create a hidden input field
var urlInput = $('<input type="hidden" name="url" value="' + clickedUrl + '" />');

// Append this field to your form
form.append(urlInput);
  1. Send the AJAX Request Now, you can proceed with the AJAX request using the constructed URL and the existing form data:
$.ajax({
    url: ajaxUrl,
    type: 'POST',
    data: form.serialize(),
    success: function(response) {
        // Handle the response as before
        // ...
    }
});
  1. Restore the Form’s Initial State After the AJAX request is complete, you can restore the original action attribute to its initial state:
form.attr('action', originalFormAction);

Conclusion

By following this approach, you successfully maintain Magento’s default form structure while enabling the desired AJAX functionality. You leverage the existing template setup by temporarily modifying the form and restoring it after the request is completed.

This solution demonstrates how to work within the constraints of a platform like Magento while still achieving custom behavior using modern JavaScript techniques such as jQuery selectors, event handling, and AJAX requests.

As always, feel free to explore more advanced topics in my previous blog posts on LaravelCompany.com. I cover a wide range of e-commerce development best practices that can help you tackle similar challenges effectively.

Checkout Catalog Themes Errors

Our network