【问题标题】:How to get order detail by product id in woocommerce如何在woocommerce中按产品ID获取订单详细信息
【发布时间】:2018-04-13 13:02:47
【问题描述】:

如何在 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);

【问题讨论】:

    标签: php wordpress woocommerce


    【解决方案1】:

    检查此功能

    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

    【讨论】:

      猜你喜欢
      • 2017-01-17
      • 1970-01-01
      • 1970-01-01
      • 2014-03-07
      • 2018-01-23
      • 2018-02-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多