Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle AJAX add to cart for simple products #386

Merged
merged 3 commits into from
Mar 12, 2024

Conversation

mikkamp
Copy link
Contributor

@mikkamp mikkamp commented Mar 11, 2024

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:

  1. If the product data attribute is not available on the add to cart button, then extract it from the value (WC sets it on the value in this template).
  2. The handlers / formatter is given the result of searching for a product. However in some cases a product is not found by the function 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 disable add_to_cart tracking, should we conditionally hook into that event?

Closes #382

Detailed test instructions:

  1. Setup tracking including add_to_cart tracking
  2. Install a plugin to do AJAX add to cart: https://wordpress.org/plugins/woo-ajax-add-to-cart/
  3. Create a simple product
  4. Add simple product on a single product page
  5. Confirm that the product is tracked
  6. Create a page with the shortcode [products] and ensure AJAX add to cart on archives is enabled in WC > Settings
  7. Add a simple product from the archive page
  8. Confirm that the product is tracked

Additional 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:

  1. Add single product 3x to cart
  2. Refresh archive page
  3. Add same single product to cart
  4. View tracking data and the quantity is set to 3

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

  • Fix - Handle AJAX add to cart for simple products

@mikkamp mikkamp self-assigned this Mar 11, 2024
@github-actions github-actions bot added changelog: fix Took care of something that wasn't working. type: bug The issue/PR is a confirmed bug. labels Mar 11, 2024
@mikkamp mikkamp requested a review from a team March 11, 2024 16:20
@puntope
Copy link
Contributor

puntope commented Mar 11, 2024

Thanks @mikkamp ✅ LGTM

Create a page with the shortcode [products] and ensure AJAX add to cart on archives is enabled in WC > Settings
Add a simple product from the archive page
Confirm that the product is tracked

These 3 steps are also fixed in #378 (or at least I cannot reproduce them when using that branch) So I tested everything with that branch merged as well to be sure is not a conflict and is handled twice.

@mikkamp
Copy link
Contributor Author

mikkamp commented Mar 12, 2024

These 3 steps are also fixed in #378 (or at least I cannot reproduce them when using that branch) So I tested everything with that branch merged as well to be sure is not a conflict and is handled twice.

Thanks for double checking. The adjusted logic makes it hard to reproduce after #378, but the important part of this PR is that it also falls back to getting the product ID from the value of a button (which is where WC places it for simple products).

Since this is approved I'm going to go ahead and merge it.

@mikkamp mikkamp merged commit 58b47dc into trunk Mar 12, 2024
1 check passed
@mikkamp mikkamp deleted the fix/382-handle-ajax-add-to-cart branch March 12, 2024 07:22
@tomalec tomalec mentioned this pull request Mar 12, 2024
19 tasks
@lith-imad
Copy link

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.

I think this is a critical bug and should be fixed in its own issue. For anyone who is looking for a quick fix, here's what I've done as workaround:

var addToCartButton = document.querySelector('.single_add_to_cart_button');
if (addToCartButton) {
	addToCartButton.dataset.product_id =  addToCartButton.parentElement.querySelector('input[name="product_id"]')?.value;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
changelog: fix Took care of something that wasn't working. type: bug The issue/PR is a confirmed bug.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

CRITICAL BUG in new tracking implementation (v2.0.1)
3 participants