"Add to cart" button doesn't work because of ReferenceError

"Add to cart" button doesn't work because of ReferenceError

August 15, 2026 · Illustrated author avatarBy Stefan Bogdanescu
"Add to cart" button doesn't work because of ReferenceError

The “Add to Cart” Button Issue in Magento: A Deep Dive

You’re facing an issue where your Magento “Add to cart” button has stopped working, throwing the error Uncaught ReferenceError: productAddToCartForm is not defined in the console. Let’s dive into this problem and find a solution.

Understanding the Problem

The error message suggests that the JavaScript variable productAddToCartForm is undefined when the button click event occurs. This variable is used to handle the form submission on the “Add to cart” click, so its absence causes the functionality to break.

Analyzing the Provided Code

You’ve shared the HTML for the “Add to cart” button and the associated JavaScript code from the page source:

<button class="button btn-cart" title="Добавить в корзину" type="button" onClick="productAddToCartForm.submit(this)">
<i class="icon-basket"></i>Добавить в корзину</button>

And here’s the relevant JavaScript code:

var productAddToCartForm = new VarienForm('product_addtocart_form');

The VarienForm is a custom form handling class provided by Magento. The issue seems to lie in how this object is being initialized and used.

Possible Causes of the Problem

There could be several reasons for this error:

  1. Script Not Loaded Before Button Click: If the JavaScript file containing the productAddToCartForm initialization isn’t loaded before the button click event, it will remain undefined.

  2. jQuery Conflict: Magento uses jQuery along with its own Varien libraries. A conflict between these libraries or another jQuery library could cause the script to not initialize properly.

  3. Theme or Extension Conflict: If you’ve installed a custom theme or extension that modifies the core Magento JavaScript files, it might be introducing an error that breaks the productAddToCartForm initialization.

Steps to Troubleshoot and Solve the Issue

Here are some steps we can take to troubleshoot this problem:

  1. Check JavaScript Console: Look for any other errors or warnings in the console before the Uncaught ReferenceError. This might give us a hint about what’s causing the issue.

  2. Inspect the Script Loading Order: Use browser developer tools to check if the script containing the productAddToCartForm initialization is loaded before the button click event occurs. If not, try moving the script to ensure it loads earlier.

  3. Check for jQuery Conflicts: Use a tool like jQuery NoConflict to avoid conflicts between Magento’s jQuery and other libraries on your site.

  4. Disable Custom Extensions or Themes Temporarily: To rule out any conflicts caused by extensions or themes, disable them temporarily and see if the issue persists.

  5. Update Your Magento Version: If you’re running an outdated version of Magento, consider updating to a newer stable release. Bugs in older versions might cause this kind of issue.

Conclusion

The “Add to cart” button error is likely caused by a JavaScript loading or conflict issue. By following the troubleshooting steps outlined above—checking script order, looking for conflicts, and temporarily disabling custom extensions—the problem should be resolvable.

Remember, maintaining your Magento installation on the latest stable version is key to avoiding such issues. Always keep your core files, themes, and extensions up-to-date with official releases.

If you’re still facing difficulties after trying these steps, feel free to reach out for further assistance or check out our Magento troubleshooting guide on LaravelCompany’s blog for more in-depth solutions.

Checkout Catalog Errors JavaScript

Our network