2

I am asking this, but with some addition.

Getting Products by category id! Magento 2.1 REST

I am getting All Categories with ( I am using Magento 2 sample data )

http://www.myshop.com/rest/V1/categories

In that result for example i took Female Tops category which has id of 21 and ran request

http://www.myshop.com/rest/V1/categories/21/products

this returns me all products (50) with their SKU, But i need all data from products.

Now i tried couple of things to achieve this:

1. Get all products by their SKU which i get from their category

http://www.myshop.com/rest/V1/products? searchCriteria[filter_groups][0][filters][0][field]=sku&searchCriteria[filter_groups][0][filters][0][value]=MH01&searchCriteria[filter_groups][0][filters][1][field]=sku&searchCriteria[filter_groups][0][filters][1][value]=MH02&searchCriteria[filter_groups][0][filters][2][field]=sku&searchCriteria[filter_groups][0][filters][2][value]=MH03 ...

It works, But the problem with this is it might get HTTP Error 414 URL is too long (if there are too many products) and i think is not very efficient.

2. Get all products by their category ID

http://www.myshop.com/rest/V1/products? searchCriteria[filter_groups][0][filters][0][field]=category_id&searchCriteria[filter_groups][0][filters][0][value]=21

This returns me 0 Products and I think I understand why, its because they are not in this category rather they are in child category (i saw that in custom_attributes key category_ids (this is in fact where they "belong")), And also this will return me all products not just bundled as I want.

Is there a better way of getting products by their categories? Without writing new APIs. I just started using magento 2 APIs and maybe i overlooked something.

  • if you have gotten solution kindly post here. i am also facing same issues – Sarvesh Tiwari Apr 22 at 10:35
3

If you want to perform a search by SKU (first case) in a better way, you could use the LIKE condition as in the example in the official guides

GET http://<magento_host>/index.php/rest/V1/products?
searchCriteria[filter_groups][0][filters][0][field]=name&
searchCriteria[filter_groups][0][filters][0][value]=%25Leggings%25&
searchCriteria[filter_groups][0][filters][0][condition_type]=like

To perform a search by category ids you can use the category Interface i.e.

GET /V1/categories/{categoryId}/products
  • But this will return me Products with name like Leggings, I need Products by Category ID or Category Name which i get from myshop.com/rest/V1/categories/21/products . Yes i know but that will return me Products SKU, and i need all product data – MePo Feb 12 '18 at 8:54
  • I'm sorry, I was suggesting how to solve the first point for a too long URL and didn't understand you wanted to search by category name. As far as I know you cannot search directly by category name, you first need to get the id. – sissy Feb 12 '18 at 8:59
  • No problem, i edited question i was at fault. I taught it was somehow possible. i do get the ID, and search by that ID , but the problem is getting all product details as i get only sku – MePo Feb 12 '18 at 9:10
  • Ok, i see now the problem of the call to category interface, but I don't understand the problem of the call http://www.myshop.com/rest/V1/products? searchCriteria[filter_groups][0][filters][0][field]=category_id&searchCriteria[filter_groups][0][filters][0][value]=21. This returns all products data, not only their skus... – sissy Feb 12 '18 at 9:21
  • True, But the problem with that is if you do it on for example TOPS (from example data) it will return 0, while there are 50 items, and also this will return all items (their colors and everything) – MePo Feb 12 '18 at 9:24
2

If you want specificate a search by category_id:

GET http://<magento_host>/index.php/rest/V1/products?searchCriteria[filter_groups][0][filters][0][field]=category_id&searchCriteria[filter_groups][0][filters][0][value]=14&searchCriteria[filter_groups][0][filters][0][condition_type]=eq

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.