在 WooCommerce 中,我有以下代码,用于显示上次客户订单(注册客户)的订单项目名称和订单状态:
<?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
<div class="row last-order">
<div class="col-md-7">
<ul>
<?php foreach ( $last_order->get_items() as $item ) : ?>
<li><?php echo $item->get_name(); ?></li>
<?php endforeach; ?>
</ul>
</div>
<div class="col-md-4 order-status-box">
<h6 class="status"><?php echo esc_html( wc_get_order_status_name( $order_status ) ); ?></h6>
<i class="fas fa-chevron-down icon"></i>
</div>
</div>
<?php endif; ?>
但是,如果客户没有任何订单,我希望它显示“尚未购买”之类的内容。我还没有找到方法。
当注册客户没有最后订单时,如何以及在何处添加条件来显示自定义文本?
最佳答案
尝试以下操作,在登录的客户尚未购买时显示自定义文本:
<?php
// For logged in users only
if ( is_user_logged_in() ) :
// Get the current WC_Customer instance Object
$customer = new WC_Customer( get_current_user_id() );
// Get the last WC_Order Object instance from current customer
$last_order = $customer->get_last_order();
?>
<div class="row last-order">
<?php
if( is_a($last_order, 'WC_Order') ) :
?>
<div class="col-md-7">
<div class="order_number">#<?php echo $last_order->get_order_number(); ?></div>
<ul>
<?php foreach ( $last_order->get_items() as $item ) : ?>
<li><?php echo $item->get_name(); ?></li>
<?php endforeach; ?>
</ul>
</div>
<div class="col-md-4 order-status-box">
<h6 class="status"><?php echo esc_html( wc_get_order_status_name( $last_order->get_status() ) ); ?></h6>
<i class="fas fa-chevron-down icon"></i>
</div>
<?php else : ?>
<p><?php _e("You have not made a purchased yet.", "woocommerce"); ?></p>
<?php endif; ?>
</div>
<?php endif; ?>
经过测试并有效。
关于php - 当注册客户尚未在 WooCommerce 中购买时显示自定义文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63110432/