Automattic\WooCommerce\Internal\Admin\Orders

ItemQuantityLimits::validate_posted_item_quantities │ public │ WC 11.2.0

Validate the order_item_qty values posted by the admin order items screen.

Item ids that do not belong to the given order are ignored.

Метод класса: ItemQuantityLimits{}

Хуков нет.

Возвращает

void. Ничего (null).

Использование

$ItemQuantityLimits = new ItemQuantityLimits();
$ItemQuantityLimits->validate_posted_item_quantities( $order, $items ): void;
$order(WC_Order) (обязательный)
The order the posted items belong to.
$items(массив) (обязательный)
Posted items, as parsed from the serialized form data (the same shape wc_save_order_items receives).

Список изменений

С версии 11.2.0 Введена.

Код ItemQuantityLimits::validate_posted_item_quantities() WC 11.2.2

public function validate_posted_item_quantities( WC_Order $order, array $items ): void {
	if ( empty( $items['order_item_qty'] ) || ! is_array( $items['order_item_qty'] ) ) {
		return;
	}

	$has_min_filter = has_filter( 'woocommerce_quantity_input_min_admin' );
	$order_items    = null;

	foreach ( $items['order_item_qty'] as $item_id => $posted_qty ) {
		$qty = (float) wc_stock_amount( wp_unslash( $posted_qty ) );

		// Without a filter the minimum is min( 0, stored quantity ), which is
		// never above 0, so a non-negative quantity cannot fail: skip the
		// per-item and product lookups on this hot path.
		if ( $qty >= 0 && ! $has_min_filter ) {
			continue;
		}

		if ( null === $order_items ) {
			$order_items = $order->get_items();
		}

		$item = $order_items[ absint( $item_id ) ] ?? null;

		if ( ! $item instanceof WC_Order_Item_Product ) {
			continue;
		}

		$min = (float) $this->get_quantity_input_min( $item );

		if ( $qty < $min ) {
			// phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped -- below_min_exception() strips and decodes the message for a JS alert.
			throw $this->below_min_exception( $item->get_name(), $min );
		}
	}
}