Durgesh
Durgesh

Reputation: 97

magento 2 rest api product filters

I am working on magento 2 api. I need products based on below filters

  1. store id
  2. by product name search
  3. shorting by name
  4. category id
  5. add limit

I have try with this api but no option available

index.php/rest/V1/categories/{id}/products

Please someone suggest how to archive this.

Thanks

Upvotes: 2

Views: 7005

Answers (2)

KielSoft
KielSoft

Reputation: 433

[
    "filter_groups": [
        {
            "filters": [
                {
                    "field": "type_id",
                    "value": "simple",
                    "condition_type": "eq"
                }
            ]
        },
        {
            "filters": [
                {
                    "field": "category_id",
                    "value": "611",
                    "condition_type": "eq"
                }
            ]
        }
    ],
    "page_size": 100,
    "current_page": 1,
    "sort_orders": [
        {
            "field": "name",
            "direction": "ASC"
        }
    ]
]

Upvotes: -2

Lorenzo S
Lorenzo S

Reputation: 1397

You are looking for the (GET) API /rest/V1/products.

  1. the store ID should be automatically detected by the store, because you can pass the store code in the URL before. If you have a store with code test, the API will start with GET /rest/test/V1/products/[...].
  2. You can use the likecondition type. Ex.: products with "sample" in their name: ?searchCriteria[filter_groups][0][filters][0][field]=name &searchCriteria[filter_groups][0][filters][0][value]=%sample% &searchCriteria[filter_groups][0][filters][0][condition_type]=like
  3. you are looking for the sortOrders. Ex.: searchCriteria[sortOrders][0][field]=name. You can even add the sort direction, for example DESC, with searchCriteria[sortOrders][0][direction]=DESC.
  4. Use the category_id field and the eq condition type. Ex.: if you want products from category 10: searchCriteria[filter_groups][0][filters][0][field]=category_id& searchCriteria[filter_groups][0][filters][0][value]=10& searchCriteria[filter_groups][0][filters][0][condition_type]=eq
  5. use searchCriteria[pageSize]. Ex.: 20 products starting from the 40th, equivalent in SQL to LIMIT 20 OFFSET 40: &searchCriteria[pageSize]=20&searchCriteria[currentPage]=3

Of course you can perform AND and OR operations with filters.

Upvotes: 6

Related Questions