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

Use WC built-in Action Scheduler to fetch in-app promotions #45628

Merged
merged 17 commits into from Mar 20, 2024

Conversation

corsonr
Copy link
Member

@corsonr corsonr commented Mar 15, 2024

Submission Review Guidelines:

Changes proposed in this Pull Request:

In #44655 and related PRs, we introduced logic to fetch data from a new Woo.com endpoint and show notices on the WooCommerce Marketplace pages, as well as a bubble on the WooCommerce > Extensions menu item. That logic, based on events scheduled with wp_schedule_event, didn't run on JN and other external sites, so the notices weren't shown. In this PR, we are using ActionScheduler to schedule the event with as_schedule_recurring_action.

Closes 19763-gh-Automattic/woocommerce.com.

How to test the changes in this Pull Request:

If you're using a local wp-env environment

  1. Check out this branch locally.
  2. Edit plugins/woocommerce/includes/admin/class-wc-admin-marketplace-promotions.php and replace the PROMOTIONS_API_URL value https://woo.com/wp-json/wccom-extensions/3.0/promotions with https://gist.githubusercontent.com/andfinally/acd8646151c0108f92979c985c20a0d8/raw/. This is a gist with dummy data that is valid until 31 March.
  3. On line 21, change the frequency of the scheduled action to five minutes:
const SCHEDULED_ACTION_INTERVAL = 5 * MINUTE_IN_SECONDS;
  1. Run nvm use.
  2. Do cd plugins/woocommerce.
  3. Run pnpm -- wp-env start to start wp-env.
  4. run pnpm --filter='@woocommerce/plugin-woocommerce' build.
  5. Visit the pending scheduled actions page and search for woocommerce_marketplace_fetch_promotions. Note that the action is pending.
  6. After 5 minutes or so, the action should run. (You may need to jog it by viewing a page on your test site.)
  7. The menu bubble should appear on the WooCommerce > Extensions menu item, and the notice should appear on the default Marketplace page http://localhost:8888/wp-admin/admin.php?page=wc-admin&path=%2Fextensions. (It also appears on the Themes and other tabs on that page – that's intentional.)

image

  1. Edit class-wc-admin-marketplace-promotions.php again and change the PROMOTIONS_API_URL to https://gist.githubusercontent.com/andfinally/8ffca3fe3844fee6e146a80e563f00bb/raw/. This is a second gist, where the expiry dates for the promotions have passed.
  2. Wait for the scheduled action to run again.
  3. The notice and menu bubble should not be shown.

If you're using a JN or other external site

  1. Do the steps 1-5 as for the local environment.
  2. Comment out this line in plugins/woocommerce/bin/build-zip.sh. This skips a memory-intensive process in the WooCommerce zip build that is irrelevant to this change.
pnpm --filter=@woocommerce/plugin-woocommerce makepot || exit "$?"
  1. Run the zip build: pnpm --filter='@woocommerce/plugin-woocommerce' build:zip.
  2. When the build is complete, you should see the resulting woocommerce.zip plugin file in plugins/woocommerce. Install the plugin in your external site.
  3. The menu bubble should appear on the WooCommerce > Extensions menu item, and the notice should appear on the wp-admin/admin.php?page=wc-admin&path=%2Fextensions page.
  4. Change the PROMOTIONS_API_URL to https://gist.githubusercontent.com/andfinally/8ffca3fe3844fee6e146a80e563f00bb/raw/. This is a second gist, where the expiry dates for the promotions have passed. If you have SSH access to your site, you can make this change directly in the PHP file.
  5. Otherwise, build the zip again as in step 3, deactivate and delete the WooCommerce plugin on your site, and install the new zip.
  6. Wait for the scheduled action to run. The notice and menu item badge should no longer appear.

Changelog entry

  • Automatically create a changelog entry from the details below.

Significance

  • Patch
  • Minor
  • Major

Type

  • Fix - Fixes an existing bug
  • Add - Adds functionality
  • Update - Update existing functionality
  • Dev - Development related task
  • Tweak - A minor adjustment to the codebase
  • Performance - Address performance issues
  • Enhancement - Improvement to existing functionality

Message

Comment

@corsonr corsonr requested a review from andfinally March 15, 2024 15:00
@corsonr corsonr self-assigned this Mar 15, 2024
@github-actions github-actions bot added the plugin: woocommerce Issues related to the WooCommerce Core plugin. label Mar 15, 2024
Copy link
Contributor

github-actions bot commented Mar 15, 2024

Hi @Dan-Q, @andfinally,

Apart from reviewing the code changes, please make sure to review the testing instructions as well.

You can follow this guide to find out what good testing instructions should look like:
https://github.com/woocommerce/woocommerce/wiki/Writing-high-quality-testing-instructions

Copy link
Contributor

github-actions bot commented Mar 15, 2024

Test Results Summary

Commit SHA: 73f1371

Test 🧪Passed ✅Failed 🚨Broken 🚧Skipped ⏭️Unknown ❔Total 📊Duration ⏱️
API Tests25900202610m 38s
E2E Tests680028803568m 46s

To view the full API test report, click here.
To view the full E2E test report, click here.
To view all test reports, visit the WooCommerce Test Reports Dashboard.

@andfinally andfinally force-pushed the fix/19763-marketplace-promotions branch from 3e6b651 to 28b9af4 Compare March 18, 2024 12:28
…Admin` constructor. `WC_Admin` is only instantiated in an admin request. If we init marketplace promotions from `woocommerce_init`, we'll be too late to add the callback for the scheduled action that fetches promotion data.
@andfinally andfinally force-pushed the fix/19763-marketplace-promotions branch from 28b9af4 to ac3380f Compare March 18, 2024 12:57
@andfinally andfinally marked this pull request as ready for review March 19, 2024 12:31
@andfinally andfinally requested review from a team and Dan-Q and removed request for a team March 19, 2024 12:36
@andfinally andfinally added the needs: developer feedback Issues that need feedback from one of the WooCommerce Core developers. label Mar 19, 2024
Copy link
Collaborator

@mcliwanow mcliwanow left a comment

Choose a reason for hiding this comment

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

Everything works as expected. Action is triggered when needed, the specified endpoint is called and both bubble and notice are visible.

corsonr and others added 2 commits March 19, 2024 17:45
… from the `WC_Admin` constructor. But `WC_Admin` is only instantiated during `is_admin` requests. We also need to respond to cron requests. So we're now including the class from `class-woocommerce.php` if the context is admin or cron, and instantiating it on `init`. This fixes the error in Scheduled Action `action failed via WP Cron: Scheduled action for woocommerce_marketplace_fetch_promotions will not be executed as no callbacks are registered.`
@andfinally
Copy link
Contributor

andfinally commented Mar 19, 2024

Thanks for the review Michal! 🙌

I've committed a small change to ensure the scheduled action works when run by cron.

  • We were including and instantiating WC_Admin_Marketplace_Promotions in the WC_Admin constructor.
  • But WC_Admin is only instantiated during is_admin requests. We also need to respond to cron requests.
  • So we're now including the class from class-woocommerce.php if the context is admin or cron, and instantiating it on init.
  • This fixes this error in the Scheduled Actions:

action failed via WP Cron: Scheduled action for woocommerce_marketplace_fetch_promotions will not be executed as no callbacks are registered.

Copy link
Contributor

@andfinally andfinally left a comment

Choose a reason for hiding this comment

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

Looks good! 👍 Tested on a new JN site and things worked as expected. Also tested that a woocommerce_marketplace_suppress_promotions filter cancels the scheduled action.

@corsonr corsonr merged commit 7c7ed0d into trunk Mar 20, 2024
35 checks passed
@corsonr corsonr deleted the fix/19763-marketplace-promotions branch March 20, 2024 14:00
@github-actions github-actions bot added this to the 8.8.0 milestone Mar 20, 2024
@github-actions github-actions bot added the needs: analysis Indicates if the PR requires a PR testing scrub session. label Mar 20, 2024
@nigeljamesstevenson nigeljamesstevenson added needs: external testing Indicates if the PR requires further testing conducted by testers external to the development team. needs: internal testing Indicates if the PR requires further testing conducted by Solaris and removed needs: analysis Indicates if the PR requires a PR testing scrub session. needs: external testing Indicates if the PR requires further testing conducted by testers external to the development team. labels Mar 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs: developer feedback Issues that need feedback from one of the WooCommerce Core developers. needs: internal testing Indicates if the PR requires further testing conducted by Solaris plugin: woocommerce Issues related to the WooCommerce Core plugin.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants