Automattic\WooCommerce\StoreApi\Routes\V1

ShopperListItems::get_route_post_responseprotectedWC 1.0

Add an item to the requested list from cart_item_key or direct product payload fields.

Method of the class: ShopperListItems{}

No Hooks.

Returns

\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.

ShopperListItems::get_route_post_response() code WC 10.9.1

protected function get_route_post_response( \WP_REST_Request $request ) {
	$list = ShopperList::get_by_slug( (string) $request['slug'] );

	if ( ! $list ) {
		throw new RouteException( 'woocommerce_rest_shopper_list_not_found', esc_html__( 'Your saved list isn\'t available right now.', 'woocommerce' ), 404 );
	}

	[ $lookup_id, $variation, $quantity ] = $this->resolve_item_payload( $request );

	try {
		$item = ShopperListItem::from_product( $lookup_id, $variation, $quantity );
	} catch ( \InvalidArgumentException $e ) {
		throw new RouteException( 'woocommerce_rest_shopper_list_invalid_variation', esc_html( $e->getMessage() ), 400 );
	}
	if ( ! $item ) {
		throw new RouteException( 'woocommerce_rest_shopper_list_unknown_product', esc_html__( 'No product exists for the supplied item.', 'woocommerce' ), 404 );
	}

	$list->add_item( $item );
	$list->save();

	$saved = $list->find_item( $item->get_key() ) ?? $item;
	return new \WP_REST_Response( $this->schema->get_item_response( $saved ), 201 );
}