php - 当注册客户尚未在 WooCommerce 中购买时显示自定义文本

标签 php html wordpress woocommerce orders

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

相关文章:

php - codeigniter 302 发现错误

android chrome html5 实时音频在 5 分钟后停止播放

javascript - 一次点击打开 2 个页面,但不是同时打开

php - 如何在jquery中获取wordpress的COOKIEPATH?

php - 无法通过套接字 '/var/run/mysqld/mysqld.sock' 连接到本地 MySQL 服务器 (13)

css - WordPress 菜单 UL 高度

php - 使用我的 PHP 服务将 AngularJS 连接到 mysql?

php - Laravel Eloquent 计算

php - 删除布鲁克林主题的视差背景

javascript - AngularJS - ng include sidebar 将没有下拉菜单的功能