Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
IsCorrectQtyCondition.php
Go to the documentation of this file.
1 <?php
6 declare(strict_types=1);
7 
9 
11 use Magento\Framework\Math\Division as MathDivision;
17 use Magento\InventorySalesApi\Api\Data\ProductSalableResultInterfaceFactory;
19 use Magento\InventorySalesApi\Api\Data\ProductSalabilityErrorInterfaceFactory;
21 
26 {
30  private $getStockItemConfiguration;
31 
35  private $getReservationsQuantity;
36 
40  private $getStockItemData;
41 
45  private $configuration;
46 
50  private $mathDivision;
51 
55  private $productSalabilityErrorFactory;
56 
60  private $productSalableResultFactory;
61 
73  public function __construct(
74  GetStockItemConfigurationInterface $getStockItemConfiguration,
75  StockConfigurationInterface $configuration,
76  GetReservationsQuantityInterface $getReservationsQuantity,
77  GetStockItemDataInterface $getStockItemData,
78  MathDivision $mathDivision,
79  ProductSalabilityErrorInterfaceFactory $productSalabilityErrorFactory,
80  ProductSalableResultInterfaceFactory $productSalableResultFactory
81  ) {
82  $this->getStockItemConfiguration = $getStockItemConfiguration;
83  $this->configuration = $configuration;
84  $this->getStockItemData = $getStockItemData;
85  $this->getReservationsQuantity = $getReservationsQuantity;
86  $this->mathDivision = $mathDivision;
87  $this->productSalabilityErrorFactory = $productSalabilityErrorFactory;
88  $this->productSalableResultFactory = $productSalableResultFactory;
89  }
90 
94  public function execute(string $sku, int $stockId, float $requestedQty): ProductSalableResultInterface
95  {
97  $stockItemConfiguration = $this->getStockItemConfiguration->execute($sku, $stockId);
98 
99  if ($this->isMinSaleQuantityCheckFailed($stockItemConfiguration, $requestedQty)) {
100  return $this->createErrorResult(
101  'is_correct_qty-min_sale_qty',
102  __(
103  'The fewest you may purchase is %1',
104  $stockItemConfiguration->getMinSaleQty()
105  )
106  );
107  }
108 
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')
113  );
114  }
115 
116  if ($this->isQuantityIncrementCheckFailed($stockItemConfiguration, $requestedQty)) {
117  return $this->createErrorResult(
118  'is_correct_qty-qty_increment',
119  __(
120  'You can buy this product only in quantities of %1 at a time.',
121  $stockItemConfiguration->getQtyIncrements()
122  )
123  );
124  }
125 
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.')
130  );
131  }
132 
133  return $this->productSalableResultFactory->create(['errors' => []]);
134  }
135 
143  private function createErrorResult(string $code, Phrase $message) : ProductSalableResultInterface
144  {
145  $errors = [
146  $this->productSalabilityErrorFactory->create([
147  'code' => $code,
148  'message' => $message
149  ])
150  ];
151  return $this->productSalableResultFactory->create(['errors' => $errors]);
152  }
153 
161  private function isDecimalQtyCheckFailed(
162  StockItemConfigurationInterface $stockItemConfiguration,
163  float $requestedQty
164  ): bool {
165  return (!$stockItemConfiguration->isQtyDecimal() && (floor($requestedQty) !== $requestedQty));
166  }
167 
175  private function isMinSaleQuantityCheckFailed(
176  StockItemConfigurationInterface $stockItemConfiguration,
177  float $requestedQty
178  ) : bool {
179  // Minimum Qty Allowed in Shopping Cart
180  if ($stockItemConfiguration->getMinSaleQty() && $requestedQty < $stockItemConfiguration->getMinSaleQty()) {
181  return true;
182  }
183  return false;
184  }
185 
193  private function isMaxSaleQuantityCheckFailed(
194  StockItemConfigurationInterface $stockItemConfiguration,
195  float $requestedQty
196  ) : bool {
197  // Maximum Qty Allowed in Shopping Cart
198  if ($stockItemConfiguration->getMaxSaleQty() && $requestedQty > $stockItemConfiguration->getMaxSaleQty()) {
199  return true;
200  }
201  return false;
202  }
203 
211  private function isQuantityIncrementCheckFailed(
212  StockItemConfigurationInterface $stockItemConfiguration,
213  float $requestedQty
214  ) : bool {
215  // Qty Increments
216  $qtyIncrements = $stockItemConfiguration->getQtyIncrements();
217  if ($qtyIncrements !== (float)0 && $this->mathDivision->getExactDivision($requestedQty, $qtyIncrements) !== 0) {
218  return true;
219  }
220  return false;
221  }
222 }
$configuration
Definition: index.php:33
__construct(GetStockItemConfigurationInterface $getStockItemConfiguration, StockConfigurationInterface $configuration, GetReservationsQuantityInterface $getReservationsQuantity, GetStockItemDataInterface $getStockItemData, MathDivision $mathDivision, ProductSalabilityErrorInterfaceFactory $productSalabilityErrorFactory, ProductSalableResultInterfaceFactory $productSalableResultFactory)
__()
Definition: __.php:13
$message
$errors
Definition: overview.phtml:9
$code
Definition: info.phtml:12
execute(string $sku, int $stockId, float $requestedQty)