/** * Add stock status information to products * * @param \Magento\Catalog\Model\Resource\Collection\AbstractCollection $productCollection * @return void * @SuppressWarnings(PHPMD.UnusedLocalVariable) */ public function addStockStatusToProducts(\Magento\Catalog\Model\Resource\Collection\AbstractCollection $productCollection) { $websiteId = $this->storeManager->getStore($productCollection->getStoreId())->getWebsiteId(); $productIds = []; foreach ($productCollection as $product) { $productId = $product->getId(); $stockStatus = $this->stockRegistry->getStockStatus($productId, $websiteId); $status = $stockStatus->getStockStatus(); $product->setIsSalable($status); } }
/** * Retrieve product stock qty * * @param Product $product * @return float */ public function getProductStockQty($product) { return $this->stockRegistry->getStockStatus($product->getId(), $product->getStore()->getWebsiteId())->getQty(); }
public function testGetStockStatusBySku() { $this->assertEquals($this->stockStatus, $this->stockRegistry->getStockStatus($this->productId, $this->websiteId)); }