Handle AJAX add to cart for simple products #386
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Changes proposed in this Pull Request:
This PR resolves the issue that an add to cart AJAX request from a single product page was throwing an error.
WC core doesn't handle AJAX from single product pages, however it can be replicated with a plugin. It's also quite common for themes to include this kind of behaviour.
Reported in the forum: https://wordpress.org/support/topic/js-error-on-product-page-2/
The PR resolved two issues:
getProductFromID
, in such cases we need to prevent from formatting the data.We also need to be careful with the handler
document.body.onadded_to_cart
as we always hook into this regardless if we disableadd_to_cart
tracking, should we conditionally hook into that event?Closes #382
Detailed test instructions:
add_to_cart
tracking[products]
and ensure AJAX add to cart on archives is enabled in WC > SettingsAdditional details:
This PR points out a separate issue that we search for products in the cart items first and then the list of products, see here.
That's a problem when it comes to the quantity number, which we can see in the following scenario:
Ideally we should be setting the quantity back to 1. Also is it really relevant to search through cart data, even though the product ID matches, we might have added one with a discount / addon which could make the price different. We'll need to log this in a separate issue.
Another issue is that it still won't work for variable products added to AJAX as we'll need to access the form data to find out which variation was selected.
Changelog entry