在SpreeCommerce API中使用taxon_id搜索产品

Opt*_*tte 2 api ruby-on-rails spree

我正在尝试使用SpreeCommerce API搜索产品taxon_ids作为过滤器.搜索api使用ransack gem 谓词进行搜索.我试过了:

/api/products?q[taxon_ids_cont]=x
Run Code Online (Sandbox Code Playgroud)

(其中x是分类单元的id).我也尝试过:

/api/products?q[taxon_ids_in]=x
Run Code Online (Sandbox Code Playgroud)

并且都返回所有产品的json而不进行过滤.我应该在products端点上使用哪些参数来获取过滤的产品,taxon_ids或者我可以如何解决此问题?

Opt*_*tte 5

我找到了解决方案.SpreeCommerce API products在taxons控制器中有一个可以通过/api/taxons/products端点访问的动作.

def products
    # Returns the products sorted by their position with the classification
    # Products#index does not do the sorting.
    taxon = Spree::Taxon.find(params[:id])
    @products = taxon.products.ransack(params[:q]).result
    @products = @products.page(params[:page]).per(500 || params[:per_page])
    render "spree/api/products/index"
  end
Run Code Online (Sandbox Code Playgroud)

端点采用参数id并且可选地采用:q参数,该参数是ransack gem谓词

例如网址:

/api/taxons/products?id=1
Run Code Online (Sandbox Code Playgroud)

将返回分类下所有产品的json,其中包含id1

/api/taxons/products?id=1&q[name_cont]=Bag
Run Code Online (Sandbox Code Playgroud)

将返回分类号为1的产品的json产品,其名称包含单词Bag.我不确定为什么官方API指南中缺少这些信息.