2

I have the following code and I need to display order data based on seller id or seller name:

$filters = array(
    'post_status' => 'published',
    'post_type' => 'shop_order',
    'posts_per_page' => 200,
    'paged' => 1,
    'orderby' => 'modified',
    'order' => 'ASC',
    'author' => $seller_id,
    'post_parent' => $order_id
);
$loop = new WP_Query($filters);
while ($loop->have_posts()) {
    $loop->the_post();
    $order = new WC_Order($loop->post->ID);
    foreach ($order->get_items() as $key => $item) 
    {
        $red = $item['product_id'];
        $_sku     = get_post_meta( $red, '_sku', true );
    }
}

The above loop is not showing any sku or price on the invoice section.

How to display order details on author ID (or seller name)?

Thanks

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
Navas Fazil
  • 43
  • 1
  • 5
  • Hello and welcome to SO! Please clarify your question to make it as minimal and clear for everyone. Maybe you may be interested in [this documentation](https://stackoverflow.com/help/how-to-ask). – John-Philip Jul 19 '17 at 12:22

1 Answers1

1

Your main problem is the post_status here: For WooCommerce Orders "published" post_status doesn't exist. The available status for WooCommerce Orders are for example:

  • 'wc-cancelled'
  • 'wc-completed'
  • 'wc-custom-status'
  • 'wc-on-hold'
  • 'wc-pending'
  • 'wc-processing'
  • 'wc-refunded'

You can have different order statuses in an array…

Also 'post_parent' is can't be the order ID as it always have a 0 value

To finish since WooCommerce 3+ Order items are now a WC_Order_Item_Product object (and you need to use available methods to access the properties values).

Last thing, to get the user ID from the user name, you can use WordPress function get_user_by().

So your code will be:

$args = array(
    'post_status' => array( 'wc-completed' ), //  <===  HERE
    'post_type' => 'shop_order',
    'posts_per_page' => 200,
    'paged' => 1,
    'orderby' => 'modified',
    'author' => $seller_id
);

$loop = new WP_Query($args);

while ($loop->have_posts()) {
    $loop->the_post();
    $order_obj = wc_get_order($loop->post->ID);
    foreach ($order_obj->get_items() as $item_id => $item_obj)
    {
        $item_data = $item_obj->get_data(); // Accessing WC_Order_Item_Product object protected data
        $product_id = $item_data['product_id']; // Product ID
        $product_sku = get_post_meta( $product_id, '_sku', true ); // SKU

        // Just for Testing output
        echo "Product ID is $product_id - Sku is $product_sku<br>";
    }
}

This code is tested and works for WooCommerce 3+


Helpful Answer: How to get WooCommerce order details

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • Sorry, I am getting zero as output. – Navas Fazil Jul 25 '17 at 06:48
  • Actually I need to get the order based on seller id. Here post_author field. – Navas Fazil Jul 25 '17 at 06:50
  • @NavasFazil I know and I have understood. I always test my code before answering and it just works with the `post_author` ID… Try to replace `$seller_id` by a number corresponding to one of your Sellers IDs in this `WP_Query` and you will see that is working. ALso you have to go in your database in wp_post table to be sure that the `post_author` column is set with the correct Seller IDs for the related `post_type` as `shop_order`… – LoicTheAztec Jul 25 '17 at 07:03