Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

woocommerce - order notes date WordPress

  • SOLVED

Hi i would like to show latest "Wed 02-Nov-2016 at 08:14AM" (see attached) in 2 places
1. custom shop_order loop, after Order NO.#
2. email template, after Order NO.#



<strong>Currrently code 1. custom shop_order loop:</strong>
<?php
//refresh page
//$url1=$_SERVER['REQUEST_URI'];
//header("Refresh: 5; URL=$url1");
// header("Refresh: 10;url=http://www.yourdestination.com/");

function redirect_to_current(){
header("Refresh: 10;url=http://www.yourdestination.com/");
}
global $woocommerce, $post;
$current_url=get_permalink();
echo '<a class="x-btn x-btn-square x-btn-regular btnNav" style="position:fixed;top:12px;" href="' .$current_url. '">UPDATE PAGE</a>';

$args = array(
'post_type' =>'shop_order',
'post_status' => 'publish',
'posts_per_page' => -1,
'orderby'=> 'date',
'order' => 'DESC',
'item_meta' => array (
'_sku' => 'ABCD',
),
'tax_query' => array(
array( 'taxonomy' => 'shop_order_status',
'field' => 'slug',
'terms' => array ('pending' , 'failed' , 'processing' , 'completed', 'on-hold' , 'cancelled' , 'refunded')
)
)
);
?>

<?php
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
$order_id = $loop->post->ID;
$order = new WC_Order($order_id);
?>
<?php if ($order->id) : ?><?php echo '#'.$order->id; ?><?php endif;?>

[HERE]




<strong>Currrently 2. email template:</strong>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td align="center"><font face="Arial" size="7"><?php printf(__('Order #%s', 'woocommerce'), $order->get_order_number()); ?></font><br>

[HERE]

Answers (3)

2016-11-02

Rempty answers:

Part 1
After
<?php if ($order->id) : ?><?php echo '#'.$order->id; ?><?php endif;?>
Add
global $wpdb;
$ordreNotesArr = $wpdb->get_results("SELECT comment_date FROM $wpdb->comments c WHERE c.comment_post_ID = ".$order->id." order by c.comment_date DESC LIMIT 1",ARRAY_A);
foreach($ordreNotesArr as $k=>$Notes){
echo printf( __( 'added on %1$s at %2$s', 'woocommerce' ), date_i18n( wc_date_format(), strtotime( $Notes['comment_date'] ) ), date_i18n( wc_time_format(), strtotime( $Notes['comment_date'] ) ) ) ;
}

For part 2 should be the same code


email889 comments:

For 1. shop_order loop
it Works! but there is unknown "35" appearing each render.

How to get rid of it? see attached.
Will test email one and get back to you.


Rempty comments:

Delete the echo from this line

echo printf( __( 'added on %1$s at %2$s', 'woocommerce' ), date_i18n( wc_date_format(), strtotime( $Notes['comment_date'] ) ), date_i18n( wc_time_format(), strtotime( $Notes['comment_date'] ) ) ) ;

should be

printf( __( 'added on %1$s at %2$s', 'woocommerce' ), date_i18n( wc_date_format(), strtotime( $Notes['comment_date'] ) ), date_i18n( wc_time_format(), strtotime( $Notes['comment_date'] ) ) ) ;


email889 comments:

it works great.


email889 comments:

I can't see the link to vote for prize anywhere?? How do I vote for prize?


Rempty comments:

Good question. I can't find it, can you ask to [email protected]?


initiatus comments:

As far as I'm aware there is a green button for that below the question.

2016-11-02

mod mi answers:

Hello,
For the first case (shop_order) you need to add the following after your order ID conditional like this:

<?php $args = array(
'post_id' => $order->id,
'type' => 'order_note',
'status' => 'all'
);
$notes = get_comments($args);

if ( $notes ) {
$numItems = count($notes);
$i = 0;
foreach( $notes as $note ) {
if($i == 0) {
printf( '<p><time datetime="%s">%s</time></p>', date_i18n( 'c', strtotime( $note->comment_date_gmt ) ), date_i18n( wc_date_format(), strtotime( $note->comment_date_gmt ) ) );
} else {
echo '';
}
$i++;
}
}
?>

