Get all products by category name or url key

H, how would I get all the products from a specific category using the url key?

Let’s say I have a category named Bags with an url key bags. How do I get all the Bags from the shop?

I’ve found solutions using the Category ID but nothing using the Category name or url key that actually works.

The way this reads, it seems like you just need to add the Bag skus to the Bag category.

Sorry I just read my question again and realised that maybe I forgot an important detail…I would like to retrieve the products programmatically.

I do this via the REST API in Magento 2.3.2. It’s somewhat involved. I follow these steps:

  1. /rest/default/V1/categories?rootCategoryId=1&depth=5
    This call gets all categories starting at the root to a depth level of 5 (your root id may be different)

  2. For each category in the list above, I call /rest/default/V1/categories/:categoryId?storeId=1
    Where the :categoryId is the id for the current iteration. Your storeId may be different. In the results from this call, check the custom_attributes array where attribute_code value is url_key and if the value matches the URL key you want, you can run the final call to get products by category id using the category id of the current item

  3. The final call would look something like this: /rest/default/V1/categories/:categoryId/products?searchCriteria[page_size]=50
    where categoryId is the id you retrieved from step 2

It’s a number of calls, but you can safely cache the results until your next category update, and none of the calls takes long at all

Hope this helps

1 Like