Automattic\WooCommerce\StoreApi\Routes\V1

CartItems::get_route_post_response()protectedWC 1.0

Creates one item from the collection.

Method of the class: CartItems{}

No Hooks.

Return

\WP_REST_Response.

Usage

// protected - for code of main (parent) or child class
$result = $this->get_route_post_response( $request );
$request(\WP_REST_Request) (required)
Request object.

CartItems::get_route_post_response() code WC 8.7.0

protected function get_route_post_response( \WP_REST_Request $request ) {
	// Do not allow key to be specified during creation.
	if ( ! empty( $request['key'] ) ) {
		throw new RouteException( 'woocommerce_rest_cart_item_exists', __( 'Cannot create an existing cart item.', 'woocommerce' ), 400 );
	}

	$result = $this->cart_controller->add_to_cart(
		[
			'id'        => $request['id'],
			'quantity'  => $request['quantity'],
			'variation' => $request['variation'],
		]
	);

	$response = rest_ensure_response( $this->prepare_item_for_response( $this->cart_controller->get_cart_item( $result ), $request ) );
	$response->set_status( 201 );
	return $response;
}