fix: run product sync in background batches to avoid 502 on large catalogs

Manual and scheduled sync previously processed every WooCommerce product
inline in a single PHP request/AJAX call, with several sequential BC API
calls per product. At real catalog sizes this exceeds PHP's
max_execution_time and the proxy's read timeout, surfacing as a 502 Bad
Gateway on "Sync Now".

Adds WBC_Batch_Sync, which queues the catalog in chunks of 25 via Action
Scheduler (bundled with WooCommerce) and processes them asynchronously.
The manual sync AJAX call now returns immediately after queuing, and the
admin UI polls for progress instead of waiting on one long request.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
2026-07-09 13:19:11 +02:00
parent 0aff5cf6d8
commit 5ac5c626f3
7 changed files with 353 additions and 107 deletions

View File

@@ -262,6 +262,11 @@ class WBC_Admin {
/**
* AJAX: Manual sync
*
* Queues background batches and returns immediately - the full sync no
* longer runs inline in this request (that approach hits PHP/proxy
* timeouts at real catalog sizes and surfaces as a 502 Bad Gateway).
* Poll ajax_sync_status() for progress.
*/
public function ajax_manual_sync() {
check_ajax_referer( 'wbc_admin_nonce', 'nonce' );
@@ -270,7 +275,7 @@ class WBC_Admin {
wp_send_json_error( array( 'message' => __( 'Permission denied.', 'woo-business-central' ) ) );
}
$result = WBC_Cron::run_sync_now();
$result = WBC_Batch_Sync::queue_full_sync();
if ( isset( $result['success'] ) && $result['success'] ) {
wp_send_json_success( $result );
@@ -279,6 +284,19 @@ class WBC_Admin {
}
}
/**
* AJAX: Get background sync progress
*/
public function ajax_sync_status() {
check_ajax_referer( 'wbc_admin_nonce', 'nonce' );
if ( ! current_user_can( 'manage_woocommerce' ) ) {
wp_send_json_error( array( 'message' => __( 'Permission denied.', 'woo-business-central' ) ) );
}
wp_send_json_success( WBC_Batch_Sync::get_status() );
}
/**
* AJAX: Clear logs
*/