Woocommerce 如何显示客户最后下订单的详细信息

Woocommerce how to display the details of the last order placed by a customer

编辑
谢谢 @Bhautik 的回答,它调试了我页面的一部分,但关键错误消息仍然存在。经过测试,正是这段代码导致了问题(它遵循我第一个问题的代码) ((我认为错误接近 $thumbnail = $product->get_image(array( 50, 50));

这里是有问题的代码:

<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>  

我使用本文 () 中的代码在他们的仪表板上显示用户的最后订单。 这仅适用于已经下订单的用户。否则,如果登录帐户未下订单,则该站点的“我的帐户”页面会宣布严重错误。

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

你能看出我的代码哪里出错了吗?

提前致谢!

get_last_order()方法可以returnWC_Order objectfalse boolean value:

因此您可以使用 is_a() PHP 函数来检查 $last_order 是否是 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;
}