I want to extend the Catalog Widget to filter Out Of Stock products.
To do this, I have created a component to extend the Catalog Widget's ProductsList class, which contains the following code to replace its createCollection function.
class ProductsList extends \Magento\CatalogWidget\Block\Product\ProductsList
{
public function createCollection(
)
{
/** @var $collection \Magento\Catalog\Model\ResourceModel\Product\Collection */
$collection = $this->productCollectionFactory->create();
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$this->_stockFilter = $objectManager->create('Magento\CatalogInventory\Helper\Stock');
$this->_stockFilter->addInStockFilterToCollection($collection);
... CODE CONTINUES AS PER THE FUNCTION IN app\vendor\magento\module-catalog-widget\Block\Product\ProductsList.php
return $collection;
}
}
This works as I want it to. However, I know we should not use the object manager to load Magento\CatalogInventory\Helper\Stock
I've tried adding a __construct function, but this just causes a "Type Error occurred when creating object" error.
How should I be loading the Stock helper?