Navas Fazil
Navas Fazil

Reputation: 51

Woocommerce order by seller name or author name

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

Upvotes: 2

Views: 3005

Answers (1)

LoicTheAztec
LoicTheAztec

Reputation: 253969

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

Upvotes: 1

Related Questions