phpwordpresswoocommercewoocommerce-theming

How to display the details of the last order placed by a


EDIT @Bhautik's answer 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?


Solution

  • 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;
    }