0

I want to add a stock filter for the product search GraphQl query. check below my code

schema.graphqls

input ProductAttributeFilterInput @doc(description: "Defines the filters"){
    stock_status: FilterEqualTypeInput
}

di.xml

   <virtualType name="Magento\Catalog\Model\Api\SearchCriteria\CollectionProcessor\ProductFilterProcessor" type="Magento\Eav\Model\Api\SearchCriteria\CollectionProcessor\FilterProcessor">
        <arguments>
            <argument name="customFilters" xsi:type="array">
                <item name="stock_status" xsi:type="object">Custom\Module\Model\Resolver\Products\SearchCriteria\CollectionProcessor\FilterProcessor\CustomStockFilter</item>
            </argument>
        </arguments>
    </virtualType>

CustomStockFilter.php

<?php
namespace Custom\Module\Model\Resolver\Products\SearchCriteria\CollectionProcessor\FilterProcessor;

use Magento\Catalog\Model\ResourceModel\Product\Collection;
use Magento\Framework\Api\Filter;
use Magento\Framework\Api\SearchCriteria\CollectionProcessor\FilterProcessor\CustomFilterInterface;
use Magento\Framework\Data\Collection\AbstractDb;

class CustomStockFilter implements CustomFilterInterface
{

    private $logger;
    private $stock;

    public function __construct(
        \Psr\Log\LoggerInterface $logger,
        \Magento\CatalogInventory\Helper\Stock $stock
    ) {
        $this->logger = $logger;
        $this->stock = $stock;
    }


    /**
     * Apply filter by 'category_id' to product collection.
     *
     * For anchor categories, the products from all children categories will be present in the result.
     *
     * @param Filter $filter
     * @param AbstractDb $collection
     * @return bool Whether the filter is applied
     */
    public function apply(Filter $filter, AbstractDb $collection)
    {
        $this->logger->info('stock filter log');
        $conditionType = $filter->getConditionType();
        $value = $filter->getValue();
        $this->stock->addInStockFilterToCollection($collection);
        
        return true;
    }

}

GraphQL Query

{
  products(
    search: "test"
    filter: { stock_status: { in: ["1"] } }
  ) {
    items {
      uid
    }
  }
}

I added a logger in the resolver file but the resolver is not triggered. my Magento ver. 2.4.4-p1.

how to add in-stock/outstock filter in product search query.

stock_status I added a custom field but its not working

please help me on this

6
  • For now Igonre file trigger or not but just confirm as per your attribute value products data returns or not? Like this image: prnt.sc/Vc6iTdsFpN1M Feb 26 at 12:14
  • no its not return
    – Devidas
    Feb 26 at 12:48
  • Have you created attributes by admin or script? Feb 26 at 12:54
  • by admin added the attributes
    – Devidas
    Feb 27 at 6:07
  • So you need to configure attribute like this: prnt.sc/mw6vZWkXLb11 Feb 27 at 11:33

0

Your Answer

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

Browse other questions tagged or ask your own question.