从产品ID中获取所有订单ID

时间:2017-04-27 17:55:49

标签: php wordpress woocommerce product orders

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

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

我知道如何通过MySQL实现这一点,但有没有办法通过WP_Query函数来实现这一点?

4 个答案:

答案 0 :(得分:9)

  

更新:将SQL查询更改为 "SELECT DISTINCT" 而不是 "SELECT" 更改为避免<数组中的/ strong> 重复 订单ID (然后无需 array_unique() 来过滤重复项...)。

使用WP查询这是不可能的,但是使用WordPress class wpdb您可以轻松地执行此操作,包括SQL查询。

然后您可以将此功能嵌入到以 $product_id 作为参数的自定义函数中。
您必须在其中设置订单状态你的目标是什么。

所以这是完成这项工作的功能:

function retrieve_orders_ids_from_a_product_id( $product_id ) {
    global $wpdb;

    // 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 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 LIKE '_product_id'
        AND woim.meta_value LIKE '$product_id'
        ORDER BY woi.order_item_id DESC"
    );
    // Return an array of Orders IDs for the given product ID
    return $orders_ids;
}

此代码包含在任何php文件中。

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

用法示例:

## 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 = retrieve_orders_ids_from_a_product_id( $product_id );

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

答案 1 :(得分:2)

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

E.g。

如果有一种名为&#34; apples&#34; with product_id =&gt; 1036

客户放置&#34;苹果&#34;在他们的购物车中购买3次,创建order_id =&gt; 555

如果我查询product_id-&gt; 1036,我会得到数组(555,555,555)。

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

Declare @FWIdle as bYN, @NewComp as bYN

set @FW = 'Y'           
set @NEW = 'Y'  

select Case(
when w.WearRate <0.400 then round((- 50*w.WearRate+112.5),2
when w.WearRate <0.450 then round((-100*w.WearRate+132.5),2)
when w.WearRate <0.550 then round((- 50*w.WearRate+110.5),2)
else 52.5 END as Score

from EMWear w

答案 2 :(得分:1)

修改功能以获取特定用户产品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>';

答案 3 :(得分:0)

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

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
  foreach ($order_ids as $order_id) {
    $filtered_order_ids = array();
    $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';

$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);