Listing Products by category

swift
categories
products

#1

In my application Xcode/Swift4 application I’m trying to list products by just their specific categories that are associated with them in my molten store. F or example a I have two categories Dresses and Tops. My query in the application is as follows:

let query = MoltinQuery(offset: nil, limit: nil, sort: nil, filter: nil, include: [.products])

    Moltin.category.list(withQuery: query) { result in
        switch result {
        case .failure(let error):
            print(error)
        case .success(let categories):
            let filteredCategories = categories.filter { !$0.products.isEmpty }
            self.categories = filteredCategories
            self.collectionView.reloadData()
        }
    }
}

}

extension StoreFrontViewController: UICollectionViewDelegate, UICollectionViewDataSource {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return categories.count
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CollectionCell", for: indexPath) as! ProductCollectionCollectionViewCell
    
    let category = categories[indexPath.item]
    
    cell.collectionNameLabel.text = category.name
    
     //if let file = category.files.first {
        //cell.collectionImageView.af_setImage(withURL: file.url)
    //}
    return cell
}

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    let category = categories[indexPath.item]
    
    let controller = ProductListViewController()
    controller.category = category
    
    show(controller, sender: nil)
}

}

extension StoreFrontViewController: UICollectionViewDelegateFlowLayout {
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return collectionView.frame.size
}
}

class ProductCollectionCollectionViewCell: UICollectionViewCell {
let collectionNameLabel: UILabel = {
let l = UILabel()
l.translatesAutoresizingMaskIntoConstraints = false
l.font = UIFont.heuristicaItalic(size: 20)
l.textColor = .darkGray
l.numberOfLines = 0
l.textAlignment = .center
l.backgroundColor = UIColor.init(white: 1.0, alpha: 0.8)
l.layer.cornerRadius = 4
l.layer.masksToBounds = true
return l
}()

let collectionImageView: UIImageView = {
    let i = UIImageView()
    i.translatesAutoresizingMaskIntoConstraints = false
    i.contentMode = .scaleAspectFill
    i.clipsToBounds = true
    return i
}()

override init(frame: CGRect) {
    super.init(frame: frame)
    
    addSubview(collectionImageView)
    collectionImageView.leadingAnchor.constraint(equalTo: leadingAnchor).isActive = true
    collectionImageView.trailingAnchor.constraint(equalTo: trailingAnchor).isActive = true
    collectionImageView.topAnchor.constraint(equalTo: topAnchor).isActive = true
    collectionImageView.bottomAnchor.constraint(equalTo: bottomAnchor).isActive = true
    
    backgroundColor = .white
    
    addSubview(collectionNameLabel)
    
    collectionNameLabel.centerXAnchor.constraint(equalTo: centerXAnchor).isActive = true
    collectionNameLabel.centerYAnchor.constraint(equalTo: centerYAnchor).isActive = true
    collectionNameLabel.leadingAnchor.constraint(equalTo: leadingAnchor, constant: 20).isActive = true
}

required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}

}

When I launch the app and select Tops for example in the Tops view I get not only the Tops but Dress as well. As I stated in my store each of the products is associated with either Tops or Dresses. I should be gettin both.

Is my Query wrong. I just want to get Products by associated with a specific category.

Need Help
Mark


#2

Hi,

can you show me the code for ProductListViewController?

The code above shows you are searching for categories which atleast have one product inside and you list them in a CollectionView. For me everything looks fine here, the question how and where do you create the list of products inside the ProductListViewController? There should be a problem.

Best regards


#3

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