1

I am trying to get products data having qty >=5 and I tried following options in Search Criteria

/rest/default/V1/products?searchCriteria[page_size]=20&searchCriteria[filter_groups][0][filters][0][field]=stock_item[qty]&searchCriteria[filter_groups][0][filters][0][value]=5&searchCriteria[filter_groups][0][filters][0][condition_type]=gteq

/rest/default/V1/products?searchCriteria[page_size]=20&searchCriteria[filter_groups][0][filters][0][field]=qty&searchCriteria[filter_groups][0][filters][0][value]=5&searchCriteria[filter_groups][0][filters][0][condition_type]=gteq

/rest/default/V1/products?searchCriteria[page_size]=20&searchCriteria[filter_groups][0][filters][0][field]=extension_attributes[qty]&searchCriteria[filter_groups][0][filters][0][value]=5&searchCriteria[filter_groups][0][filters][0][condition_type]=gteq

/rest/default/V1/products?searchCriteria[page_size]=20&searchCriteria[filter_groups][0][filters][0][field]=extension_attributes[stock_item[qty]]&searchCriteria[filter_groups][0][filters][0][value]=5&searchCriteria[filter_groups][0][filters][0][condition_type]=gteq

/rest/default/V1/products?searchCriteria[page_size]=20&searchCriteria[filter_groups][0][filters][0][field]=extension_attributes[stock_item][qty]&searchCriteria[filter_groups][0][filters][0][value]=5&searchCriteria[filter_groups][0][filters][0][condition_type]=gteq

but none of them is working for me, I have magento version 2.3.3 , having MSI but only 1 source.

and all are giving error as:

The "%1" attribute name is invalid. Reset the name and try again.

3 Answers 3

1

You need to create a custom filter to get product collection by REST API with the QTY filter. Please make the below files in the module.

app/code/Vendor/Module/etc/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="qty" xsi:type="object">Vendor\Module\Model\Api\SearchCriteria\CollectionProcessor\FilterProcessor\ProductQtyFilter</item>
        </argument>
    </arguments>
</virtualType>

app/code/Vendor/Module/Model/Api/SearchCriteria/CollectionProcessor/FilterProcessor/ProductQtyFilter.php

<?php
namespace Vendor\Module\Model\Api\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 ProductQtyFilter implements CustomFilterInterface
{
    /**
     * Apply QTY Filter to Product Collection
     *
     * @param Filter $filter
     * @param AbstractDb $collection
     * @return bool Whether the filter is applied
     */
    public function apply(Filter $filter, AbstractDb $collection)
    {
        $value = $filter->getValue();
        $conditionType = $filter->getConditionType() ?: 'in';

        /** @var Collection $collection */
        $collection->joinField(
            'qty', 'cataloginventory_stock_status', 'qty', 'product_id=entity_id', '{{table}}.stock_id=1', 'left'
        )->addFieldToFilter('qty', array($conditionType => $value));

        return true;
    }
}

Now you can use the REST API with the QTY filter like below.

/rest/default/V1/products?searchCriteria[page_size]=20&searchCriteria[filter_groups][0][filters][0][field]=qty&searchCriteria[filter_groups][0][filters][0][value]=5&searchCriteria[filter_groups][0][filters][0][condition_type]=gteq

I hope this will work for you.

1
  • Just in case somebody tries this and it doesn't work. The di.xml should be probably placed in etc/graphql. Commented Sep 13, 2023 at 10:30
0

There is no such API on Magento default.

You can write your own API that allows product searching according to a certain qty that you want.

0

I think that you will first have to extract the SKUs with the endpoint inventory/source-items and then do another request to get the product data.

/rest/default/V1/inventory/source-items/?searchCriteria[page_size]=20&searchCriteria[filter_groups][0][filters][0][field]=quantity&searchCriteria[filter_groups][0][filters][0][value]=5&searchCriteria[filter_groups][0][filters][0][condition_type]=gteq

Reference from ReDoc: https://magento.redoc.ly/2.3.6-admin/tag/productsproductSkustockItemsitemId#operation/inventoryApiSourceItemRepositoryV1GetListGet

I couldn't find any direct way to retrieve the data. So you ether chain 2 calls and parse the response from the first one to the second one, or you write your own custom REST method.

5
  • I tried that, and given even all API access to API key I generated, still I am always getting message that Not authorized to access this resource. Commented Mar 5, 2021 at 16:39
  • i think this has to do with the resources allocated to the user you are using. how do you make this call? it works well for me if i use a dedicated admin account and make a login request first to retrieve the token, then use it in the bearer token authorization type.
    – Diana
    Commented Mar 8, 2021 at 15:44
  • Sorry for little delayed response, got busy with some other development stuff, but as I mentioned I have assigned all available API resources to the API key I generated, but interesting this is, if go to Swagger and use Admin API key there, it shows this API resource, but not with customer API key having all resource access. Commented Mar 16, 2021 at 16:42
  • Yeah, resources and available endpoints differ. It's one set for admin and another for customer. And it's normal, isn't it? You wouldn't want a customer performing business related operations.
    – Diana
    Commented Mar 16, 2021 at 16:45
  • Check this devdocs.magento.com/guides/v2.4/get-started/authentication/… it says "Before you can make web API calls, you must authenticate your identity and have necessary permissions (authorization) to access the API resource. Authentication allows Magento to identify the caller’s user type. A user’s (administrator, integration, customer, or guest) access rights determine an API call’s resource accessibility."
    – Diana
    Commented Mar 16, 2021 at 16:47

Your Answer

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

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