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

Php 当注册客户尚未在WooCommerce中购买时显示自定义文本,php,html,wordpress,woocommerce,orders,Php,Html,Wordpress,Woocommerce,Orders,在WooCommerce中,我有以下代码显示上次客户订单(注册客户)的订单项目名称和订单状态: 但是,如果客户没有任何订单,我希望它显示“尚未购买”之类的内容。我还没有找到做这件事的方法 当注册客户没有最后订单时,如何以及在何处添加显示自定义文本的条件?尝试以下操作,以便在登录客户尚未购买时显示自定义文本: <?php // For logged in users only if ( is_user_logged_in() ) : // Get the current WC_Cu

在WooCommerce中,我有以下代码显示上次客户订单(注册客户)的订单项目名称和订单状态:


  • 但是,如果客户没有任何订单,我希望它显示“尚未购买”之类的内容。我还没有找到做这件事的方法


    当注册客户没有最后订单时,如何以及在何处添加显示自定义文本的条件?

    尝试以下操作,以便在登录客户尚未购买时显示自定义文本:

    <?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; ?>
    
    
    #
    


    测试并正常工作。

    这是一个if条件问题,您可以检查
    $last\u order
    是否实际包含数据,如果不包含(false)。。。echo“还没买”。我在这方面很年轻?如果您不知道如何添加if条件,您如何编写上述代码?那条信息应该出现在哪里,哪个部门?非常感谢。谢谢。是否有一种方法可以将订单id显示给客户?@BrendanFredrich您想在html中的什么位置显示它?你想如何格式化它?我想它上面的订单项目定位<代码>#订单号
      @BrendanFredrich刚刚更新了代码,以包含指定的订单号。
      <?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; ?>