Home > Net >  How to get last order details of a customer in Woocommerce
How to get last order details of a customer in Woocommerce

Time:01-25

I'm trying to build an accessible and user-friendly dashboard. So I wanted to offer the user the details of the last purchase made. Doing some research I found this post: enter image description here

Worked for me: enter image description here

CodePudding user response:

As you mention this wont work correctly if a user has no orders yet. The get_last_order() returns false if this is the case. So you would need to check if get_last_order is false or not an object, and if so display some alternate html eg

//Start Woocommerce Add Info Order on Dashboard 

<?php
    // For logged in users only
    if ( is_user_logged_in() ) :

    $user_id = get_current_user_id(); // The current user ID

    // Get the WC_Customer instance Object for the current user
    $customer = new WC_Customer( $user_id );

    // Get the last WC_Order Object instance from current customer
    $last_order = $customer->get_last_order();

    if ( ! $last_order ) {  // or check if ( ! is_object( $last_order ) )
        ?><div >You currently have no orders</div><?php
    } else {
    $order_id     = $last_order->get_id(); // Get the order id
    $order_data   = $last_order->get_data(); // Get the order unprotected data in an array
    $order_status = $last_order->get_status(); // Get the order status
    $currency     = $last_order->get_currency(); //Get Currency

?>


<?php foreach ( $last_order->get_items() as $item ) : ?>

<div ><?php echo $item->get_name(); ?></div>

<div ><?php echo $order_total = $order_data['total']; ?>€</div>
<div ><?php echo $order_date_created = $order_data['date_created']->date('Y-m-d H:i:s'); ?></div>
<div ><?php echo esc_html( wc_get_order_status_name( $order_status ) ); ?></div>


<?php endforeach; ?>
<?php }
      endif; ?>

I haven't tested, but I am confident this should work

CodePudding user response:

The solution posted by @jtowell works perfectly, thanks again. However, before this question was answered I was looking for a solution and I found this: Display a custom text when a registered customer has not yet purchased in WooCommerce

After that, I implemented my question code in the following way, this solution also worked:

<?php

// For logged in users only
if ( is_user_logged_in() ) :

// Get the current WC_Customer instance Object
$customer = new WC_Customer( get_current_user_id() );

// Get the last WC_Order Object instance from current customer
$last_order = $customer->get_last_order();
?>

<?php if( is_a($last_order, 'WC_Order') ) : ?>
   
<?php foreach ( $last_order->get_items() as $item ) : ?>

<div ><?php echo $item->get_name(); ?></div>
<div ><?php echo $item->get_total(); ?>€</div>

<div >#<?php echo $last_order->get_order_number(); ?> </div>
<div >Data acquisto <?php echo $last_order->get_date_created()->date('d/m/Y - H:i'); ?> </div>
<div >Stato dell'ordine <?php echo esc_html( wc_get_order_status_name( $last_order->get_status() ) ); ?>
  
<?php endforeach; ?>

<?php else : ?>

<div >
        <a  href="<?php echo esc_url( apply_filters( 'woocommerce_return_to_shop_redirect', wc_get_page_permalink( 'shop' ) ) ); ?>"><?php esc_html_e( 'Browse products', 'woocommerce' ); ?></a>
        <?php esc_html_e( 'No order has been made yet.', 'woocommerce' ); ?>
        </div>

<?php endif; ?>
<?php endif; ?>
  •  Tags:  
  • Related