Automattic\WooCommerce\Admin\Features\Fulfillments

OrderFulfillmentsRestController::create_fulfillmentpublicWC 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.

OrderFulfillmentsRestController::create_fulfillment() code WC 10.7.0

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();
		$fulfillment->set_props( $request->get_json_params() );
		$fulfillment->set_meta_data( $request->get_json_params()['meta_data'] );
		$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( $fulfillment->get_raw_data(), WP_Http::CREATED );
}