Dedicated to the Hydrogen framework, headless commerce, and building custom storefronts using the Storefront API.
Hello. I've added a metaField to my Shopify Store that store the location of the Products and Collections. I can fetch the value added to the metafield from my Model class. What I'm trying to do is to filter the products fetched based on the value added to the metaField. I'm not able to find a way to do that.
Below is the ProductModel that I'm using where I'm accessing the metaField value. I'm able to access the value of the metaField this way:
import Foundation import Buy final class ProductModel: Model { typealias ModelType = Storefront.Product let model: ModelType let id: String let title: String let summary: String let price: String let images: PageableArray<ImageModel> let variants: PageableArray<VariantModel> let storeLocation: String? // ---------------------------------- // MARK: - Init - // required init(from model: ModelType) { self.model = model let variants = model.variants.nodes.models.sorted { $0.price < $1.price } let lowestPrice = variants.first?.price self.id = model.id.rawValue self.title = model.title self.summary = model.descriptionHtml self.price = lowestPrice == nil ? "No price" : Currency.stringFrom(lowestPrice!) self.images = PageableArray( with: model.images.nodes, pageInfo: PageMetaData(pageInfo: model.images.pageInfo) ) self.variants = PageableArray( with: model.variants.nodes, pageInfo: PageMetaData(pageInfo: model.variants.pageInfo) ) if let metaFieldValue = model.metafield?.value { self.storeLocation = metaFieldValue BSLog.debugLog("MetaField : \(metaFieldValue)") } else { self.storeLocation = "No Location" } } }
Below is how I'm fetching the products:
func fetchProducts(with ids: [String], completion: @escaping (PageableArray<ProductModel>?) -> Void) { let query = ClientQueryProduct.queryForProducts(with: ids) let task = self.client.queryGraphWith(query) { (query, error) in error.debugPrint() if let query = query, let results = query.nodes as? [Storefront.Product] { let products = PageableArray( with: results, pageInfo: PageMetaData() ) completion(products) } else { print("Failed to load products: \(String(describing: error))") completion(nil) } } task.resume() }
How I'm querying the product:
static func queryForProducts(with ids: [String]) -> Storefront.QueryRootQuery { let graphIDs: [GraphQL.ID] = ids.map { GraphQL.ID.init(rawValue: $0) } return Storefront.buildQuery { $0 .nodes(ids: graphIDs) { $0 .onProduct { $0 .fragmentForProduct() } } } }
The Product Query to fetch the details of the product.
extension Storefront.ProductQuery { @discardableResult func fragmentForProduct() -> Storefront.ProductQuery { return self .id() .title() .descriptionHtml() .variants(first: 250) { $0 .fragmentForStandardVariant() } .images(first: 250) { $0 .fragmentForStandardProductImage() } .metafield(namespace: "custom", key: "location") { $0 .value() } } }
Is there a way I can filter the products using the metaField value?
hi @ShahnasEk
did you found any solution ?
Girish | Shopify Expert
- Was my reply helpful? Click Like to let me know!
- Was your question answered? Mark it as an Accepted Solution
- My Shopify Apps: App Store | Looking for a solution to a problem in your store? Send me an email