Automattic\WooCommerce\StoreApi\Utilities
CartController::get_product_for_cart
Get a product object to be added to the cart.
Method of the class: CartController{}
No Hooks.
Returns
\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() CartController::get product for cart code WC 10.8.1
protected function get_product_for_cart( $request ) {
$product = wc_get_product( $request['id'] );
if ( ! $product || ProductStatus::TRASH === $product->get_status() ) {
throw new RouteException(
'woocommerce_rest_cart_invalid_product',
sprintf(
/* translators: %s: product ID */
esc_html__( 'Product with ID "%s" was not found and cannot be added to the cart.', 'woocommerce' ),
esc_html( $request['id'] )
),
400
);
}
return $product;
}