Automattic\WooCommerce\Internal\StockNotifications\AsyncTasks

CycleStateService::get_or_initialize_cycle_statepublicWC 1.0

Parse the cycle state for a product.

Method of the class: CycleStateService{}

No Hooks.

Returns

Array.

Usage

$CycleStateService = new CycleStateService();
$CycleStateService->get_or_initialize_cycle_state( $product_id ): array;
$product_id(int) (required)
The product ID.

CycleStateService::get_or_initialize_cycle_state() code WC 10.3.6

public function get_or_initialize_cycle_state( int $product_id ): array {

	if ( $product_id <= 0 ) {
		throw new \Exception( 'Product ID is required.' );
	}

	$default_state = array(
		'cycle_start_time' => time(),
		'product_ids'      => array( $product_id ),
		'total_count'      => 0,
		'skipped_count'    => 0,
		'sent_count'       => 0,
		'failed_count'     => 0,
		'duration'         => 0,
	);

	$cycle_state = $this->get_raw_cycle_state( $product_id );
	if ( empty( $cycle_state ) ) {
		return $default_state;
	}

	if ( array_diff_key( $default_state, $cycle_state ) || empty( $cycle_state['cycle_start_time'] ) || ! is_numeric( $cycle_state['cycle_start_time'] ) ) {
		throw new \Exception( 'Invalid cycle state.' );
	}

	$cycle_state = wp_parse_args( $cycle_state, $default_state );

	return $cycle_state;
}