/** * 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); } }
/** * Load collection * * @param bool $printQuery * @param bool $logQuery * @return $this */ public function load($printQuery = false, $logQuery = false) { if ($this->isLoaded()) { return $this; } if ($this->_loadWithProductCount) { $this->addAttributeToSelect('all_children'); $this->addAttributeToSelect('is_anchor'); } parent::load($printQuery, $logQuery); if ($this->_loadWithProductCount) { $this->_loadProductCount(); } return $this; }
/** * Set Order field * * @param string $attribute * @param string $dir * @return $this */ public function setOrder($attribute, $dir = 'desc') { if ($attribute == 'price') { $this->addAttributeToSort($attribute, $dir); } else { parent::setOrder($attribute, $dir); } return $this; }