Automattic\WooCommerce\Admin\Features\Fulfillments

OrderFulfillmentsRestController::create_fulfillmentpublicWC 10.1.0

Create a new fulfillment with the given data for the order.

Method of the class: OrderFulfillmentsRestController{}

Returns

WP_REST_Response. The created fulfillment, or an error if the request fails.

Usage

$OrderFulfillmentsRestController = new OrderFulfillmentsRestController();
$OrderFulfillmentsRestController->create_fulfillment( $request );
$request(WP_REST_Request) (required)
The request object.

Changelog

Since 10.1.0 Introduced.
Since 10.8.0 Date fields in the response are returned as ISO 8601 UTC with 'Z' suffix.
Since 10.8.0 Meta data from the request is normalized via MetaDataUtil.

OrderFulfillmentsRestController::create_fulfillment() code WC 10.9.3

public function create_fulfillment( WP_REST_Request $request ) {
	$order_id        = (int) $request->get_param( 'order_id' );
	$notify_customer = (bool) $request->get_param( 'notify_customer' );

	$order = wc_get_order( $order_id );

	if ( ! $order ) {
		// If the order does not exist, return an error.
		FulfillmentsTracker::track_fulfillment_validation_error( 'create', 'woocommerce_rest_order_invalid_id', $this->check_request_source( $request ) );
		return $this->prepare_error_response(
			'woocommerce_rest_order_invalid_id',
			esc_html__( 'Invalid order ID.', 'woocommerce' ),
			WP_Http::NOT_FOUND
		);
	}

	// Create a new fulfillment.
	try {
		$fulfillment = new Fulfillment();
		$params      = $request->get_json_params();
		$fulfillment->set_props( $params );
		if ( isset( $params['meta_data'] ) ) {
			$this->apply_request_meta_data( $params['meta_data'], $fulfillment );
		}
		$fulfillment->set_entity_type( WC_Order::class );
		$fulfillment->set_entity_id( "$order_id" );

		$fulfillment->save();

		FulfillmentsTracker::track_fulfillment_creation(
			$this->check_request_source( $request ),
			$fulfillment->get_is_fulfilled() ? 'fulfilled' : 'draft',
			$fulfillment->get_item_count() === (int) $order->get_item_count() ? 'full' : 'partial',
			$fulfillment->get_item_count(),
			(int) $order->get_item_count(),
			$notify_customer
		);

		// Track if tracking information was added with this new fulfillment.
		$this->maybe_track_tracking_added( $fulfillment, $request );

		if ( $fulfillment->get_is_fulfilled() && $notify_customer ) {
			/**
			 * Trigger the fulfillment created notification on creating a fulfilled fulfillment.
			 *
			 * @since 10.1.0
			 */
			do_action( 'woocommerce_fulfillment_created_notification', $order_id, $fulfillment, wc_get_order( $order_id ) );
			FulfillmentsTracker::track_fulfillment_notification_sent( 'fulfillment_created', $fulfillment->get_id(), $order_id );
		}
	} catch ( ApiException $ex ) {
		FulfillmentsTracker::track_fulfillment_validation_error( 'create', $ex->getErrorCode(), $this->check_request_source( $request ) );
		return $this->prepare_error_response(
			$ex->getErrorCode(),
			$ex->getMessage(),
			WP_Http::BAD_REQUEST
		);
	} catch ( \Exception $e ) {
		FulfillmentsTracker::track_fulfillment_validation_error( 'create', (string) $e->getCode(), $this->check_request_source( $request ) );
		return $this->prepare_error_response(
			$e->getCode(),
			$e->getMessage(),
			WP_Http::BAD_REQUEST
		);
	}

	return new WP_REST_Response( $this->prepare_fulfillment_response_data( $fulfillment->get_raw_data() ), WP_Http::CREATED );
}