Magento 2.2.* seems to make it very difficult to get out of stock products in a collection! I have the following code which works fine in Magento 2.0 and 2.1:
use Magento\Framework\View\Result\PageFactory;
use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory;
class Index extends \Magento\Framework\App\Action\Action
{
public function __construct(
\Magento\Framework\App\Action\Context $context,
PageFactory $resultPageFactory,
\Magento\Framework\Controller\Result\JsonFactory $resultJsonFactory,
\Magento\Store\Model\StoreManagerInterface $storeManager,
CollectionFactory $collectionFactory
) {
parent::__construct($context);
$this->resultJsonFactory = $resultJsonFactory;
$this->resultPageFactory = $resultPageFactory;
$this->_storeManager = $storeManager;
$this->_collectionFactory = $collectionFactory;
}
public function execute()
{
// should get ALL products including out of stock ones
$collection = $this->_collectionFactory->create();
Now testing it on Magento 2.2.1 and 2.2.2, and it is only retrieving products which are in stock - does anyone know a way to work around this?
I have looked at the internal code for collections and can't find any obvious differences - Any info much appreciated as this is a very irritating change that they've made!
e
.*,stock_status_index
.stock_status
ASis_salable
FROMcatalog_product_entity
ASe
INNER JOINcataloginventory_stock_status
ASstock_status_index
ON e.entity_id = stock_status_index.product_id AND stock_status_index.website_id = 0 AND stock_status_index.stock_id = 1 WHERE (stock_status_index.stock_status = 1)"