1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
<?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()));
}
}
}
}