简体   繁体   English

在 Stripe 中检索所有具有相同元数据值的产品

[英]Retrieve all products with same metadata value in Stripe

In Stripe (using Next.js and Node.js), I'm trying to retrieve all products with the same key-value pair in their metadata:在 Stripe(使用 Next.js 和 Node.js)中,我试图检索其元数据中具有相同键值对的所有产品:

example: retrieve all products with metadata: {category: 'foo'}示例:使用metadata: {category: 'foo'}

In attempts to render category-specific pages, I don't want to list and parse through my whole catalog but I also know that products can only be retrieved through id ( docs ), inferring that I would have to:在尝试呈现特定类别的页面时,我不想列出和解析我的整个目录,但我也知道只能通过id ( docs ) 检索产品,因此我必须:

list all products 
for all products:
  if metadata matches, 
    add id to array
for id array:
  retrieve each

This is brute forcing though, and not a good idea.虽然这是蛮力的,但不是一个好主意。

What would be the best way to retrieve conditionally based on metadata ?根据metadata有条件地检索的最佳方法是什么?

Any help is appreciated.任何帮助表示赞赏。

Stripe's API currently doesn't support querying Products using metadata. Stripe 的 API 目前不支持使用元数据查询产品。 You'd have to retrieve all Products and filter through them.您必须检索所有产品并过滤它们。 Stripe does have a Search API [2] which is currently in Beta so you might want to write in to see if you qualify. Stripe 确实有一个搜索 API [2],它目前处于测试阶段,所以你可能想写信看看你是否有资格。

If you happen to keep a track of Products server-side in a database, you could add an additional column for category.如果您碰巧在数据库中跟踪产品服务器端,则可以为类别添加一个附加列。 You can query your database by category to find those specific Products and then include just those Product IDs using the ids [2] parameter when you retrieve them through Stripe.您可以按类别查询数据库以查找那些特定产品,然后在通过 Stripe 检索它们时使用ids [2] 参数仅包含这些产品 ID。

[1] https://stripe.com/docs/search-api [1] https://stripe.com/docs/search-api

[2] https://stripe.com/docs/api/products/list#list_products-ids [2] https://stripe.com/docs/api/products/list#list_products-ids

Stripe's Search API has been released and is no longer in beta. Stripe 的搜索 API 已发布,不再处于测试阶段。 You can retrieve products using metadata per your original request.您可以根据原始请求使用元数据检索产品。 Example from Stripe documentation: Stripe 文档中的示例:

const stripe = require('stripe')('sk_test_i3ykuKQXMT0WrWVLRUmPODl2');

const product = await stripe.products.search({
  query: 'metadata[\'category\']:\'foo\'',
});

Full documentation here: https://stripe.com/docs/api/products/search此处的完整文档: https://stripe.com/docs/api/products/search

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM