woocommerce / google-listings-and-ads

Sync your store with Google to list products for free, run paid ads, and track performance straight from your store dashboard.
https://woo.com/products/google-listings-and-ads/
GNU General Public License v3.0
44 stars 21 forks source link

Fix fatal error when fetching marketing campaigns for Marketing Channel #2423

Closed jorgemd24 closed 2 months ago

jorgemd24 commented 2 months ago

Changes proposed in this Pull Request:

When loading the Marketing -> Overview section, there is a request to fetch the campaigns, but it currently fails because the campaign types are not loaded. The campaign types are only loaded if the filter woocommerce_gla_enable_mcm returns true.

The fatal error is the below:

undefined array key "google-ads" in /opt/homebrew/var/www/wp-content/plugins/google-listings-and-ads/src/MultichannelMarketing/GLAChannel.php on line 195, referer: https://civet-pleasing-internally.ngrok-free.app/wp-admin/admin.php?page=wc-admin&path=%2Fmarketing
[Thu Jun 06 16:19:22.787147 2024] [php:error] [pid 2819] [client ::1:56339] PHP Fatal error:  Uncaught TypeError: Automattic\\WooCommerce\\Admin\\Marketing\\MarketingCampaign::__construct(): Argument #2 ($type) must be of type Automattic\\WooCommerce\\Admin\\Marketing\\MarketingCampaignType, null given, called in /opt/homebrew/var/www/wp-content/plugins/google-listings-and-ads/src/MultichannelMarketing/GLAChannel.php on line 194 and defined in /opt/homebrew/var/www/wp-content/plugins/woocommerce-dev/plugins/woocommerce/src/Admin/Marketing/MarketingCampaign.php:68\nStack trace:\n#0 /opt/homebrew/var/www/wp-content/plugins/google-listings-and-ads/src/MultichannelMarketing/GLAChannel.php(194): Automattic\\WooCommerce\\Admin\\Marketing\\MarketingCampaign->__construct('21359526875', NULL, 'Campaign 2024-0...', 'https://civet-p...', Object(Automattic\\WooCommerce\\Admin\\Marketing\\Price))\

Screenshots:

image

This PR prevents the error by checking if the campaign types have been enabled before fetching the campaigns.

Detailed test instructions:

  1. Checkout develop.
  2. Go to Marketing -> Overview.
  3. Check your PHP errors or open the console and see how the campaign request fails.
  4. Checkout this branch.
  5. Refresh the Marketing -> Overview page.
  6. See that we prevent the fatal error.
  7. Add the following filter: add_filter( 'woocommerce_gla_enable_mcm', '__return_true' ); 8 Refresh and see that now the campaigns are loaded.

Additional details:

Tests are failing because of this issue: https://github.com/woocommerce/google-listings-and-ads/pull/2415#issuecomment-2149368962 however you can run the tests locally and see if everything is OK

Changelog entry

Fix - Fatal error when loading campaign in the marketing overview section.