【发布时间】:2022-01-24 23:29:26
【问题描述】:
我正在尝试构建一个易于访问且用户友好的仪表板。所以我想向用户提供上次购买的详细信息。做了一些研究,我发现了这篇文章:How to get the last order of a customer in Woocommerce
我已经实施了推荐的解决方案,一切似乎都运行良好。唯一的问题是,如果用户没有下任何订单,那么页面视图就会出错。
我已经按照以下方式实现了代码,有人能指出我错在哪里吗?
//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; ?>
作为管理员,它对我来说很好,我有一些试用购买,但不适用于没有购买任何东西的用户。
【问题讨论】:
标签: php html wordpress woocommerce