Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
AdaptGetStockStatusPlugin.php
Go to the documentation of this file.
1 <?php
6 declare(strict_types=1);
7 
9 
19 
24 {
28  private $isProductSalable;
29 
33  private $getProductSalableQty;
34 
38  private $getSkusByProductIds;
39 
43  private $storeManager;
44 
48  private $stockResolver;
49 
57  public function __construct(
58  IsProductSalableInterface $isProductSalable,
59  GetProductSalableQtyInterface $getProductSalableQty,
60  GetSkusByProductIdsInterface $getSkusByProductIds,
61  StoreManagerInterface $storeManager,
62  StockResolverInterface $stockResolver
63  ) {
64  $this->isProductSalable = $isProductSalable;
65  $this->getProductSalableQty = $getProductSalableQty;
66  $this->getSkusByProductIds = $getSkusByProductIds;
67  $this->storeManager = $storeManager;
68  $this->stockResolver = $stockResolver;
69  }
70 
79  public function afterGetStockStatus(
80  StockRegistryInterface $subject,
81  StockStatusInterface $stockStatus,
82  $productId,
83  $scopeId = null
85  $websiteCode = null === $scopeId
86  ? $this->storeManager->getWebsite()->getCode()
87  : $this->storeManager->getWebsite($scopeId)->getCode();
88  $stockId = $this->stockResolver->execute(SalesChannelInterface::TYPE_WEBSITE, $websiteCode)->getStockId();
89  $sku = $this->getSkusByProductIds->execute([$productId])[$productId];
90 
91  $status = (int)$this->isProductSalable->execute($sku, $stockId);
92  try {
93  $qty = $this->getProductSalableQty->execute($sku, $stockId);
94  } catch (InputException $e) {
95  $qty = 0;
96  }
97 
98  $stockStatus->setStockStatus($status);
99  $stockStatus->setQty($qty);
100  return $stockStatus;
101  }
102 }
$storeManager
__construct(IsProductSalableInterface $isProductSalable, GetProductSalableQtyInterface $getProductSalableQty, GetSkusByProductIdsInterface $getSkusByProductIds, StoreManagerInterface $storeManager, StockResolverInterface $stockResolver)
$status
Definition: order_status.php:8
if(!isset($_GET['website_code'])) $websiteCode
Definition: website.php:11
afterGetStockStatus(StockRegistryInterface $subject, StockStatusInterface $stockStatus, $productId, $scopeId=null)