<?php /** * Copyright © Magento, Inc. All rights reserved. * See COPYING.txt for license details. */ declare(strict_types=1); namespace Magento\InventorySales\Model; use Magento\Framework\Exception\LocalizedException; use Magento\InventorySalesApi\Api\IsProductSalableForRequestedQtyInterface; use Magento\InventorySalesApi\Api\Data\ProductSalableResultInterface; use Magento\InventorySalesApi\Api\Data\ProductSalabilityErrorInterface; class CheckItemsQuantity { /** * @var IsProductSalableForRequestedQtyInterface */ private $isProductSalableForRequestedQty; /** * @param IsProductSalableForRequestedQtyInterface $isProductSalableForRequestedQty */ public function __construct( IsProductSalableForRequestedQtyInterface $isProductSalableForRequestedQty ) { $this->isProductSalableForRequestedQty = $isProductSalableForRequestedQty; } /** * Check whether all items salable * * @param array $items [['sku' => 'qty'], ...] * @param int $stockId * @return void * @throws LocalizedException */ public function execute(array $items, int $stockId) : void { foreach ($items as $sku => $qty) { /** @var ProductSalableResultInterface $isSalable */ $isSalable = $this->isProductSalableForRequestedQty->execute((string)$sku, $stockId, (float)$qty); if (false === $isSalable->isSalable()) { $errors = $isSalable->getErrors(); /** @var ProductSalabilityErrorInterface $errorMessage */ $errorMessage = array_pop($errors); throw new LocalizedException(__($errorMessage->getMessage())); } } } }