moltin / js-sdk

JavaScript & Node.js SDKs for the Elastic Path Commerce Cloud eCommerce API
http://documentation.elasticpath.com/
MIT License
173 stars 77 forks source link

Cannot filter Products by Category #117

Closed ThomasSmWatson closed 7 years ago

ThomasSmWatson commented 7 years ago

Previously in V1, you could filter products by Category; it seems you cannot do that now in the SDK. My current v1 implementation of moltin requires the capability to do this. I would prefer not to do this manually on my micro-service if possible

ynnoj commented 7 years ago

@ThomasSmWatson Unfortunately it is not currently possible to filter products by relationship on V2 of the API. This is not a limitation of this library. The best way to achieve this would be to get the category in question, and include the related products in the payload.

With the category ID:

Moltin.Categories.Get(id).With(['products'])

Or alternatively with the name/slug:

Moltin.Categories.All().Filter(
  eq: {
    slug: 'category-slug'
  }
}).With(['products']);

The response payload will include a top level included object with a products array.

{
  "data": {
     // Your category record
  },
  "included": {
    "products": [
      // The related products
    ]
  }
}

Filtering by relationships is functionality that will we introduce further down the product roadmap.

Hope this helps! 🙃

GanchoDanailov commented 6 years ago

Uncaught TypeError: Moltin.Categories.Get(...).With is not a function

ynnoj commented 6 years ago

@GanchoDanailov Oops, the correct way to chain those methods:

Moltin.Categories.With('products').Get(id)

Hope this helps 😄