Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
AdaptGetProductStockStatusPlugin.php
Go to the documentation of this file.
1 <?php
6 declare(strict_types=1);
7 
9 
16 
21 {
25  private $isProductSalable;
26 
30  private $getSkusByProductIds;
31 
35  private $storeManager;
36 
40  private $stockResolver;
41 
48  public function __construct(
49  IsProductSalableInterface $isProductSalable,
50  GetSkusByProductIdsInterface $getSkusByProductIds,
51  StoreManagerInterface $storeManager,
52  StockResolverInterface $stockResolver
53  ) {
54  $this->isProductSalable = $isProductSalable;
55  $this->getSkusByProductIds = $getSkusByProductIds;
56  $this->storeManager = $storeManager;
57  $this->stockResolver = $stockResolver;
58  }
59 
68  public function aroundGetProductStockStatus(
69  StockRegistryInterface $subject,
70  callable $proceed,
71  $productId,
72  $scopeId = null
73  ): int {
74  $websiteCode = null === $scopeId
75  ? $this->storeManager->getWebsite()->getCode()
76  : $this->storeManager->getWebsite($scopeId)->getCode();
77  $stockId = $this->stockResolver->execute(SalesChannelInterface::TYPE_WEBSITE, $websiteCode)->getStockId();
78  $sku = $this->getSkusByProductIds->execute([$productId])[$productId];
79 
80  return (int)$this->isProductSalable->execute($sku, $stockId);
81  }
82 }
$storeManager
aroundGetProductStockStatus(StockRegistryInterface $subject, callable $proceed, $productId, $scopeId=null)
__construct(IsProductSalableInterface $isProductSalable, GetSkusByProductIdsInterface $getSkusByProductIds, StoreManagerInterface $storeManager, StockResolverInterface $stockResolver)
if(!isset($_GET['website_code'])) $websiteCode
Definition: website.php:11