【问题标题】:How to get last order details of a customer in Woocommerce如何在 Woocommerce 中获取客户的最后订单详细信息
【发布时间】: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


    【解决方案1】:

    正如您提到的,如果用户还没有订单,这将无法正常工作。如果是这种情况,get_last_order() 将返回 false。因此,您需要检查 get_last_order 是否为 false 或不是对象,如果是,则显示一些备用 html 例如

    //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();
    
        if ( ! $last_order ) {  // or check if ( ! is_object( $last_order ) )
            ?><div class="last_order_items">You currently have no orders</div><?php
        } else {
        $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; ?>
    

    我还没有测试过,但我相信这应该可以工作

    【讨论】:

    【解决方案2】:

    @jtowell 发布的解决方案完美运行,再次感谢。然而,在回答这个问题之前,我一直在寻找解决方案,我发现了这个:Display a custom text when a registered customer has not yet purchased in 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();
    ?>
    
    <?php if( is_a($last_order, 'WC_Order') ) : ?>
       
    <?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 $item->get_total(); ?>€</div>
    
    <div class="order_number">#<?php echo $last_order->get_order_number(); ?> </div>
    <div class="order_number">Data acquisto <?php echo $last_order->get_date_created()->date('d/m/Y - H:i'); ?> </div>
    <div class="order_number">Stato dell'ordine <?php echo esc_html( wc_get_order_status_name( $last_order->get_status() ) ); ?>
      
    <?php endforeach; ?>
    
    <?php else : ?>
    
    <div class="woocommerce-message woocommerce-message--info woocommerce-Message woocommerce-Message--info woocommerce-info">
            <a class="woocommerce-Button button" href="<?php echo esc_url( apply_filters( 'woocommerce_return_to_shop_redirect', wc_get_page_permalink( 'shop' ) ) ); ?>"><?php esc_html_e( 'Browse products', 'woocommerce' ); ?></a>
            <?php esc_html_e( 'No order has been made yet.', 'woocommerce' ); ?>
            </div>
    
    <?php endif; ?>
    <?php endif; ?>
    

    【讨论】:

      猜你喜欢
      • 2014-05-15
      • 2017-01-17
      • 2021-10-30
      • 1970-01-01
      • 2016-12-28
      • 2015-01-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多