/** * @param int $stockId * @return StockInterface * @throws NoSuchEntityException */ public function get($stockId) { $stock = $this->stockFactory->create(); $this->resource->load($stock, $stockId); if (!$stock->getId()) { throw new NoSuchEntityException(__('Stock with id "%1" does not exist.', $stockId)); } return $stock; }
public function testAddStockStatusToCollectionRequireStockItems() { $requireStockItems = true; $productCollection = $this->getMockBuilder('Magento\\Catalog\\Model\\Resource\\Product\\Collection')->disableOriginalConstructor()->setMethods(['hasFlag'])->getMock(); $this->event->expects($this->once())->method('getCollection')->will($this->returnValue($productCollection)); $productCollection->expects($this->once())->method('hasFlag')->with('require_stock_items')->will($this->returnValue($requireStockItems)); $stock = $this->getMockBuilder('Magento\\CatalogInventory\\Model\\Stock')->disableOriginalConstructor()->getMock(); $this->stockFactory->expects($this->once())->method('create')->will($this->returnValue($stock)); $stock->expects($this->once())->method('addItemsToProducts')->with($productCollection)->will($this->returnSelf()); $this->assertEquals($this->model, $this->model->addStockStatusToCollection($this->eventObserver)); }
/** * Add Stock items to product collection * * @param EventObserver $observer * @return $this */ public function addInventoryDataToCollection($observer) { $productCollection = $observer->getEvent()->getProductCollection(); $this->_stockFactory->create()->addItemsToProducts($productCollection); return $this; }
/** * Load some inventory configuration settings * * @return void */ protected function _initConfig() { if (!$this->_isConfig) { $configMap = array('_isConfigManageStock' => \Magento\CatalogInventory\Model\Stock\Item::XML_PATH_MANAGE_STOCK, '_isConfigBackorders' => \Magento\CatalogInventory\Model\Stock\Item::XML_PATH_BACKORDERS, '_configMinQty' => \Magento\CatalogInventory\Model\Stock\Item::XML_PATH_MIN_QTY, '_configNotifyStockQty' => \Magento\CatalogInventory\Model\Stock\Item::XML_PATH_NOTIFY_STOCK_QTY); foreach ($configMap as $field => $const) { $this->{$field} = (int) $this->_scopeConfig->getValue($const, \Magento\Store\Model\ScopeInterface::SCOPE_STORE); } $this->_isConfig = true; $this->_stock = $this->_stockFactory->create(); $this->_configTypeIds = array_keys($this->stockItemService->getIsQtyTypeIds(true)); } }