Snake93
Snake93

Reputation: 476

How to get last order details of a customer in Woocommerce

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: How to get the last order of a customer in Woocommerce

I have implemented the recommended solution and everything seems to be working fine. The only problem is that if the user didn't place any order then the page view is bugged.

I have implemented the code in the following way, could someone point me where I'm wrong ?

//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();

    $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 class="last_order_items"><?php echo $item->get_name(); ?></div>

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


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

it works fine for me as the admin and I have some trial purchases, but not for users who have not made any purchases.

broken layout for users who have not placed orders: enter image description here

Worked for me: enter image description here

Upvotes: 1

Views: 1615

Answers (2)

Snake93
Snake93

Reputation: 476

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 class="last_order_items"><?php echo $item->get_name(); ?></div>
<div class="last_order_items"><?php echo $item->get_total(); ?>€</div>

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

<?php else : ?>

<div class="woocommerce-message woocommerce-message--info woocommerce-Message woocommerce-Message--info woocommerce-info">
        <a class="woocommerce-Button button" 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; ?>

Upvotes: 0

jtowell
jtowell

Reputation: 246

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 class="last_order_items">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 class="last_order_items"><?php echo $item->get_name(); ?></div>

<div class="last_order_items"><?php echo $order_total = $order_data['total']; ?>€</div>
<div class="last_order_items"><?php echo $order_date_created = $order_data['date_created']->date('Y-m-d H:i:s'); ?></div>
<div class="last_order_items"><?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

Upvotes: 2

Related Questions