2

We are developing an iOS app for a Magento website using Magento2 rest apis. App is fetching and showing all products added to the magneto website. But some of the products are not being added to the customer/guest cart as they are unavailable as of now.

In order to achieve this functionality we have added a new filter to show the products based on their stock and quantity's availability status. for this we have added "quantity_and_stock_status" filter to the products api, which is not working.

Here are the details of the api we have implemented for filtration of products:

func fetchItems(field:String?, value:String?) {
    //Check for internet connection.
    guard self.isNetworkAvailable() == true else {
        return
    }
    self.showProgressHUD()

    var productSearchAPI = API.host +  "/rest/V1/products?"
    if let fieldName = field, let fieldValue = value {
        productSearchAPI += "searchCriteria[filter_groups][0][filters][0][field]=\(fieldName)"
        productSearchAPI += "&searchCriteria[filter_groups][0][filters][0][value]=\(fieldValue)"
        productSearchAPI += "&searchCriteria[filter_groups][0][filters][0][condition_type]=eq&"
    }

    productSearchAPI += "searchCriteria[filter_groups][0][filters][1][field]=status"
    productSearchAPI += "&searchCriteria[filter_groups][0][filters][1][value]=1"
    productSearchAPI += "&searchCriteria[filter_groups][0][filters][1][condition_type]=eq&"

    /*productSearchAPI += "searchCriteria[filter_groups][0][filters][2][field]=quantity_and_stock_status"
    productSearchAPI += "&searchCriteria[filter_groups][0][filters][2][value]=0"
    productSearchAPI += "&searchCriteria[filter_groups][0][filters][2][condition_type]=gt&"*/

    productSearchAPI += "searchCriteria[pageSize]=\(pageSize)"
    productSearchAPI += "&searchCriteria[currentPage]=\(currentPage)"

    Alamofire.request(productSearchAPI, method: .get, parameters: nil, encoding: URLEncoding.default, headers: ["Authorization":API.key]).responseJSON { response in

        self.hideProgressHUD()
        guard let JSON = response.result.value as? [String:AnyObject], let items = JSON["items"] as? [[String:AnyObject]], let pages = JSON["total_count"] as? Int else {
            return
        }
        if self.currentPage == 1 {
            self.items = items
        } else {
            self.items?.append(contentsOf: items)
        }
        self.totalPages = pages
        self.collectionView.reloadData()
    }
}

Can you please help us by pointing what is wrong with our product list filter's implementation?

5
  • Actually it's virtual attribute to represent stock information on product form. It does not persist in DB so you cannot filter by it. Also it represents two values, so how are you expect to filter by it?
    – KAndy
    Jul 29, 2019 at 9:37
  • 1
    Also if you build client facing app, better to use GraphQL for it see devdocs.magento.com/guides/v2.3/graphql
    – KAndy
    Jul 29, 2019 at 9:39
  • So on what basis we can filter our product list and show only available products only? Jul 29, 2019 at 9:49
  • Rest API is not designed for that. To do it you need build your own view, and query it, or use GraphQL
    – KAndy
    Jul 29, 2019 at 10:08
  • Thanks, we will try to learn GraphQL or implement custom api for querying products availability. However I still have a strong feeling that there should be a parameters provided by Magento for such kind of simple filtration. Jul 29, 2019 at 11:25

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.