1

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!

4
  • I have check the code and it is working ...Are u sure that you have tried default magento 2.2.X vewrsion
    – Amit Bera
    Dec 19, 2017 at 15:05
  • @AmitBera I have tested in magento 2.2.2 and it is not working for me also. Query for $collection->load() is "SELECT e.*, stock_status_index.stock_status AS is_salable FROM catalog_product_entity AS e INNER JOIN cataloginventory_stock_status AS stock_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)"
    – Sarvagya
    Jan 23, 2018 at 8:06
  • I've the same issue here, did you makes it work?
    – lpFranz
    Apr 6, 2018 at 13:56
  • I switched to using an instance of Magento\Catalog\Api\ProductRepositoryInterface to load each product in the loop, loading it by the entity id of the collection member, rather than accessing the collection members directly - it seems to work now. Accessing the collection members directly seemed to skip over the out of stock products. Apr 10, 2018 at 13:51

1 Answer 1

0

Here magento2 added this. Feel free to change it

<type name="Magento\Catalog\Model\ResourceModel\Product\Collection">
        <plugin name="add_stock_information" type="Magento\CatalogInventory\Model\AddStockStatusToCollection" />
    </type>

You can always disable it in your di.xml using this

plugin disabled: To disable a plugin, set this element to true. The default value is false. Use this property to disable core or third-party plugins in your di.xml file.

https://devdocs.magento.com/guides/v2.0/extension-dev-guide/plugins.html#declaring-a-plugin

Here is the file

vendor/magento/module-catalog-inventory/Model/AddStockStatusToCollection.php.

You can override and remove that.

EDIT

Even better there is a config option in magento 2.2

configuration->catalog->inventory->Display out of stock products

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.