【问题标题】:Get all Orders IDs from a product ID in Woocommerce从 Woocommerce 中的产品 ID 获取所有订单 ID
【发布时间】:2017-09-25 15:56:25
【问题描述】:

如何按产品 ID 获取包含订单 ID 的数组?

我的意思是接收所有展示特定产品的订单。

我知道如何通过 MySQL 做到这一点,但是有没有办法通过 WP_Query 函数做到这一点?

【问题讨论】:

  • 据我所知,WP_Query 不适用于自定义或非 WordPress 默认表,因此如果您检查 plugins\woocommerce\includes\admin\reports\class-wc-report-sales-by-product.php 文件,您必须为此编写 MySQL 查询,WooCommerce 本身使用 MySQL 查询。所以你必须使用@LoicTheAztec 答案。
  • @RaunakGupta 谢谢,确认一下 :)

标签: php wordpress woocommerce product orders


【解决方案1】:

更新:

  • 2017 - SQL 查询更改为 "SELECT DISTINCT" 而不是 "SELECT"避免 数组中的重复 订单 ID(则不需要 array_unique() 来过滤重复...)。

  • 2019 - 在 SQL 查询中启用产品变体类型支持

然后您可以将其嵌入到自定义函数中,并以 $product_id 作为参数。
您必须在其中设置您的订单状态定位。

下面是可以完成这项工作的函数:

function get_orders_ids_by_product_id( $product_id ) {
    global $wpdb;
    
    // Define HERE the orders status to include in  <==  <==  <==  <==  <==  <==  <==
    $orders_statuses = "'wc-completed', 'wc-processing', 'wc-on-hold'";

    # Get All defined statuses Orders IDs for a defined product ID (or variation ID)
    return $wpdb->get_col( "
        SELECT DISTINCT woi.order_id
        FROM {$wpdb->prefix}woocommerce_order_itemmeta as woim, 
             {$wpdb->prefix}woocommerce_order_items as woi, 
             {$wpdb->prefix}posts as p
        WHERE  woi.order_item_id = woim.order_item_id
        AND woi.order_id = p.ID
        AND p.post_status IN ( $orders_statuses )
        AND woim.meta_key IN ( '_product_id', '_variation_id' )
        AND woim.meta_value LIKE '$product_id'
        ORDER BY woi.order_item_id DESC"
    );
}

这段代码可以放在任何 php 文件中。

此代码经过测试,适用于 WooCommerce 版本 2.5+、2.6+ 和 3+


使用示例:

## This will display all orders containing this product ID in a coma separated string ##

// A defined product ID: 40
$product_id = 40;

// We get all the Orders for the given product ID in an arrray
$orders_ids_array = get_orders_ids_by_product_id( $product_id );

// We display the orders in a coma separated list
echo '<p>' . implode( ', ', $orders_ids_array ) . '</p>';

【讨论】:

  • 如果我想扩展此查询以同时返回订购的数量,我该怎么办?
【解决方案2】:

我想指出,如果订单中有多个商品,上述答案将返回 order_id 的副本。

例如

如果有一个名为“apples”的产品,product_id=>1036

客户将“苹果”放入购物车 3 次并购买,创建 order_id=>555

如果我查询product_id->1036,我会得到array(555,555,555)。

可能有一种 SQL 方式可以更快地执行此操作,(感谢任何可以添加到此的人),否则我使用:array_unqiue() 来合并重复项。

function retrieve_orders_ids_from_a_product_id( $product_id )
{
    global $wpdb;

    $table_posts = $wpdb->prefix . "posts";
    $table_items = $wpdb->prefix . "woocommerce_order_items";
    $table_itemmeta = $wpdb->prefix . "woocommerce_order_itemmeta";

    // Define HERE the orders status to include in  <==  <==  <==  <==  <==  <==  <==
    $orders_statuses = "'wc-completed', 'wc-processing', 'wc-on-hold'";

    # Requesting All defined statuses Orders IDs for a defined product ID
    $orders_ids = $wpdb->get_col( "
        SELECT $table_items.order_id
        FROM $table_itemmeta, $table_items, $table_posts
        WHERE  $table_items.order_item_id = $table_itemmeta.order_item_id
        AND $table_items.order_id = $table_posts.ID
        AND $table_posts.post_status IN ( $orders_statuses )
        AND $table_itemmeta.meta_key LIKE '_product_id'
        AND $table_itemmeta.meta_value LIKE '$product_id'
        ORDER BY $table_items.order_item_id DESC"
    );
    // return an array of Orders IDs for the given product ID
    $orders_ids = array_unique($orders_ids);
    return $orders_ids;
}

【讨论】:

    【解决方案3】:

    如果您希望您的代码在未来的 WC 更新中工作,最好使用 WC 提供的函数从 DB 中获取详细信息,因为 WC 经常更改 DB 结构。 我会尝试类似:

    function get_orders_id_from_product_id($product_id, $args = array() ) {
      //first get all the order ids
      $query = new WC_Order_Query( $args );
      $order_ids = $query->get_orders();
      //iterate through order
      $filtered_order_ids = array();
      foreach ($order_ids as $order_id) {
        $order = wc_get_order($order_id);
        $order_items = $order->get_items();
        //iterate through an order's items
        foreach ($order_items as $item) {
          //if one item has the product id, add it to the array and exit the loop
          if ($item->get_product_id() == $product_id) {
            array_push($filtered_order_ids, $order_id);
            break;
          }
        }
      }
      return $filtered_order_ids;
    }
    

    使用示例:

    $product_id = '2094';
    // NOTE: With 'limit' => 10 you only search in the last 10 orders
    $args = array(
        'limit' => 10,
        'orderby' => 'date',
        'order' => 'DESC',
        'return' => 'ids',
    );
    
    $filtered_order_ids = get_orders_id_from_product_id($product_id, $args);
    
    print_r($filtered_order_ids);
    

    【讨论】:

      【解决方案4】:

      修改了获取特定用户产品ID的函数

      function retrieve_orders_ids_from_a_product_id( $product_id,$user_id )
      {
          global $wpdb;
      
          $table_posts = $wpdb->prefix . "posts";
          $table_postmeta = $wpdb->prefix . "postmeta";
          $table_items = $wpdb->prefix . "woocommerce_order_items";
          $table_itemmeta = $wpdb->prefix . "woocommerce_order_itemmeta";
      
          // Define HERE the orders status to include in  <==  <==  <==  <==  <==  <==  <==
          $orders_statuses = "'wc-completed', 'wc-processing', 'wc-on-hold'";
      
          # Requesting All defined statuses Orders IDs for a defined product ID
          $orders_ids = $wpdb->get_col( "
              SELECT DISTINCT $table_items.order_id
              FROM $table_itemmeta, $table_items, $table_posts , $table_postmeta
              WHERE  $table_items.order_item_id = $table_itemmeta.order_item_id
              AND $table_items.order_id = $table_posts.ID
              AND $table_posts.post_status IN ( $orders_statuses )
              AND $table_postmeta.meta_key LIKE '_customer_user'
              AND $table_postmeta.meta_value LIKE  '$user_id '
              AND $table_itemmeta.meta_key LIKE '_product_id'
              AND $table_itemmeta.meta_value LIKE '$product_id'
              ORDER BY $table_items.order_item_id DESC"
          );
          // return an array of Orders IDs for the given product ID
          return $orders_ids;
      }
      

      使用示例

      ## This will display all orders containing this product ID in a coma separated string ##
      
      // A defined product ID: 40
      $product_id = 40;
      
      // Current User
      $current_user = wp_get_current_user();
      
      // We get all the Orders for the given product ID of current user in an arrray
      $orders_ids_array = retrieve_orders_ids_from_a_product_id( $product_id, $current_user->ID );
      
      // We display the orders in a coma separated list
      echo '<p>' . implode( ', ', $orders_ids_array ) . '</p>';
      

      【讨论】:

      • 这对我不起作用。它每次都返回空。
      • @RiotAct 你确定当前用户有订单吗?确保客户登录
      • 是的。当前用户有订单,已登录。我什至在 phpmyadmin 中对此进行了测试并得到一个空响应(即输入了用户 ID 和产品 ID,并将查询重写为对 phpmyadmin sql 有效。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-04-01
      • 2018-03-25
      • 2020-12-09
      • 2021-04-23
      • 1970-01-01
      • 2015-05-27
      • 1970-01-01
      相关资源
      最近更新 更多