Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
AbstractStockqtyPlugin.php
Go to the documentation of this file.
1 <?php
6 declare(strict_types=1);
7 
9 
17 
19 {
23  private $getStockItemConfiguration;
24 
28  private $stockByWebsiteId;
29 
33  private $getProductSalableQty;
34 
38  private $isSourceItemManagementAllowedForProductType;
39 
46  public function __construct(
47  StockByWebsiteIdResolverInterface $stockByWebsiteId,
48  GetStockItemConfigurationInterface $getStockItemConfig,
49  GetProductSalableQtyInterface $getProductSalableQty,
50  IsSourceItemManagementAllowedForProductTypeInterface $isSourceItemManagementAllowedForProductType
51  ) {
52  $this->getStockItemConfiguration = $getStockItemConfig;
53  $this->stockByWebsiteId = $stockByWebsiteId;
54  $this->getProductSalableQty = $getProductSalableQty;
55  $this->isSourceItemManagementAllowedForProductType = $isSourceItemManagementAllowedForProductType;
56  }
57 
65  public function aroundIsMsgVisible(AbstractStockqty $subject, callable $proceed): bool
66  {
67  $productType = $subject->getProduct()->getTypeId();
68  if (!$this->isSourceItemManagementAllowedForProductType->execute($productType)) {
69  return false;
70  }
71 
72  $sku = $subject->getProduct()->getSku();
73  $websiteId = (int)$subject->getProduct()->getStore()->getWebsiteId();
74  $stockId = (int)$this->stockByWebsiteId->execute($websiteId)->getStockId();
75  $stockItemConfig = $this->getStockItemConfiguration->execute($sku, $stockId);
76 
77  return ($stockItemConfig->getBackorders() === StockItemConfigurationInterface::BACKORDERS_NO
78  || $stockItemConfig->getBackorders() !== StockItemConfigurationInterface::BACKORDERS_NO
79  && $stockItemConfig->getMinQty() < 0)
80  && $this->getProductSalableQty->execute($sku, $stockId) <= $stockItemConfig->getStockThresholdQty();
81  }
82 
90  public function aroundGetStockQtyLeft(AbstractStockqty $subject, callable $proceed): float
91  {
92  $sku = $subject->getProduct()->getSku();
93  $websiteId = (int)$subject->getProduct()->getStore()->getWebsiteId();
94  $stockId = (int)$this->stockByWebsiteId->execute($websiteId)->getStockId();
95  return $this->getProductSalableQty->execute($sku, $stockId);
96  }
97 }
__construct(StockByWebsiteIdResolverInterface $stockByWebsiteId, GetStockItemConfigurationInterface $getStockItemConfig, GetProductSalableQtyInterface $getProductSalableQty, IsSourceItemManagementAllowedForProductTypeInterface $isSourceItemManagementAllowedForProductType)