Magento – Magento2 REST API products list and out of stock

apiinventorymagento2out-of-stockrest

In the api call V1/products i get also out of stock products, even though I've set in Stores->configuration->catalog->inventory "Display out of stock products" to No.
there's a way to get rid of this annoing missing feature?

Best Answer

Magento Core team better look and fix this issue is still exist in 2.2.7 and I fixed it with a plugin in my custom module and I also added a bonus for you; this also allows you to filter products by website and store id

  1. Custom/ModuleName/etc/di.xml
<?xml version="1.0"?>
<!--
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="Magento\Catalog\Model\ResourceModel\Product\Collection">
    <plugin name="instockFilter" type="Custom\ModuleName\Model\Plugin\InstockFilter" />
</type>

</config>
  1. configuration = $configuration; $this->stockHelper = $stockHelper; } public function aroundAddFieldToFilter(ProductCollection $subject, \Closure $proceed, $fields, $condition = null) { if ($this->configuration->isShowOutOfStock() != 1) { $this->stockHelper->addInStockFilterToCollection($subject); } if (is_array($fields)) { foreach ($fields as $key => $filter) { if ($filter['attribute'] == 'website_id' && isset($filter['eq'])) { $subject->addWebsiteFilter([$filter['eq']]); unset($fields[$key]); } else if ($filter['attribute'] == 'store_id' && isset($filter['eq'])) { $subject->addStoreFilter($filter['eq']); unset($fields[$key]); } } } return $fields? $proceed($fields, $condition) : $subject; } }`
Related Topic