Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
StockRegistryProvider.php
Go to the documentation of this file.
1 <?php
7 
12 use Magento\CatalogInventory\Api\Data\StockInterfaceFactory;
13 use Magento\CatalogInventory\Api\Data\StockItemInterfaceFactory;
14 use Magento\CatalogInventory\Api\Data\StockStatusInterfaceFactory;
15 use Magento\CatalogInventory\Api\StockCriteriaInterfaceFactory;
16 use Magento\CatalogInventory\Api\StockItemCriteriaInterfaceFactory;
17 use Magento\CatalogInventory\Api\StockStatusCriteriaInterfaceFactory;
19 
25 {
29  protected $stockRepository;
30 
34  protected $stockFactory;
35 
40 
44  protected $stockItemFactory;
45 
50 
55 
60 
65 
70 
75 
88  public function __construct(
90  StockInterfaceFactory $stockFactory,
92  StockItemInterfaceFactory $stockItemFactory,
94  StockStatusInterfaceFactory $stockStatusFactory,
95  StockCriteriaInterfaceFactory $stockCriteriaFactory,
96  StockItemCriteriaInterfaceFactory $stockItemCriteriaFactory,
97  StockStatusCriteriaInterfaceFactory $stockStatusCriteriaFactory
98  ) {
99  $this->stockRepository = $stockRepository;
100  $this->stockFactory = $stockFactory;
101  $this->stockItemRepository = $stockItemRepository;
102  $this->stockItemFactory = $stockItemFactory;
103  $this->stockStatusRepository = $stockStatusRepository;
104  $this->stockStatusFactory = $stockStatusFactory;
105  $this->stockCriteriaFactory = $stockCriteriaFactory;
106  $this->stockItemCriteriaFactory = $stockItemCriteriaFactory;
107  $this->stockStatusCriteriaFactory = $stockStatusCriteriaFactory;
108  }
109 
114  public function getStock($scopeId)
115  {
116  $stock = $this->getStockRegistryStorage()->getStock($scopeId);
117  if (null === $stock) {
118  $criteria = $this->stockCriteriaFactory->create();
119  $criteria->setScopeFilter($scopeId);
120  $collection = $this->stockRepository->getList($criteria);
121  $stock = current($collection->getItems());
122  if ($stock && $stock->getStockId()) {
123  $this->getStockRegistryStorage()->setStock($scopeId, $stock);
124  } else {
125  $stock = $this->stockFactory->create();
126  }
127  }
128  return $stock;
129  }
130 
136  public function getStockItem($productId, $scopeId)
137  {
138  $stockItem = $this->getStockRegistryStorage()->getStockItem($productId, $scopeId);
139  if (null === $stockItem) {
140  $criteria = $this->stockItemCriteriaFactory->create();
141  $criteria->setProductsFilter($productId);
142  $collection = $this->stockItemRepository->getList($criteria);
143  $stockItem = current($collection->getItems());
144  if ($stockItem && $stockItem->getItemId()) {
145  $this->getStockRegistryStorage()->setStockItem($productId, $scopeId, $stockItem);
146  } else {
147  $stockItem = $this->stockItemFactory->create();
148  }
149  }
150  return $stockItem;
151  }
152 
158  public function getStockStatus($productId, $scopeId)
159  {
160  $stockStatus = $this->getStockRegistryStorage()->getStockStatus($productId, $scopeId);
161  if (null === $stockStatus) {
162  $criteria = $this->stockStatusCriteriaFactory->create();
163  $criteria->setProductsFilter($productId);
164  $criteria->setScopeFilter($scopeId);
165  $collection = $this->stockStatusRepository->getList($criteria);
166  $stockStatus = current($collection->getItems());
167  if ($stockStatus && $stockStatus->getProductId()) {
168  $this->getStockRegistryStorage()->setStockStatus($productId, $scopeId, $stockStatus);
169  } else {
170  $stockStatus = $this->stockStatusFactory->create();
171  }
172  }
173  return $stockStatus;
174  }
175 
179  private function getStockRegistryStorage()
180  {
181  if (null === $this->stockRegistryStorage) {
182  $this->stockRegistryStorage = \Magento\Framework\App\ObjectManager::getInstance()
183  ->get(\Magento\CatalogInventory\Model\StockRegistryStorage::class);
184  }
186  }
187 }
$stock
__construct(StockRepositoryInterface $stockRepository, StockInterfaceFactory $stockFactory, StockItemRepositoryInterface $stockItemRepository, StockItemInterfaceFactory $stockItemFactory, StockStatusRepositoryInterface $stockStatusRepository, StockStatusInterfaceFactory $stockStatusFactory, StockCriteriaInterfaceFactory $stockCriteriaFactory, StockItemCriteriaInterfaceFactory $stockItemCriteriaFactory, StockStatusCriteriaInterfaceFactory $stockStatusCriteriaFactory)