6 declare(strict_types=1);
17 use Magento\InventorySalesApi\Api\Data\ProductSalableResultInterfaceFactory;
19 use Magento\InventorySalesApi\Api\Data\ProductSalabilityErrorInterfaceFactory;
30 private $getStockItemConfiguration;
35 private $getReservationsQuantity;
40 private $getStockItemData;
45 private $configuration;
50 private $mathDivision;
55 private $productSalabilityErrorFactory;
60 private $productSalableResultFactory;
78 MathDivision $mathDivision,
79 ProductSalabilityErrorInterfaceFactory $productSalabilityErrorFactory,
80 ProductSalableResultInterfaceFactory $productSalableResultFactory
82 $this->getStockItemConfiguration = $getStockItemConfiguration;
84 $this->getStockItemData = $getStockItemData;
85 $this->getReservationsQuantity = $getReservationsQuantity;
86 $this->mathDivision = $mathDivision;
87 $this->productSalabilityErrorFactory = $productSalabilityErrorFactory;
88 $this->productSalableResultFactory = $productSalableResultFactory;
97 $stockItemConfiguration = $this->getStockItemConfiguration->execute($sku, $stockId);
99 if ($this->isMinSaleQuantityCheckFailed($stockItemConfiguration, $requestedQty)) {
100 return $this->createErrorResult(
101 'is_correct_qty-min_sale_qty',
103 'The fewest you may purchase is %1',
104 $stockItemConfiguration->getMinSaleQty()
109 if ($this->isMaxSaleQuantityCheckFailed($stockItemConfiguration, $requestedQty)) {
110 return $this->createErrorResult(
111 'is_correct_qty-max_sale_qty',
112 __(
'The requested qty exceeds the maximum qty allowed in shopping cart')
116 if ($this->isQuantityIncrementCheckFailed($stockItemConfiguration, $requestedQty)) {
117 return $this->createErrorResult(
118 'is_correct_qty-qty_increment',
120 'You can buy this product only in quantities of %1 at a time.',
121 $stockItemConfiguration->getQtyIncrements()
126 if ($this->isDecimalQtyCheckFailed($stockItemConfiguration, $requestedQty)) {
127 return $this->createErrorResult(
128 'is_correct_qty-is_qty_decimal',
129 __(
'You cannot use decimal quantity for this product.')
133 return $this->productSalableResultFactory->create([
'errors' => []]);
143 private function createErrorResult(
string $code, Phrase
$message) : ProductSalableResultInterface
146 $this->productSalabilityErrorFactory->create([
151 return $this->productSalableResultFactory->create([
'errors' =>
$errors]);
161 private function isDecimalQtyCheckFailed(
162 StockItemConfigurationInterface $stockItemConfiguration,
165 return (!$stockItemConfiguration->isQtyDecimal() && (floor($requestedQty) !== $requestedQty));
175 private function isMinSaleQuantityCheckFailed(
176 StockItemConfigurationInterface $stockItemConfiguration,
180 if ($stockItemConfiguration->getMinSaleQty() && $requestedQty < $stockItemConfiguration->getMinSaleQty()) {
193 private function isMaxSaleQuantityCheckFailed(
194 StockItemConfigurationInterface $stockItemConfiguration,
198 if ($stockItemConfiguration->getMaxSaleQty() && $requestedQty > $stockItemConfiguration->getMaxSaleQty()) {
211 private function isQuantityIncrementCheckFailed(
212 StockItemConfigurationInterface $stockItemConfiguration,
216 $qtyIncrements = $stockItemConfiguration->getQtyIncrements();
217 if ($qtyIncrements !== (
float)0 && $this->mathDivision->getExactDivision($requestedQty, $qtyIncrements) !== 0) {
__construct(GetStockItemConfigurationInterface $getStockItemConfiguration, StockConfigurationInterface $configuration, GetReservationsQuantityInterface $getReservationsQuantity, GetStockItemDataInterface $getStockItemData, MathDivision $mathDivision, ProductSalabilityErrorInterfaceFactory $productSalabilityErrorFactory, ProductSalableResultInterfaceFactory $productSalableResultFactory)
execute(string $sku, int $stockId, float $requestedQty)