Automattic\WooCommerce\Internal\StockNotifications\AsyncTasks
NotificationsProcessor::process_batch
Process a batch of notifications.
Method of the class: NotificationsProcessor{}
No Hooks.
Returns
null. Nothing (null).
Usage
$NotificationsProcessor = new NotificationsProcessor(); $NotificationsProcessor->process_batch( $product_id );
- $product_id(int) (required)
- The product ID from AS job args.
NotificationsProcessor::process_batch() NotificationsProcessor::process batch code WC 10.3.6
public function process_batch( $product_id ) {
// Sanity checks.
try {
$product_id = $this->parse_args( $product_id );
$cycle_state = $this->cycle_state_service->get_or_initialize_cycle_state( $product_id );
$product = $this->parse_product( $product_id );
} catch ( \Throwable $e ) {
$product_id = (int) $product_id ?? 0;
$this->logger->error(
sprintf( 'Background process for product %s terminated. Reason: %s', $product_id, $e->getMessage() ),
array(
'source' => 'wc-customer-stock-notifications',
'product_id' => $product_id,
'exception' => get_class( $e ),
)
);
// Clean up the cycle state.
if ( isset( $cycle_state ) ) {
$this->cycle_state_service->complete_cycle( $product_id, $cycle_state );
}
return;
}
$cycle_state['product_ids'] = $this->eligibility_service->get_target_product_ids( $product );
// Get notifications.
$notifications = NotificationQuery::get_notifications(
array(
'status' => NotificationStatus::ACTIVE,
'product_id' => $cycle_state['product_ids'],
'last_attempt_limit' => (int) $cycle_state['cycle_start_time'],
'return' => 'ids',
'limit' => $this->get_batch_size(),
'orderby' => 'id',
'order' => 'ASC',
)
);
if ( empty( $notifications ) ) {
$this->cycle_state_service->complete_cycle( $product_id, $cycle_state );
return;
}
foreach ( $notifications as $notification_id ) {
$notification = Factory::get_notification( $notification_id );
if ( ! $notification instanceof Notification ) {
$this->logger->error(
sprintf( 'Failed to get notification ID: %d', $notification_id ),
array( 'source' => 'wc-customer-stock-notifications' )
);
continue;
}
$notification->set_date_last_attempt( time() );
++$cycle_state['total_count'];
if ( $this->eligibility_service->should_skip_notification( $notification, $product ) ) {
++$cycle_state['skipped_count'];
$notification->save();
continue;
}
$is_sent = true;
try {
$this->email_manager->send_stock_notification_email( $notification );
} catch ( \Throwable $e ) {
$is_sent = false;
}
if ( $is_sent ) {
$notification->set_date_notified( time() );
$notification->set_status( NotificationStatus::SENT );
++$cycle_state['sent_count'];
} else {
$notification->set_status( NotificationStatus::CANCELLED );
$notification->set_cancellation_source( NotificationCancellationSource::SYSTEM );
++$cycle_state['failed_count'];
}
// Always save the notification to reflect last attempt time.
$notification->save();
}
if ( count( $notifications ) === $this->get_batch_size() ) {
$this->cycle_state_service->save_cycle_state( $product_id, $cycle_state );
$this->job_manager->schedule_next_batch_for_product( $product_id );
return;
}
$this->cycle_state_service->complete_cycle( $product_id, $cycle_state );
}