Automattic\WooCommerce\Internal\ProductFeed\Integrations\POSCatalog

AsyncGenerator::feed_generation_actionpublicWC 10.5.0

Action scheduler callback for the feed generation.

Method of the class: AsyncGenerator{}

No Hooks.

Returns

null. Nothing (null).

Usage

$AsyncGenerator = new AsyncGenerator();
$AsyncGenerator->feed_generation_action( $option_key );
$option_key(string) (required)
The option key for the feed generation status.

Changelog

Since 10.5.0 Introduced.

AsyncGenerator::feed_generation_action() code WC 10.8.1

public function feed_generation_action( string $option_key ) {
	$status = get_option( $option_key );

	if ( ! is_array( $status ) || ! isset( $status['state'] ) || self::STATE_SCHEDULED !== $status['state'] ) {
		wc_get_logger()->error( 'Invalid feed generation status', array( 'status' => $status ) );
		return;
	}

	$status['state'] = self::STATE_IN_PROGRESS;
	update_option( $option_key, $status );

	try {
		$feed   = $this->integration->create_feed();
		$walker = ProductWalker::from_integration( $this->integration, $feed );

		// Add dynamic args to the mapper.
		$args = $status['args'] ?? array();
		if (
			isset( $args['_product_fields'] )
			&& is_string( $args['_product_fields'] ) &&
			! empty( $args['_product_fields'] )
		) {
			$this->integration->get_product_mapper()->set_fields( $args['_product_fields'] );
		}
		if (
			isset( $args['_variation_fields'] )
			&& is_string( $args['_variation_fields'] ) &&
			! empty( $args['_variation_fields'] )
		) {
			$this->integration->get_product_mapper()->set_variation_fields( $args['_variation_fields'] );
		}

		$walker->walk(
			function ( WalkerProgress $progress ) use ( &$status, $option_key ) {
				$status = $this->update_feed_progress( $status, $progress );
				update_option( $option_key, $status );
			}
		);

		// Store the final details.
		$status['state']        = self::STATE_COMPLETED;
		$status['url']          = $feed->get_file_url();
		$status['path']         = $feed->get_file_path();
		$status['completed_at'] = time();
		update_option( $option_key, $status );

		// Schedule another action to delete the file after the expiry time.
		as_schedule_single_action(
			time() + self::FEED_EXPIRY,
			self::FEED_DELETION_ACTION,
			array(
				$option_key,
				$feed->get_file_path(),
			),
			'woo-product-feed',
			false
		);
	} catch ( \Throwable $e ) {
		wc_get_logger()->error(
			'Feed generation failed',
			array(
				'error'      => $e->getMessage(),
				'option_key' => $option_key,
			)
		);

		$status['state']     = self::STATE_FAILED;
		$status['error']     = $e->getMessage();
		$status['failed_at'] = time();
		update_option( $option_key, $status );
	}
}