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

Return empty array if WC_Cart is not yet instantiated #435

Merged
merged 2 commits into from
Jun 11, 2024

Conversation

martynmjones
Copy link
Contributor

Changes proposed in this Pull Request:

Closes #432

When the formatted cart data is added to the window.ga4w object, the main WC (core) class should have instantiated WC_Cart. As reported in #432, an error is sometimes being recorded if the cart is unavailable.

WooCommerce won't instantiate the cart if the request is not a frontend request. So this is likely caused by a cron configuration loading the frontend of the website or triggering the wp_head action at the very least. I've not been able to reproduce the error but this PR adds a simple check to confirm if the cart is available before accessing class methods.

Detailed test instructions:

  1. Checkout fix/432-error-if-cart-unavailable
  2. Debug test site using https://tagassistant.google.com
  3. Add products to cart
  4. Visit a classic cart page
  5. Remove a product from the cart
    ⚠️ Make sure the product you remove from cart isn't also displayed in the You may be interested in… section
  6. Confirm the remove_from_cart event is tracked in TagAssistant with the correct product information

Changelog entry

Tweak - Confirm WC_Cart is available before formatting cart data

@martynmjones martynmjones self-assigned this Jun 7, 2024
@github-actions github-actions bot added type: bug The issue/PR is a confirmed bug. changelog: fix Took care of something that wasn't working. labels Jun 7, 2024
@martynmjones martynmjones added changelog: tweak Small change, that isn't actually very important. and removed type: bug The issue/PR is a confirmed bug. labels Jun 7, 2024
@martynmjones martynmjones requested a review from a team June 7, 2024 14:04
Copy link
Member

@ianlin ianlin 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 fixing it. I could see the event remove_from_cart is tracked in TagAssistant using this PR. The code looks good as it avoids calling a function from null.

@ianlin
Copy link
Member

ianlin commented Jun 11, 2024

I saw the unit tests failed in the checks. I tested in my local and it also failed when using WC 8.9.3 (latest), this is the error when running ./bin/install-unit-tests.sh:

warning: Could not find remote branch 8.9.3 to clone.
fatal: Remote branch 8.9.3 not found in upstream origin

The woocommerce.json returns 8.9.3 as the latest WC version, but in WC GH repo there is no 8.9.3 branch.

@martynmjones martynmjones merged commit 903260a into trunk Jun 11, 2024
4 of 6 checks passed
@martynmjones martynmjones deleted the fix/432-error-if-cart-unavailable branch June 11, 2024 09:04
@martynmjones
Copy link
Contributor Author

Thanks for the review @ianlin!

I saw the unit tests failed in the checks. I tested in my local and it also failed when using WC 8.9.3 (latest), this is the error when running ./bin/install-unit-tests.sh:

warning: Could not find remote branch 8.9.3 to clone.
fatal: Remote branch 8.9.3 not found in upstream origin

The woocommerce.json returns 8.9.3 as the latest WC version, but in WC GH repo there is no 8.9.3 branch.

This is happening across other extensions at the moment as well. Ref: https://github.com/woocommerce/automatewoo/issues/1781

@tomalec tomalec mentioned this pull request Jun 12, 2024
19 tasks
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. changelog: tweak Small change, that isn't actually very important.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Uncaught Error: Call to a member function get_cart() on null
2 participants