wordpress - How to create Woocoomerce order query with product name? -


hi need orders belongs or contain product. ie if product1 product name need orders in loop contain product . order detail use following query

                      $args = array(                      'post_type'         => 'shop_order',                      'post_status'       => 'publish'                          );           $loop=new wp_query($args);           while($loop->have_posts()): $loop->the_post();           $order_id=get_the_id();          $order = new wc_order($order_id);          ..details fetched query ..               endwhile ; 

to item details can use $order->get_items(); want to

(1) best method fetch order details in loop ?

(2)how fetch orders belongs product1 [where product1 product name ] .?

is know ,please .

for first question yes need think on it, second question can use following,

<?php   global $wpdb;      $produto_id = 22777; // id produto   $consulta = "select order_id {$wpdb->prefix}woocommerce_order_itemmeta woim     left join {$wpdb->prefix}woocommerce_order_items oi      on woim.order_item_id = oi.order_item_id      meta_key = '_product_id' , meta_value = %d     group order_id;";   $order_ids = $wpdb->get_col( $wpdb->prepare( $consulta, $produto_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')                   )                  )                        );             $orders = new wp_query( $args );             } ?>  

for more information refer http://zjstech.com/basic-woocommerce-mysql-queries-for-orders-products-and-categories/


Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -