How do I filter a specific category of product when running a get products function?


#1

How do I filter a specific category of product when running a get products function?

  private func getProducts() {
            let query = MoltinQuery(offset: nil, limit: nil, sort: nil, filter: nil, include: [.files, .brands, .categories])
            
            Moltin.product.list(withQuery: query) { result in
                switch result {
                case .failure(let error):
                    print(error)
                case .success(let list):
                    self.products = list.products
                    self.tablev?.reloadData()
                    print(self.products)
                }
            }
            
        }

#2

Hi Alex,

Rather than directly listing products, you’ll need to include products when you get information about the category you want to filter on using the following method on Moltin.category:

public func get(withCategoryID id: String, include: [MoltinQuery.Include]? = nil, completion: @escaping (Result<ProductCategory?>) -> ())

For example:

Moltin.category.get(withCategoryID: "12345", include: [.products]) { result in
    ...
}

Hope this helps.