woocommerce / woocommerce-google-analytics-integration

WordPress plugin: Provides the integration between WooCommerce and Google Analytics.
http://wordpress.org/plugins/woocommerce-google-analytics-integration/
170 stars 69 forks source link

Return empty array if WC_Cart is not yet instantiated #435

Closed martynmjones closed 3 months ago

martynmjones commented 3 months ago

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

ianlin commented 3 months ago

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 commented 3 months ago

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