2016-11-12 72 views
0

我在前端有一个受密码保护的页面。在该页面上,我想显示与单个woocommerce产品ID关联的所有购买的完整列表。如何显示显示最近的第一个产品的所有订单? Woocommerce

选项1:

我已经通过了woocommerce docs一看,我发现: WC_CLI_Orderlist_($args, $assoc_args)功能,列出了所有的订单。

据推测,订单可以通过product id

过滤[ - =]基于顺序属性 过滤订单。

订单项字段(数值数组,开始与指数零):

line_items.0.product_id 

选项2:

found this article,但它是基于客户ID。我修改了基于meta_key和meta_value猜测的代码。

$customer_orders = get_posts(array(
    'numberposts' => -1, 
    'meta_key' => '_product', 
    'meta_value' => get_product_id(), 
    'post_type' => wc_get_order_types(), 
    'post_status' => array_keys(wc_get_order_statuses()), 
)); 

如何显示显示最近订单的单个产品的所有订单?

+0

有您是否尝试更改查询以根据您的产品购买产品?像get_purchases(array('product'=>'pencil'))它正在构建查询玩具的需求。我不是woocommerce的专家。但我认为这些数组应该是一个查询键如meta_value是不需要的,如果它不在您的表中 –

回答

2

这里是我放在一起基于这些文章的解决方案:

解决方案

<?php /* Template Name: CustomPageT1 */ ?> 
<?php 
global $wpdb; 
$produto_id = 41; // Product ID 
$consulta = "SELECT order_id " . 
      "FROM {$wpdb->prefix}woocommerce_order_itemmeta woim " . 
      "LEFT JOIN {$wpdb->prefix}woocommerce_order_items oi " . 
      "ON woim.order_item_id = oi.order_item_id " . 
      "WHERE meta_key = '_product_id' AND meta_value = %d " . 
      "GROUP BY order_id;"; 

$order_ids = $wpdb->get_col($wpdb->prepare($consulta, $produto_id)); 

foreach($order_ids as $order_id) { 
    var_dump($order_id); 
} 

if($order_ids) { 
    $args = array(
     'post_type' => 'shop_order', 
     'post__in' => $order_ids, 
     'post_status' => 'publish', 
     'posts_per_page' => 20, 
     'order' => 'DESC', 
     'tax_query' => array(
      array(
       'taxonomy' => 'shop_order_status', 
       'field' => 'slug', 
       'terms' => array (
        'Pending' , 'Failed' , 'Processing' , 'Completed', 'On-Hold' , 'Cancelled' , 'Refunded' 
       ) 
      ) 
     ) 
    ); 
    $wc_query = new WP_Query($args); 
} 

?> 

// Display products 
<div> 
    <?php if ($wc_query->have_posts()) : ?> 
    <?php while ($wc_query->have_posts()) : // (4) 
        $wc_query->the_post(); // (4.1) ?> 

    <ul> 
     <li> 
      <?php the_title(); // (4.2) ?> 
     </li> 
    </ul> 

    <?php endwhile; ?> 
    <?php wp_reset_postdata(); // (5) ?> 
    <?php else: ?> 
    <p> 
     <?php _e('No Orders'); // (6) ?> 
    </p> 
    <?php endif; ?> 
</div> 
+0

感谢您的解决方案,对我的需求非常满意。请注意,WooCommerce> 2.2不再为订单状态使用分类法。你应该使用post_status来代替。 –

相关问题