For the second case (email_template) it's the same logic . You have to add it to the template file email-order-details.php

— If you want the date to appear only for emails sent to customer add it after line 26 (woo commerce v2.6.7) with the <h2> title.

— If you want the date to appear on emails sent to admin too, add it after line 29 (woo commerce v2.6.7) after the endif.

<?php $args = array(
'post_id' => $order->id,
'type' => 'order_note',
'status' => 'all'
);
$notes = get_comments($args);

if ( $notes ) {
$numItems = count($notes);
$i = 0;
foreach( $notes as $note ) {
if($i == 0) {
printf( '<p><time datetime="%s">%s</time></p>', date_i18n( 'c', strtotime( $note->comment_date_gmt ) ), date_i18n( wc_date_format(), strtotime( $note->comment_date_gmt ) ) );
} else {
echo '';
}
$i++;
}
}
?>


email889 comments:

my woocommerce is 2.5.5

1. I tried below, nothing rendered. But $notes returned array.

$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
$order_id = $loop->post->ID;
$order = new WC_Order($order_id);

$args2 = array(
'post_id' => $order->id,
'type' => 'order_note',
'status' => 'all'
);
$notes = get_comments($args2);
//echo '$notes:'.$notes; (rendered array)
if ( $notes ) { printf( '<p><time datetime="%s">%s</time></p>', date_i18n( 'c', strtotime( $note->comment_date_gmt ) ), date_i18n( wc_date_format(), strtotime( $note->comment_date_gmt ) ) );
$numItems = count($notes);
$i = 0;
foreach( $notes as $note ) {
if($i == 0) {
printf( '<p><time datetime="%s">%s</time></p>', date_i18n( 'c', strtotime( $note->comment_date_gmt ) ), date_i18n( wc_date_format(), strtotime( $note->comment_date_gmt ) ) );
} else {
echo '';
}
$i++;
}
}


mod mi comments:

Did it work for your case 2 with the email template?

For case 1 on the your custom shop_order do you include endwhile; to close the loop?
May I ask in which template are you using this code. Is the code you posted the whole in code included in that template. Could you please give more details?


email889 comments:

This is no.1 complete code.

<?php
$args = array(
'post_type' =>'shop_order',
'post_status' => 'publish',
'posts_per_page' => 50,
'order' => 'DESC',
'item_meta' => array (
'_sku' => 'ABCD',
),
'tax_query' => array(
array( 'taxonomy' => 'shop_order_status',
'field' => 'slug',
'terms' => array ('Pending' , 'Failed' , 'Processing' , 'Completed', 'On-Hold' , 'Cancelled' , 'Refunded')
)
)
);
?>
<table id="tblExport" class="demotable1" style="border:1px solid black; ">
<thead>
<tr>
<th ><?php _e('ID:', ' '); ?></th>
<th ><?php _e('order_note - TIME:', ' '); ?></th>
<th ><?php _e('Categories:', ' '); ?></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 $args = array(
'post_id' => $order->id,
'type' => 'order_note',
'status' => 'all'
);
$notes = get_comments($args);
//echo '$notes:'.$notes;
if ( $notes ) {
$numItems = count($notes);
$i = 0;
foreach( $notes as $note ) {
if($i == 0) {
printf( '<p><time datetime="%s">%s</time></p>', date_i18n( 'c', strtotime( $note->comment_date_gmt ) ), date_i18n( wc_date_format(), strtotime( $note->comment_date_gmt ) ) );
} else {
echo '';
}
$i++;
}
}
?>
</td>
<td>
<?php
// Categories
if (sizeof($order->get_items())>0) { foreach($order->get_items() as $item)
{ $_product = get_product( $item['product_id'] );
echo $_product->get_categories( ', ', '' . _n( '', '', $size, 'woocommerce' ) . ' ', ' ' ); } }
?>
</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>

2016-11-02

Arnav Joy answers:

if your problem solved ??


email889 comments:

not yet