【问题标题】:Woocommerce how to display the details of the last order placed by a customerWoocommerce如何显示客户最后一次下订单的详细信息
【发布时间】:2021-10-30 11:09:48
【问题描述】:

编辑
感谢 @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>  

我使用本文中的代码 (How to get the last order of a customer in Woocommerce) 在其仪表板上显示用户的最后一个订单。 这仅适用于已经下订单的用户。否则,如果已登录的帐户没有下订单,则网站的“我的帐户”页面会显示严重错误。

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

你能看到我的代码中的错误在哪里吗?

提前致谢!

【问题讨论】:

  • 你检查了 debug.log 文件吗?
  • 谢谢!查看日志后,是 LiteSpeedCache 插件导致了问题。我停用了它,错误不再存在!好吧,我不明白这种不兼容性,我认为我必须摆脱这个插件。

标签: php wordpress woocommerce woocommerce-theming customer


【解决方案1】:

get_last_order() 方法可以返回WC_Order objectfalse boolean value

因此您可以使用is_a() PHP 函数来检查$last_order 是否是该类的对象。

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

【讨论】:

  • 感谢@Bhautik 的回答,但问题仍然存在。如果您有时间,我会更新我的帖子!提前感谢您的帮助!
猜你喜欢
  • 1970-01-01
  • 2018-10-19
  • 2016-12-28
  • 2015-01-28
  • 2014-05-15
  • 2017-01-17
  • 1970-01-01
  • 2018-06-21
  • 1970-01-01
相关资源
最近更新 更多