获取用户拥有的所有产品

发布于 2024-12-16 00:57:40 字数 356 浏览 3 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

吾性傲以野 2024-12-23 00:57:40

好吧,如果其他人感兴趣的话,答案如下:

显然有一个名为“field_data_commerce_product”的表,其中有一个用于行项目 ID 的“entity_id”列和一个用于产品 ID 的“commerce_product_product_id”列。该表允许 db_select 轻松连接订单、行项目和产品,以实现我询问的那种查询。

完整的查询如下:

<?php
  // Assuming we have $user populated with the user's ID:
  $query = db_select('commerce_order', 'cord');
  $query->join('commerce_line_item', 'li', 'cord.order_id = li.order_id');
  $query->join('field_data_commerce_product', 'prod', 'li.line_item_id = prod.entity_id');
  $query->condition('cord.uid', $user, '=')
        ->condition('cord.status', 'completed', '=')
        ->fields('prod', array('commerce_product_product_id'));
  $result = $query->execute();
?>

这将返回给定用户已经拥有的所有产品 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:

<?php
  // Assuming we have $user populated with the user's ID:
  $query = db_select('commerce_order', 'cord');
  $query->join('commerce_line_item', 'li', 'cord.order_id = li.order_id');
  $query->join('field_data_commerce_product', 'prod', 'li.line_item_id = prod.entity_id');
  $query->condition('cord.uid', $user, '=')
        ->condition('cord.status', 'completed', '=')
        ->fields('prod', array('commerce_product_product_id'));
  $result = $query->execute();
?>

This will return all Product IDs already owned by the given user.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文