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

Add checks and explicit errors when cart buttons are missing data #443

Merged
merged 3 commits into from
Jun 25, 2024

Conversation

tomalec
Copy link
Member

@tomalec tomalec commented Jun 24, 2024

Changes proposed in this Pull Request:

Add checks and explicit errors when cart buttons are missing data
to help debugging issues with other extensions manipulating WC Core add & remove from cart buttons or events.

This would help identifying issues like the one reported at https://wordpress.org/support/topic/js-error-on-add-to-cart-when-logged-out/

Checks:

  • Does your code follow the WordPress coding standards?
  • Have you written new tests for your changes, as applicable?
  • Have you successfully run tests with your changes locally?
  • Have you checked to ensure there aren't other open Pull Requests for the same update/change?

Screenshots:

image

Detailed test instructions:

  1. Build the branch
  2. open shop page
  3. Run synthetic/malformed cart events
jQuery( document.body ).trigger( 'added_to_cart' );
jQuery( document.body ).trigger( 'removed_from_cart' );
  1. Check for errors in the console
  2. Add an item to the cart using UI.
  3. Remove all product ids document.querySelectorAll('[data-product_id]').forEach((e)=>e.removeAttribute('data-product_id')). 8. Remove item from mini cart using UI
  4. Check for errors in the console
  5. Smoke test not tempered events and buttons for adding to & removing from cart, for regression

Additional details:

Changelog entry

Tweak - Add checks and explicit errors when cart buttons are missing data.

to help debugging issues with other extensions manipulating WC Core add & remove from cart buttons or events.
@tomalec tomalec requested a review from a team June 24, 2024 20:33
@tomalec tomalec self-assigned this Jun 24, 2024
@github-actions github-actions bot added the changelog: tweak Small change, that isn't actually very important. label Jun 24, 2024
Copy link
Member

@eason9487 eason9487 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the improvement. LGTM.

Commented on two minor concerns, and I believe it won't need another round even if there are related changes.

@@ -60,14 +60,20 @@ export function classicTracking(
}
// Get product ID from data attribute (archive pages) or value (single product pages).
const productID = parseInt(
button[ 0 ].dataset.product_id || button[ 0 ].value
button?.[ 0 ]?.dataset.product_id || button?.[ 0 ].value
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the purpose is to help debugging, perhaps it also needs to consider the case where the button parameter is an empty array/jQuery collection.

jQuery( document.body ).trigger( 'added_to_cart', [ , , jQuery('match-nothing') ] );

image

);

if ( isNaN( productID ) ) {
Copy link
Member

@eason9487 eason9487 Jun 25, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Although parseInt won't return a type other than number or NaN, it would be better to avoid using isNaN when possible because it has some confusing special-case behavior that leads to semantics ambiguity.

Maybe it could be replaced with Number.isNaN.

@tomalec
Copy link
Member Author

tomalec commented Jun 25, 2024

Thanks @eason9487 for the detailed review :)

@tomalec tomalec merged commit 2dcc95f into trunk Jun 25, 2024
1 check passed
@tomalec tomalec deleted the tweak/add-to-cart-button branch June 25, 2024 17:33
@eason9487 eason9487 mentioned this pull request Jul 2, 2024
19 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
changelog: tweak Small change, that isn't actually very important.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants