You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
feat(export): run pricelist export in the background via Action Scheduler
The full per-user pricelist is far too large to build in one request, so the cron
job and the "Generate and email now" button were timing out. Move both to Action
Scheduler (bundled with WooCommerce): capture the product list once, then process
users in bounded batches, emailing the assembled CSV after the last batch.
- PricelistExporter: split out email_file(); add batch primitives product_refs(),
user_refs_page(), init_csv(), append_rows(). Pass the explicit fputcsv escape arg
(silences the PHP 8.4 deprecation; output unchanged).
- ExportModule: start_background() sizes user batches to ~wc_pricebook_export_batch_rows
(default 5,000) rows, writes the header, and queues the first batch; run_batch()
processes a page and re-queues, or emails + cleans up when users run out. Single active
run (guarded); stale/failed runs are reset. Synchronous fallback when Action Scheduler
is unavailable. run_cron() and the Send-now button now start a background run; new
"started"/"running" admin notices. CLI gains --async.
- Plugin: on_deactivation clears queued batches + run state.
- Docs/README: document background processing, --async, and wc_pricebook_export_batch_rows.
- Tests: init_csv + append_rows round-trip. 120 passing.
Verified end-to-end on a live store: batches processed via `wp action-scheduler run`,
final batch emailed and cleaned up, no wp_mail failure logged.
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
'started' => array( 'success', __( 'Pricelist export started. It runs in the background — you’ll get an email with the CSV when it finishes.', 'wc-pricebook' ) ),
401
+
'running' => array( 'warning', __( 'A pricelist export is already running. You’ll get the email when it finishes.', 'wc-pricebook' ) ),
'mailfail' => array( 'error', __( 'The pricelist CSV was generated but the email could not be sent. Check the site’s mail configuration.', 'wc-pricebook' ) ),
201
404
'norecipient' => array( 'error', __( 'No recipient email is set. Enter one in the Pricelist export settings.', 'wc-pricebook' ) ),
@@ -229,11 +432,16 @@ public function maybe_notice() {
229
432
* : Email the CSV to the configured recipient (or the site admin) instead of / in
230
433
* addition to writing a file.
231
434
*
435
+
* [--async]
436
+
* : Queue the export via Action Scheduler (background batches) and email it when done,
437
+
* instead of building it inline. Uses --email or the configured recipient.
\WP_CLI::success( sprintf( 'Queued the background pricelist export to %s. Run the Action Scheduler queue (e.g. `wp action-scheduler run`) to process it.', $recipient ) );
466
+
} elseif ( 'running' === $status ) {
467
+
\WP_CLI::warning( 'A background pricelist export is already running.' );
468
+
} elseif ( 'norecipient' === $status ) {
469
+
\WP_CLI::error( 'No valid recipient. Pass --email=<address> or configure a recipient in Pricebook settings.' );
470
+
} else {
471
+
\WP_CLI::error( 'Could not start the background export. Check the log.' );
472
+
}
473
+
return;
474
+
}
475
+
252
476
try {
253
477
// Emailing: use the recipient flag, else the configured/admin recipient.
0 commit comments