1

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?

  • Did you ever found out about this? Stock levels also don't seem to be listed in the data that comes back, so you never quite know what is in and what is out of stock.. – Erfan Jun 21 '18 at 8:17
0

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. Conpany/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. ` namespace Custom\ModuleName\Model\Plugin;

use Magento\Catalog\Model\ResourceModel\Product\Collection as ProductCollection; use Magento\Catalog\Model\Product\Link; use Magento\Catalog\Model\ResourceModel\Product\Link\Product\Collection; use Magento\CatalogInventory\Helper\Stock;

class InstockFilter {

private $configuration;


private $stockHelper;


public function __construct(Configuration $configuration, Stock $stockHelper)
{
    $this->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;
}

}`

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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