• Resolved eli0086

    (@eli0086)


    Hi Ewout,
    I know this is out the range of the support forum here…but I’m hoping maybe you could point me in the right direction because you’ve been so helpful before…

    My client would like to display the customer’s previous order date on the invoice somewhere (so they can tell when the last time the person ordered and can maybe include a free product or something based on that). Also, they will know that if the previous order date is blank, it’s a brand new customer and they can include information for that person.

    How would I go about adding that info somewhere? I know this is a custom query, but any information you can provide me with would be great. I’ve been researching it, but haven’t come up with anything yet. Thanks!

    https://ww.wp.xz.cn/plugins/woocommerce-pdf-invoices-packing-slips/

Viewing 6 replies - 1 through 6 (of 6 total)
  • Plugin Contributor Ewout

    (@pomegranate)

    Hi Eli,
    This is a bit beyond the support for the free version indeed, but I think I can help you get in the right direction.

    The following code will get an array of all orders for a certain customer. All you need to do is get the first or last result and then use the post id to get the order number…

    <?php
    global $wpdb;
    // we use customer email to also apply this for guest users
    $customer_email = get_post_meta($this->order->id,'_billing_email',true);
    
    // filter only completed & processing
    $order_statuses = array( 'wc-completed', 'wc-processing' );
    $customer_orders = $wpdb->get_results( "SELECT *
    	FROM $wpdb->posts as posts
    
    	LEFT JOIN {$wpdb->postmeta} AS meta ON posts.ID = meta.post_id
    
    	WHERE   meta.meta_key       = '_billing_email'
    	AND     posts.post_type     IN ('" . implode( "','", wc_get_order_types( 'order-count' ) ) . "')
    	AND     posts.post_status   IN ('" . implode( "','", (array) $order_statuses )  . "')
    	AND     meta_value          = '$customer_email'
    " );
    }
    ?>

    Hope that helps!
    Ewout

    Thread Starter eli0086

    (@eli0086)

    Thank you very much for your time! This will definitely get me started. I will post back what I figure out based on the code you’ve provided. This gives me a lot to work with.

    Thread Starter eli0086

    (@eli0086)

    Hi Ewout,
    I’ve been playing around with this code you provided. So far, I’ve been able to output all the dates from all the orders that the customer has placed in the past – I’m still working on being able to just display the last order.

    What I’m having trouble with is this bit of code:

    $customer_email = get_post_meta($this->order->id,'_billing_email',true);

    When I input a specific customer email rather than pulling the email through the code, everything prints out fine. When I try to pull in the data with the above code, it just breaks the template and shows nothing. I’m definitely learning, so I know I’m not doing something right.

    Thread Starter eli0086

    (@eli0086)

    Just FYI, this is what I have so far (that is now pulling just the second to last order instead of all).

    This is working, except that the code I mentioned above breaks it. If I input a specific email, it works fine. I’m having trouble pulling in the billing email from the current customer order.

    ** I’ve pulled bits of code from other sources to make this work, so I know it’s not perfect, and probably not very efficient!

    function wc_get_customer_orders() {
    
    global $wpdb;
    
    // we use customer email to also apply this for guest users
    $customer_email = '[email protected]';
    
    // filter only completed & processing
    $order_statuses = array( 'wc-completed', 'wc-processing' );
    $customer_orders = $wpdb->get_results( "SELECT * FROM $wpdb->posts as posts
    
    	LEFT JOIN {$wpdb->postmeta} AS meta ON posts.ID = meta.post_id
    
    	WHERE   meta.meta_key       = '_billing_email'
    	AND     posts.post_type     IN ('" . implode( "','", wc_get_order_types( 'order-count' ) ) . "')
    	AND     posts.post_status   IN ('" . implode( "','", (array) $order_statuses )  . "')
    	AND     meta_value          = '$customer_email'
    	ORDER BY id DESC LIMIT 1 OFFSET 1 ;
    	" );
    
    	if ( $customer_orders >=1 ) : ?>
    	<table class="shop_table my_account_orders" width="100%">
    
    		<thead>
    			<tr>
    				<th class="order-date"><span class="nobr"><?php _e( 'Last Order Date:', 'woocommerce' ); ?></span></th>
    			</tr>
    		</thead>
    
    		<tbody><?php
    			foreach ( $customer_orders as $customer_order ) {
    				$order = new WC_Order();
    
    				$order->populate( $customer_order );
    
    				$item_count = $order->get_item_count();
    
    				?><tr class="order">
    					<td class="order-date">
    						<time datetime="<?php echo date( 'Y-m-d', strtotime( $order->order_date ) ); ?>" title="<?php echo esc_attr( strtotime( $order->order_date ) ); ?>"><?php echo date_i18n( get_option( 'date_format' ), strtotime( $order->order_date ) ); ?></time>
    					</td>
    
    				</tr><?php
    			}
    		?></tbody>
    
    	</table>
    <?php endif;
    }
    
    add_action( 'wpo_wcpdf_after_order_data', 'wc_get_customer_orders', 10, 2 );
    Plugin Contributor Ewout

    (@pomegranate)

    That explains πŸ™‚ ‘$this’ is not defined outside of the plugins context, filters don’t run in a class but just as procedural scripts. I wrote the code as if it were placed in the template itself, when $this refers to the wcpdf export object (which also holds the order object).

    Anyway, the wpo_wcpdf_after_order_data filter gets two parameters, which you did’t use: $template_type and $order. So you can get the $order_id from that $order object (if you make sure it reads those parameters):

    function wc_get_customer_orders($template_type, $order) {
        $customer_email = get_post_meta($order->id,'_billing_email',true);

    I haven’t tested your code, but that’ll probably do the trick!
    If not, try enabling DEBUG output, as this will give you a more descriptive error.

    Ewout

    Thread Starter eli0086

    (@eli0086)

    That worked perfectly! I actually had the two parameters set in wpo_wcpdf_after_order_data, however when setting a custom email, it didn’t seem to make a difference.
    However, now that I changed both lines of code, it’s working just as expected. Thank you very much for your help on this project…I certainly learned a lot and really appreciate your help and explaining in detail along the way!

Viewing 6 replies - 1 through 6 (of 6 total)

The topic ‘Display Previous Order Date’ is closed to new replies.