Automattic\WooCommerce\Internal\Fulfillments

FulfillmentsSettings::auto_fulfill_items_on_processingpublicWC 1.0

Automatically fulfill items in the order on the processing state.

Method of the class: FulfillmentsSettings{}

Returns

null. Nothing (null).

Usage

$FulfillmentsSettings = new FulfillmentsSettings();
$FulfillmentsSettings->auto_fulfill_items_on_processing( $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_processing() code WC 10.3.3

public function auto_fulfill_items_on_processing( int $order_id, $order ): void {
	$order = $order instanceof WC_Order ? $order : wc_get_order( $order_id );

	if ( ! $order || empty( $order->get_items() ) ) {
		return;
	}
	$auto_fulfill_downloadable = 'yes' === get_option( 'auto_fulfill_downloadable', 'yes' );
	$auto_fulfill_virtual      = 'yes' === get_option( 'auto_fulfill_virtual', 'no' );

	/**
	 * Filter to get the list of the item, or variant ID's that should be auto-fulfilled.
	 *
	 * @since 10.1.0
	 *
	 * @param array $auto_fulfill_items List of product or variant ID's to auto-fulfill.
	 * @param \WC_Order $order The order object.
	 *
	 * @return array Filtered list of product or variant ID's to auto-fulfill
	 */
	$auto_fulfill_product_ids = apply_filters( 'woocommerce_fulfillments_auto_fulfill_products', array(), $order );
	$auto_fulfill_items       = array();

	foreach ( $order->get_items() as $item ) {
		/**
		 * Get the product associated with the item.
		 *
		 * @var \WC_Order_Item_Product $item
		 * @var \WC_Product $product
		 */
		$product = $item->get_product();
		if ( ! $product ) {
			continue;
		}

		if ( ( $product->is_downloadable() && $auto_fulfill_downloadable )
			|| ( $product->is_virtual() && $auto_fulfill_virtual )
			|| in_array( $product->get_id(), $auto_fulfill_product_ids, true ) ) {
			$auto_fulfill_items[] = $item;
		}
	}

	if ( ! empty( $auto_fulfill_items ) ) {
		$fulfillment = new Fulfillment();
		$fulfillment->set_entity_type( WC_Order::class );
		$fulfillment->set_entity_id( (string) $order_id );
		$fulfillment->set_status( 'fulfilled' );
		$fulfillment->set_items(
			array_map(
				function ( $item ) {
					return array(
						'item_id' => $item->get_id(),
						'qty'     => $item->get_quantity(),
					);
				},
				$auto_fulfill_items
			)
		);
		$fulfillment->save();
	}

	$order->update_meta_data( '_auto_fulfill_processed', true );
}