Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
SuggestQtyPlugin.php
Go to the documentation of this file.
1 <?php
6 declare(strict_types=1);
7 
9 
18 
23 {
27  private $getProductSalableQty;
28 
32  private $getSkusByProductIds;
33 
37  private $stockResolver;
38 
42  private $storeManager;
43 
47  private $getStockItemConfiguration;
48 
57  public function __construct(
58  GetProductSalableQtyInterface $getProductSalableQty,
59  GetSkusByProductIdsInterface $getSkusByProductIds,
60  StockResolverInterface $stockResolver,
61  StoreManagerInterface $storeManager,
62  GetStockItemConfigurationInterface $getStockItemConfiguration
63  ) {
64  $this->getProductSalableQty = $getProductSalableQty;
65  $this->getSkusByProductIds = $getSkusByProductIds;
66  $this->stockResolver = $stockResolver;
67  $this->storeManager = $storeManager;
68  $this->getStockItemConfiguration = $getStockItemConfiguration;
69  }
70 
85  public function aroundSuggestQty(
86  StockStateInterface $subject,
87  \Closure $proceed,
88  $productId,
89  $qty,
90  $scopeId = null
91  ): float {
92  try {
93  $skus = $this->getSkusByProductIds->execute([$productId]);
94  $productSku = $skus[$productId];
95 
96  $websiteCode = $this->storeManager->getWebsite()->getCode();
97  $stock = $this->stockResolver->execute(SalesChannelInterface::TYPE_WEBSITE, $websiteCode);
98  $stockId = (int)$stock->getStockId();
99 
100  $stockItemConfiguration = $this->getStockItemConfiguration->execute($productSku, $stockId);
101  $qtyIncrements = $stockItemConfiguration->getQtyIncrements();
102 
103  if ($qty <= 0 || $stockItemConfiguration->isManageStock() === false || $qtyIncrements < 2) {
104  throw new LocalizedException(__('Wrong condition.'));
105  }
106 
107  $minQty = max($stockItemConfiguration->getMinSaleQty(), $qtyIncrements);
108  $divisibleMin = ceil($minQty / $qtyIncrements) * $qtyIncrements;
109  $maxQty = min(
110  $this->getProductSalableQty->execute($productSku, $stockId),
111  $stockItemConfiguration->getMaxSaleQty()
112  );
113  $divisibleMax = floor($maxQty / $qtyIncrements) * $qtyIncrements;
114 
115  if ($qty < $minQty || $qty > $maxQty || $divisibleMin > $divisibleMax) {
116  throw new LocalizedException(__('Wrong condition.'));
117  }
118 
119  $closestDivisibleLeft = floor($qty / $qtyIncrements) * $qtyIncrements;
120  $closestDivisibleRight = $closestDivisibleLeft + $qtyIncrements;
121  $acceptableLeft = min(max($divisibleMin, $closestDivisibleLeft), $divisibleMax);
122  $acceptableRight = max(min($divisibleMax, $closestDivisibleRight), $divisibleMin);
123 
124  return abs($acceptableLeft - $qty) < abs($acceptableRight - $qty) ? $acceptableLeft : $acceptableRight;
125  } catch (LocalizedException $e) {
126  return $qty;
127  }
128  }
129 }
$storeManager
__()
Definition: __.php:13
foreach($websiteCodes as $websiteCode) $skus
__construct(GetProductSalableQtyInterface $getProductSalableQty, GetSkusByProductIdsInterface $getSkusByProductIds, StockResolverInterface $stockResolver, StoreManagerInterface $storeManager, GetStockItemConfigurationInterface $getStockItemConfiguration)
$stock
if(!isset($_GET['website_code'])) $websiteCode
Definition: website.php:11
aroundSuggestQty(StockStateInterface $subject, \Closure $proceed, $productId, $qty, $scopeId=null)