Problem displaying the details of the last order placed by a Woocommerce customer

EDIT ==> Thank you @Bhautik for your answer, it debugged a part of my page, but the critical error message remains. After having tested, it is this code which causes problems (it follows the code of my first question) ((I think the error is close to $thumbnail = $product->get_image(array( 50, 50));

Here is the code in question:

<div class="row last-order">
  <h3>Derniere commande</h3>
<div>
    <div>
  <h6>État : <?php echo esc_html( wc_get_order_status_name( $order_status ) ); ?></h6>
    <p>COMMANDE N° <?php echo $order_id;?></p>
</div>
    <p><?php echo $order_total."€"; ?></p>
    <p>
        <?php 
            setlocale(LC_TIME, 'fr_FR');
            date_default_timezone_set('Europe/Paris');
            echo utf8_encode(strftime('%d %B %Y', strtotime($date_created)));
            //echo date('d-F-Y', strtotime($date_created)); ?>
    </p>
    <div>
    <?php foreach ( $last_order->get_items() as $item ) : ?>
        <?php 
   $product   = $item->get_product(); // Get the WC_Product object (from order item)
    $thumbnail = $product->get_image(array( 50, 50)); // Get the product thumbnail (from product object)
    if( $product->get_image_id() > 0 ){
        $item_name = '<div class="item-thumbnail">' . $thumbnail . </div> . $item_name;
}
 echo $item_name . $item->get_name();?>
    <?php endforeach;?>
    </div>
    <div>
        <p>Tout Voir>
    </div>
</div>  

I used the code in this article (How to get the last order of a customer in Woocommerce) to display the user's last order on their dashboard. This ONLY works for users who have already placed an order. Otherwise, if no order has been placed by the logged in account, the "my account" page of the site announces a critical error.

add_action( 'woocommerce_account_dashboard', 'recent_order', 1 );
function recent_order(){
// 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
$date_created  = $last_order->get_date_created();
$order_total = $last_order->get_total();
?>

Can you see where the error is in my code? Thanks in advance!

Solutions

The get_last_order() method can return WC_Order object or false boolean value:

So you can use the is_a() PHP function to check that a $last_order is an Object of that class.

add_action( 'woocommerce_account_dashboard', 'recent_order', 1 );
function recent_order(){
    // 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 ( is_a( $last_order, 'WC_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
            $date_created  = $last_order->get_date_created();
            $order_total = $last_order->get_total();
        }

    endif;
}

Similar questions

How to remove fields from WooCommerce Customer Thank You Email (Customer Processing Order)
I'm in some dire need for help. I can't seem to find anything online about this and your input would be greatly appreciated. I need to remove the following from the Customer Processing Order email that Woocommerce sends customers once they have placed an order: I've attached an image for a visual look at what needs ot be removed from the email: ite...
How can I get customer details from an order in WooCommerce?
I have a function that does this: How can I get customer details from this? I have tried everything in the documentation, but somehow, just some details are present, but the rest aren't. For example. Is empty. Doing Produces: object(WC_Customer)#654 (2) { ["_data":protected]=> array(14) { ["country"]=> string(2) "IT&q...
Woocommerce display extra field in customer details in order view page
When customer submits order and he goes to a thank you page where order details are shown. Inside the customer details, i want to display a single note. Currently i have edited 'order-details-customer.php', it works fine but i want to handle this using woocommerce hooks. I have tried this code, but its not working. Here is the placement of customer...
Get the Order billing city in WooCommerce email-customer-details.php email template
I don't understand why get_billing_city() method doesn't work in WC_email customer_details() method, unlike get_billing_country() is working perfectly. How to make get_billing_city() working in WC_Email customer_details() method to get the correct output in email-customer-details.php WooCommerce template?
How to send Customer invoice / Order details email notification Automatically In Woocommerce
I want to send Customer invoice / Order details email notification Automatically In Woocommerce.When i checking Customer invoice / Order email notification in woocommerce it have only way to send Customer invoice / Order email manually.I want to send Customer invoice / Order email notification when order status is pending payment.If there any funct...
add recipients to "Customer invoice / Order details" in Woocommerce emails
In Woocommerce, there is already a built-in way to add recipients to the "New Order" , "Failed", and "Cancelled" emails, but for some reason, the "Customer invoice / Order details" don't allow any recipient other than the customer. There is a simple plugin that allows for that, but it is very limited in featu...

Also ask

We use cookies to deliver the best possible experience on our website. By continuing to use this site, accepting or closing this box, you consent to our use of cookies. To learn more, visit our privacy policy.