Why doesn't adding to cart work from a collection page?

I’m need help in resolving an issue where adding a product to the cart does not work when navigating through a collection page, while it works fine when accessing the product directly. Despite several debugging attempts, including inspecting network requests, reviewing JavaScript functions, and adding console logs for debugging, the issue persists. There seems to be a discrepancy in how the add-to-cart functionality behaves based on the URL structure or the page context. Any insights or suggestions from the Shopify Community would be greatly appreciated.

Issue Summary:

Problem Description:

  • When navigating to a product via the dropdown menu and selecting a category (e.g., hats), adding the product to the cart does not work. However, adding the same product to the cart works as expected when navigating directly from the main page, bypassing the category route.

URL Structures:

Tests Conducted:1. Comparison of Network Requests:

  • Analyzed network requests when adding a product to the cart from both the collection page and the product page. Observed a 400 Bad Request error when adding from the collection page, while the direct product page worked fine.
  1. Review of AJAX Add-to-Cart Functionality:

    • Examined the AJAXFormAddToCart and AJAXAddToCart functions in the functions.js file, responsible for handling add-to-cart operations. Suggested adding console.log statements for debugging, but no logs were observed when clicking the button.
  2. Inspection of Button’s onClick Event:

    • Inspected the Liquid code for the add-to-cart button, which calls an addToCart function. Added console.log to the onClick attribute and directly into the addToCart function for debugging. Again, no console output was observed upon clicking the button.
  3. Exploration of Potential Causes:

    • Discussed various potential causes including event propagation, JavaScript errors on the page, conflicts between scripts or libraries, dynamic content loading, browser extensions, script execution context, and server-side configurations.

I’m stumped on this issue and could use some guidance on where else to troubleshoot and see what’s happening.

Please try to remove your browser’s cache and check again or use another browser to check. I have checked and cannot see the issue you mentioned on my side.

Thanks for checking this out Dan. Yes, I’ve confirmed this bug after refreshing cache as well as on a separate computer, browser, and through mobile. It looks like the bug doesn’t show itself by just following the hyperlink you have to navigate to the products through a specific route.

Here’s the specific route that causes the bug:
Home > Shop > Hats > Chickadee Corduroy Hat (or any other product in this collection) > Add to Cart

Thanks for helping!

One other thing to add: The add-to-cart functionality works as expected if I reload the page.

Found the solution. There was a custom script that was finding product color variants and listing them as individual products on the collection page and saves the product variant id as the active product when the user clicks through to the product page. However, if there was only 1 variant the local stored data on the product page wouldn’t set properly. My solution was to add a counter on how many color variants were found on for each product on the collection page and if that product only had 1 variant, it wouldn’t log anything into localStorage, thus defaulting the product page to it’s original default functionality.