如何在woocommerce

时间:2018-04-13 13:02:48

标签: php wordpress woocommerce

如何在woo-commerce中按产品ID获取所有订单详细信息我的代码很简单,但我无法获得详细信息。

function getProductId($id){
$customer_orders = get_posts( array(
    'numberposts' => -1,
    'meta_key'    => '_customer_user',
    'meta_value'  => get_current_user_id(),
    'post_type'   => wc_get_order_types(),
    'post_status' => array_keys( wc_get_order_statuses() ),
) );

foreach ($customer_orders as $customer) {

    $order = wc_get_order( $customer->ID );
    $items = $order->get_items();

    foreach ( $items as $item ) {
        if ($id == $item->get_product_id())
            return true;
    }
} } global $product; $id = $product->get_id(); getProductId($id);

1 个答案:

答案 0 :(得分:0)

检查此功能

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;
}

参考:Get all Orders IDs from a product ID