Automattic\WooCommerce\Internal\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.3.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' );
	// 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();

		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 ) );
		}
	} catch ( ApiException $ex ) {
		return $this->prepare_error_response(
			$ex->getErrorCode(),
			$ex->getMessage(),
			WP_Http::BAD_REQUEST
		);

	} catch ( \Exception $e ) {
		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 );
}