Automattic\WooCommerce\StoreApi\Utilities

CartController::get_product_for_cart()protectedWC 1.0

Get a product object to be added to the cart.

Method of the class: CartController{}

No Hooks.

Return

\WC_Product|Error. Returns a product object if purchasable.

Usage

// protected - for code of main (parent) or child class
$result = $this->get_product_for_cart( $request );
$request(array) (required)
Add to cart request params.

CartController::get_product_for_cart() code WC 9.3.3

protected function get_product_for_cart( $request ) {
	$product = wc_get_product( $request['id'] );

	if ( ! $product || 'trash' === $product->get_status() ) {
		throw new RouteException(
			'woocommerce_rest_cart_invalid_product',
			__( 'This product cannot be added to the cart.', 'woocommerce' ),
			400
		);
	}

	return $product;
}