3

I am using "How can I get the latest order id in Woocommerce" answer code that returns last order with a custom function get_last_order_id().

Here is my code attempt where I get order items:

<?php

    $latest_order_id = get_last_order_id(); // Last order ID
    $order = wc_get_order( $latest_order_id ); // Get an instance of the WC_Order object
    $order_details = $order->get_data(); // Get the order data in an array
    $order_status = esc_html( wc_get_order_status_name( $order->get_status() ) );
    $order_items = $order_details['line_items'];
?>

Then I use this in this code:

<div class="row last-order">
    <div class="col-md-7">
      <ul>
        <?php
        foreach ($order_items as $product_name) { ?>
          <li><?php echo $product_name['name']; ?></li>
        <?php
        }
        ?>
      </ul>
    </div>
    <div class="col-md-4 order-status-box">
      <h6 class="status"><?php echo $order_status; ?></h6>
      <i class="fas fa-chevron-down icon"></i>
    </div>
</div>

I would like to get the last order for the current customer. How can I change the custom function get_last_order_id() to get the last order for the current customer?

I would like to receive the contents of my cart in addition to the latest order from the current user.

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
Arman Bagheri
  • 608
  • 7
  • 19

1 Answers1

13

Updated: replaced WC()->customer by new WC_Customer( get_current_user_id() ); for better compatibility.

The Class WC_Customer Class include the get_last_order() method to get the last order for a customer (so you don't need anymore the custom function get_last_order_id() from this answer thread).

So your code will be:

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

Tested and works.

Note: Now to "receive the contents of the cart in addition to the latest order from the current user", you will have to ask a new question with more details, one question at the time please.

Related:

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • @PullataPraveen This code still works perfectly on last WooCommerce version. Your comment is completely unclear… What template are you talking about? how are you embedding the code. You should better ask a new question explaining things! – LoicTheAztec May 12 '20 at 13:51
  • can you please check this https://stackoverflow.com/questions/61753817/how-to-get-latest-order-id-if-user-logged-in-woocommerce – Praveen May 12 '20 at 14:05
  • what if a customer is a guest? – Joy Kumar Bera Jun 21 '21 at 08:42
  • I used the proposed code and it works perfectly. However, if the user hasn't placed an order, there is a page display error. How to resolve ? – Snake Jan 23 '22 at 14:04