Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
AdaptGetStockStatusBySkuPlugin.php
Go to the documentation of this file.
1 <?php
6 declare(strict_types=1);
7 
9 
18 
23 {
27  private $isProductSalable;
28 
32  private $getProductSalableQty;
33 
37  private $storeManager;
38 
42  private $stockResolver;
43 
50  public function __construct(
51  IsProductSalableInterface $isProductSalable,
52  GetProductSalableQtyInterface $getProductSalableQty,
53  StoreManagerInterface $storeManager,
54  StockResolverInterface $stockResolver
55  ) {
56  $this->isProductSalable = $isProductSalable;
57  $this->getProductSalableQty = $getProductSalableQty;
58  $this->storeManager = $storeManager;
59  $this->stockResolver = $stockResolver;
60  }
61 
70  public function afterGetStockStatusBySku(
71  StockRegistryInterface $subject,
72  StockStatusInterface $stockStatus,
73  $productSku,
74  $scopeId = null
76  $websiteCode = null === $scopeId
77  ? $this->storeManager->getWebsite()->getCode()
78  : $this->storeManager->getWebsite($scopeId)->getCode();
79  $stockId = $this->stockResolver->execute(SalesChannelInterface::TYPE_WEBSITE, $websiteCode)->getStockId();
80 
81  $status = (int)$this->isProductSalable->execute($productSku, $stockId);
82  try {
83  $qty = $this->getProductSalableQty->execute($productSku, $stockId);
84  } catch (InputException $e) {
85  $qty = 0;
86  }
87 
88  $stockStatus->setStockStatus($status);
89  $stockStatus->setQty($qty);
90  return $stockStatus;
91  }
92 }
$storeManager
$status
Definition: order_status.php:8
__construct(IsProductSalableInterface $isProductSalable, GetProductSalableQtyInterface $getProductSalableQty, StoreManagerInterface $storeManager, StockResolverInterface $stockResolver)
afterGetStockStatusBySku(StockRegistryInterface $subject, StockStatusInterface $stockStatus, $productSku, $scopeId=null)
if(!isset($_GET['website_code'])) $websiteCode
Definition: website.php:11