Automattic\WooCommerce\Admin\Features\Fulfillments

FulfillmentsSettings::auto_fulfill_items_on_completedpublicWC 1.0

Automatically fulfill items in the order for orders that skip the processing state.

Method of the class: FulfillmentsSettings{}

No Hooks.

Returns

null. Nothing (null).

Usage

$FulfillmentsSettings = new FulfillmentsSettings();
$FulfillmentsSettings->auto_fulfill_items_on_completed( $order_id, $order ): void;
$order_id(int) (required)
The ID of the order being created.
$order(WC_Order) (required)
The order object.

FulfillmentsSettings::auto_fulfill_items_on_completed() code WC 10.8.1

public function auto_fulfill_items_on_completed( int $order_id, $order ): void {
	$order = $order instanceof WC_Order ? $order : wc_get_order( $order_id );
	if ( ! $order || empty( $order->get_items() ) ) {
		return;
	}

	// If auto-fulfill already processed, skip.
	if ( $order->get_meta( '_auto_fulfill_processed', true ) ) {
		return;
	}

	// If fulfillments already exist, skip auto-fulfillment.
	try {
		/**
		 * Fulfillments data store.
		 *
		 * @var \Automattic\WooCommerce\Admin\Features\Fulfillments\DataStore\FulfillmentsDataStore $fulfillment_data_store
		 */
		$fulfillment_data_store = \WC_Data_Store::load( 'order-fulfillment' );
		$fulfillments           = $fulfillment_data_store->read_fulfillments( \WC_Order::class, (string) $order_id );
		if ( ! empty( $fulfillments ) ) {
			return;
		}
	} catch ( \Throwable $e ) {
		wc_get_logger()->error(
			sprintf( 'Failed to load fulfillments for order %d: %s', $order_id, $e->getMessage() ),
			array( 'source' => 'fulfillments' )
		);
		return;
	}

	// Auto-fulfill items.
	$this->auto_fulfill_items_on_processing( $order_id, $order );
}