Ask your WordPress questions! Pay money and get answers fast! (more info)

show customer info in shop order WordPress

  • SOLVED

Hi, I would like to show 3 things (after billing_last_name) in my current loop:
1. Customer Total spent (all previous completed orders Total in $amount)
2. Customer Total number of orders (all previous completed orders, Total numbers/count)
3. Customer IP address.

current loop:
Current standard loop code I have is below:

<?php
$args = array(
'post_type' =>'shop_order',
'post_status' => 'publish',
'posts_per_page' => 50,
'order' => 'DESC',
'item_meta' => array (
'_sku' => 'ABCD',
)
);
?>
<table id="tblExport" class="demotable1" style="border:1px solid black; ">
<thead>
<tr>
<th ><?php _e('ID:', ' '); ?></th>
<th ><?php _e('Product:', ' '); ?></th>
<th ><?php _e('Date:', ' '); ?></th>
<th ><?php _e('Value:', ' '); ?></th>
<th ><?php _e('Name:', ' '); ?></th>
<th ><?php _e('E-mail:', ' '); ?></th>
<th ><?php _e('status:', ' '); ?></th>
</tr>
</thead>
<tbody id="export-pla" >
<?php
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
$order_id = $loop->post->ID;
$order = new WC_Order($order_id);
?>
<tr>
<td>
<?php
//ID - order
if ($order->id) : ?><?php echo $order->id; ?><?php endif;?>
</td>
<td>
<?php
// product name
if (sizeof($order->get_items())>0) { foreach($order->get_items() as $item)
{ $_product = get_product( $item['product_id'] ); echo '' . $item['name'] . ''; } }
?>
</td>

<td>
<?php echo the_time('d/m/Y'); ?>
</td>
<td>
<?php if ($order->order_total): $preco_format=($order->order_total);?>
<?php echo $trata_preco=number_format($preco_format, 2, ",", "."); ?><?php endif; ?>
</td>

<td>
<?php if ($order->billing_first_name) : ?><?php echo $order->billing_first_name; ?><?php endif; ?>
<?php if ($order->billing_last_name) : ?><?php echo $order->billing_last_name; ?><?php endif; ?>
</td>

<td>
<?php if ($order->billing_email) : ?><?php echo $order->billing_email; ?><?php endif; ?>
</td>


<td>
<?php if ($order->status) : ?><?php echo $order->status; ?><?php endif; ?>
</td>

</tr>
<?php endwhile; ?>
<?php wp_reset_query(); ?>
</tbody>
</table>

Answers (2)

2016-11-03

Rempty answers:

Hello
I have a question
Example if i have 3 orders
order-3 date 1/12/16 $100
order-2 date 1/10/16 $50
order-1 date 1/08/16 $20
If i want the total of the previous of order-3 = $70
If i want the total of the previous of order-2 = $20
Is this right?

For point 3 use:
echo $order->customer_ip_address;


email889 comments:

Sorry for the delay. Somehow, I stop receiving email notifications. I didn't know there was comment added.

More clarification:
Now, I have a shop_order loop to show list of orders (ALL order status, e.g. processing, completed, etc)
Each Shop_order, has got customer info: billing name, email, address, etc. And here, I would like to add more info about the customers order history.

So, for NO.1 & NO.2 are similar, NO.1 I would like to show "Total Numbers of Orders that this Customer have made so far". For example Order history Number is: 22 (If this customer has got 22 'Completed' status Orders history).

NO.2 Order history TOTAL in DOLLAR amount $000 ('Completed' status Orders history). Basically sum up / total of all orders history in dollar amount.

Let me know if you have any question.

Thanks.


Rempty comments:

Inside your loop you add this code

$customerid = $order->user_id;
$argsc=array(
'post_type' =>'shop_order',
'posts_per_page' => -1,
'meta_key' => '_customer_user',
'meta_value' => $customerid,
'post_status' =>'wc-completed'
);
$total_orders=0;
$count_orders=0;
$orderq=new WP_Query($argsc);
if($orderq->have_posts()):
while($orderq->have_posts()):
$orderq->the_post();
$ordera = wc_get_order( get_the_ID() );
$total_orders+=$ordera->get_total();
$count_orders++;
endwhile;
endif;

For total count fo orders
echo $count_orders;

For the total sum with dollar
echo wc_price($total_orders);


email889 comments:

I tested returned results. Just one thing is when $order->user_id = (empty), which result it is including?


email889 comments:

I checked, when $order->user_id = (empty), it returned total data that is more than total of
all existing different (user_id) and (empty user_id).

is it possible when $order->user_id = (empty), return only non user / guest only data?


Rempty comments:

You can add an if
$customerid = $order->user_id;
if($customerid=='' OR empty($customerid)){
$argsc=array(
'post_type' =>'shop_order',
'posts_per_page' => -1,
'meta_key' => '_customer_user',
'meta_value' => $customerid,
'post_status' =>'wc-completed'
);
$total_orders=0;
$count_orders=0;
$orderq=new WP_Query($argsc);
if($orderq->have_posts()):
while($orderq->have_posts()):
$orderq->the_post();
$ordera = wc_get_order( get_the_ID() );
$total_orders+=$ordera->get_total();
$count_orders++;
endwhile;
endif;
}


email889 comments:

Already tried if ($customerid == '') but result is no difference with your latest code.


Rempty comments:

TRY
var_dump($customerid);
and check what is the value when the user doesn't exist


email889 comments:

if ($customerid == ''){var_dump($customerid); echo $customerid}

Result is string(0) ""

2016-11-03

mod mi answers:

Try this where you want it to display in your loop:

// Get the customer's id from current order
$customer_id = $order->user_id;

// Get all orders of the customer
$customer_orders = get_posts( array(
'numberposts' => -1,
'meta_key' => '_customer_user',
'meta_value' => $customer_id,
'post_type' => wc_get_order_types(),
'post_status' => array_keys( wc_get_order_statuses() )
) );

//Set variable for orders total
$orders_total = 0;

//Loop through all orders, find each order total and sum it in $orders_total var
foreach($customer_orders as $customer_order)
{
$orders_total+= $customer_order->get_total();;
}

//This renders customer's orders total spent
echo count( $customer_orders );

//This renderd customer's orders total count
echo $orders_total;


email889 comments:

Thank you. I tested returned below:
Fatal error: Call to undefined method WP_Post::get_total()