Listing Product Categories and and products within

swift
categories
products

#1

I want to design a food menu which lists all product categories and their products within, so like chip(category) then large chip, small chip, burger(category) chicken, beef etc. Its for an iOS mobile application.


#2

Hey Shannon,

That sounds really cool. My first thought would be to us Product relationships. Check out the API documentation for creating category relationships (https://docs.moltin.com/?bash#create-category-relationship-s-). I have been working on some swift stuff so I will take a look at this use case now.

-George


#3

Hey Shannon,

Did some more digging and seems this can more easily be done using .include. I set up a collection and a product for this example (see attached).

I then used included on the product level, you could also use it on the category level
Collection with products
self.request = self.moltin.collection.include([.products]).all { (response) in switch response { case .success(let collection): print(collection.data) case .failure(let error): print(error) } }
Product with collection
self.request = self.moltin.product.include([.collections]).all { (response) in switch response { case .success(let products): print(products.data) case .failure(let error): print(error) } }


#4

Hi George,
I really appreciate the help!! I have one query

Can I specify which products to display within each collection dependant on the category also?
An example,

There are two food menus; Breakfast and Lunch, each with their own collections, so chips, burgers etc and within these collections are the relevant products.

So I want to display all food products linked to the Chips collection within the Lunch Category?

Thanks, Shannon


#5

Hey Shannon,

Yes, you can. You just add the category in the include.

        self.request = self.moltin.product.include([.collections, .categories]).all { (response) in
        switch response {
        case .success(let collection):
            print(collection.data)
        case .failure(let error):
            print(error)
        }
    }

-George


#6

HI,

I was discussing previously how to retrieve data from various categories/collections within the molten store. I have successfully implemented this, but am having ongoing issues trying to input this data to a drop down tableview controller. Can you shed light to this, or discuss how you would implement such?

I am able to implement a tableview controller easily, but when attempting to add section headings and getting these to display correctly is where my issue lies.

Shannon


#7

Hey Shannon,

Not sure exactly what you want to use for the drop down filter. Can you share any code you have started. Are you looking to have a table view with headers? Example
Breakfast
items
Lunch
items
Dinner
Items
Or just one category at a time? Found this tutorial this does a good job showing similar table view that you may use (skip right to Coding the Meals Table).

-George


#8

This is my code whilst playing about with sections within a tableview controller. I understand the concept when I create my own arrays and have each array input named section with an expanded false value, but I’m unsure how this would work with molten product arrays. I have attached an image of my code.

Shannon


#9

when I run this code, this is its output. I am happy how this looks, I just need the correct data input.


#10

You can use the sdk to get the categories and the products. You can use an array to use them to populate the tables.

Example
In the top of the class
var categories: [moltin.Category] = []

In the view did load grab the data
self.moltin.category.include([.products]).all { (result: Result<PaginatedResponse<[moltin.Category]>>) in
switch result {
case .success(let response):
self.categories = response.data ?? []
DispatchQueue.main.async {
self.collectionView.reloadData()
}
case .failure(let error):
print(error)
}
}

Then in the tableView
let category = self.categories[indexPath.row]
let products = self.categories[indexPath.row].products

Then for the the label
category.name

And for the products
category.products
or products.name

-George


#11

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.