Automattic\WooCommerce\Blocks\StoreApi\Utilities
CartController::validate_cart_item() public WC 1.0
Validates an existing cart item and returns any errors.
{} Это метод класса: CartController{}
Хуки из метода
Возвращает
Null. Ничего.
Использование
$CartController = new CartController(); $CartController->validate_cart_item( $cart_item );
- $cart_item(массив) (обязательный)
- Cart item array.
Код CartController::validate_cart_item() CartController::validate cart item WC 5.0.0
public function validate_cart_item( $cart_item ) {
$product = $cart_item['data'];
if ( ! $product instanceof \WC_Product ) {
return;
}
if ( ! $product->is_purchasable() ) {
$this->throw_default_product_exception( $product );
}
if ( $product->is_sold_individually() && $cart_item['quantity'] > 1 ) {
throw new RouteException(
'woocommerce_rest_cart_product_sold_individually',
sprintf(
/* translators: %s: product name */
__( 'There are too many "%s" in the cart. Only 1 can be purchased.', 'woocommerce' ),
$product->get_name()
),
400
);
}
if ( ! $product->is_in_stock() ) {
throw new RouteException(
'woocommerce_rest_cart_product_no_stock',
sprintf(
/* translators: %s: product name */
__( '"%s" is out of stock and cannot be purchased.', 'woocommerce' ),
$product->get_name()
),
400
);
}
if ( $product->managing_stock() && ! $product->backorders_allowed() ) {
$qty_remaining = $this->get_remaining_stock_for_product( $product );
$qty_in_cart = $this->get_product_quantity_in_cart( $product );
if ( $qty_remaining < $qty_in_cart ) {
throw new RouteException(
'woocommerce_rest_cart_product_no_stock',
sprintf(
/* translators: 1: quantity in stock, 2: product name */
_n(
'There is only %1$s unit of "%2$s" in stock.',
'There are only %1$s units of "%2$s" in stock.',
$qty_remaining,
'woocommerce'
),
wc_format_stock_quantity_for_display( $qty_remaining, $product ),
$product->get_name()
),
400
);
}
}
/**
* Fire action to validate add to cart. Functions hooking into this should throw an \Exception to prevent
* add to cart from occurring.
*
* @param \WC_Product $product Product object being added to the cart.
* @param array $cart_item Cart item array.
*/
do_action( 'wooocommerce_store_api_validate_cart_item', $product, $cart_item );
}