Automattic\WooCommerce\Internal\StockNotifications\AsyncTasks

JobManager::schedule_initial_job_for_productpublicWC 1.0

Schedule a job.

Method of the class: JobManager{}

Returns

true|false. True if the job was scheduled, false otherwise.

Usage

$JobManager = new JobManager();
$JobManager->schedule_initial_job_for_product( $product_id ): bool;
$product_id(int) (required)
The product ID.

JobManager::schedule_initial_job_for_product() code WC 10.3.6

public function schedule_initial_job_for_product( int $product_id ): bool {
	$args = array( 'product_id' => $product_id );

	try {

		if ( $this->queue->get_next( self::AS_JOB_SEND_STOCK_NOTIFICATIONS, $args, self::AS_JOB_GROUP ) ) {
			return false;
		}

		/**
		 * Filter: woocommerce_customer_stock_notifications_first_batch_delay
		 *
		 * @since 10.2.0
		 *
		 * Schedule the first batch with a delay to prevent overwhelming the system.
		 *
		 * @param int   $delay       Delay time in seconds before first batch.
		 * @param int   $product_id  Product ID being scheduled.
		 */
		$delay = (int) apply_filters( 'woocommerce_customer_stock_notifications_first_batch_delay', MINUTE_IN_SECONDS, $product_id );
		$delay = max( 0, $delay );

		$action_id = $this->queue->schedule_single(
			time() + $delay,
			self::AS_JOB_SEND_STOCK_NOTIFICATIONS,
			$args,
			self::AS_JOB_GROUP
		);

		if ( ! $action_id ) {
			return false;
		}

		$this->logger->info(
			sprintf( 'Scheduled stock notification for product %d', $product_id ),
			array( 'source' => 'wc-customer-stock-notifications' )
		);

		return true;
	} catch ( Exception $e ) {
		$this->logger->error(
			sprintf( 'Failed to schedule stock notification for product %d: %s', $product_id, $e->getMessage() ),
			array( 'source' => 'wc-customer-stock-notifications' )
		);

		return false;
	}
}