/** * Add stock status information to products * * @param AbstractCollection $productCollection * @return void */ public function addStockStatusToProducts(AbstractCollection $productCollection) { $websiteId = $this->storeManager->getStore($productCollection->getStoreId())->getWebsiteId(); foreach ($productCollection as $product) { $productId = $product->getId(); $stockStatus = $this->stockRegistryProvider->getStockStatus($productId, $websiteId); $status = $stockStatus->getStockStatus(); $product->setIsSalable($status); } }
/** * @param int $productId * @param int $websiteId * @return \Magento\CatalogInventory\Api\Data\StockStatusInterface */ public function getStockStatus($productId, $websiteId = null) { //if (!$websiteId) { $websiteId = $this->stockConfiguration->getDefaultWebsiteId(); //} return $this->stockRegistryProvider->getStockStatus($productId, $websiteId); }
/** * Add stock status information to products * * @param AbstractCollection $productCollection * @return void */ public function addStockStatusToProducts(AbstractCollection $productCollection) { $websiteId = $this->getStockConfiguration()->getDefaultScopeId(); foreach ($productCollection as $product) { $productId = $product->getId(); $stockStatus = $this->stockRegistryProvider->getStockStatus($productId, $websiteId); $status = $stockStatus->getStockStatus(); $product->setIsSalable($status); } }
public function testGetStockStatus() { $this->stockStatusCriteriaFactory->expects($this->once())->method('create')->willReturn($this->stockStatusCriteria); $this->stockStatusCriteria->expects($this->once())->method('setScopeFilter')->willReturn(null); $this->stockStatusCriteria->expects($this->once())->method('setProductsFilter')->willReturn(null); $stockStatusCollection = $this->getMock('\\Magento\\CatalogInventory\\Model\\ResourceModel\\Stock\\Status\\Collection', ['getFirstItem', '__wakeup', 'getItems'], [], '', false); $stockStatusCollection->expects($this->once())->method('getItems')->willReturn([$this->stockStatus]); $this->stockStatusRepository->expects($this->once())->method('getList')->willReturn($stockStatusCollection); $this->stockStatus->expects($this->once())->method('getProductId')->willReturn($this->productId); $this->assertEquals($this->stockStatus, $this->stockRegistryProvider->getStockStatus($this->productId, $this->scopeId)); }