php - How to display all orders for a single product showing the most recent first? Woocommerce -
i have password protected page on frontend. on page, want display full list of purchases associated single woocommerce product id.
option 1:
i've looked through woocommerce docs, , found: wc_cli_order , list_( $args, $assoc_args )
function lists orders.
presumably, orders can filtered product id
[--=] filter orders based on order property.
line item fields (numeric array, started index zero):
line_items.0.product_id
option 2:
i found article, based on customer id. i've modified code based on guesses 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() ), ) );
how display orders single product showing recent order first?
here solution put based on these articles:
- http://www.wpbeginner.com/wp-themes/how-to-create-a-custom-page-in-wordpress/#
- https://wordpress.org/support/topic/how-to-show-all-orders-details-for-specific-or-particular-product-in-woocommerce-1/
- https://www.gavick.com/blog/wp_query-woocommerce-products
solution
<?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' , meta_value = %d " . "group order_id;"; $order_ids = $wpdb->get_col( $wpdb->prepare( $consulta, $produto_id ) ); foreach( $order_ids $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>
Comments
Post a Comment