WooCommerce获取多个订单/批量查询的订单商品数量

时间:2019-10-14 15:03:20

标签: wordpress woocommerce

我在另一页上找到了此代码。

function jdr_get_orders_ids_from_product_id( $product_id, $orders_statuses = [ 'wc-completed' ] ) {
    global $wpdb;

    // Define HERE the orders status to include in  <==  <==  <==  <==  <==  <==  <==
    $orders_statuses = "'" . implode( "', '", $orders_statuses ) . "'";

    # 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"
    );
}

我可以找到除包含特定$ product_id之外的任何订单的$ order_id。

但是,我想更进一步,以获取订单中每种特定产品的数量。

我们可以使用以下几个自定义函数来做到这一点:

function jdr_qty_sold_by_product_id( $product_id ) {
  $qty = 0;
  foreach( jdr_get_orders_ids_from_product_id( $product_id ) as $order_id )
    $qty = $qty + jdr_get_order_item_qty( $order_id, $product_id );
  return $qty;
}

function jdr_get_order_item_qty( $order_id, $product_id ) {
    $_order = wc_get_order( $order_id );
    foreach ($_order->get_items() as $item_id => $item_data) {
      $product = $item_data->get_product();
      if ( $product->get_id() == $product_id )
        return $item_data->get_quantity();
    }
    return 0;
}

但这需要循环遍历每个订单和每个项目,这需要很长时间才能加载到需要此功能的页面上(100多个订单)。

我想知道是否有一个类似于第一个函数的SQL解决方案?

任何帮助表示赞赏。

1 个答案:

答案 0 :(得分:1)

这最终成为解决方案:

function jdr_qty_sold_by_product_id( $product_id ) {
  global $wpdb;

  $data = jdr_get_orders_ids_and_item_ids_from_product_id( $product_id );

  $order_item_ids = array_map(function ($obj) { return $obj->order_item_id; }, $data);

  $order_item_ids = "'" . implode( "', '", $order_item_ids ) . "'";

  return $wpdb->get_var( "
      SELECT sum(meta_value)
      FROM {$wpdb->prefix}woocommerce_order_itemmeta
      WHERE order_item_id IN ( $order_item_ids )
      AND meta_key = '_qty'"
  );
}

在我编辑了另一个函数以包含item_id之后:

function jdr_get_orders_ids_and_item_ids_from_product_id( $product_id, $orders_statuses = [ 'wc-completed' ] ) {
    global $wpdb;

    $orders_statuses = "'" . implode( "', '", $orders_statuses ) . "'";

    return $wpdb->get_results( "
        SELECT DISTINCT woi.order_id, woi.order_item_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"
    );
}