获取用户拥有的所有产品
我正在使用 Drupal 7 和 Commerce 模块。
每当用户将他们已经拥有的产品添加到购物车时,我都会尝试警告用户。获取用户已拥有的产品列表(在 PHP 代码中)似乎相当具有挑战性,因为行项目和产品之间的连接位于序列化的 blob 中。
作为参数,我有当前在购物车中的产品列表和用户。
如果我采用 EntityFieldQuery 的方式,我可以轻松获取与给定产品相关的所有行项目,但我无法扩展查询以仅筛选给定用户完成的订单。 (或者我可以吗?那就太好了。)
如果我采用 db_select 的方式,我可以轻松获取给定用户的所有已完成订单,但我无法桥接行项目 <->产品连接。
任何帮助将不胜感激。
I'm using Drupal 7 and the Commerce module.
I'm trying to warn the user whenever they add to the cart a product they already own. Getting a list of products already owned by the user (in PHP code) seems quite the challenge, because the connection between Line Item and a Product is in a serialized blob.
I have, as arguments, a list of products currently in the cart, and the user.
If I go the way of EntityFieldQuery, I can easily get all line items pertaining to the given products, but I can't extend the query to filter only completed orders by the given user. (Or can I? That would be great.)
If I go the way of db_select, I can easily get all completed orders for the given user, but I can't bridge the Line Item <-> Product connection.
Any help will be appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,如果其他人感兴趣的话,答案如下:
显然有一个名为“field_data_commerce_product”的表,其中有一个用于行项目 ID 的“entity_id”列和一个用于产品 ID 的“commerce_product_product_id”列。该表允许 db_select 轻松连接订单、行项目和产品,以实现我询问的那种查询。
完整的查询如下:
这将返回给定用户已经拥有的所有产品 ID。
Well, here's the answer in case anyone else is interested:
Apparently there's a table called "field_data_commerce_product", which has an "entity_id" column for the Line Item ID, and a "commerce_product_product_id" column for the Product ID. This table allows db_select to easily connect Orders, Line Items, and Products to achieve the kind of query I asked about.
Here's the complete query:
This will return all Product IDs already owned by the given user.