-1

i want to get the order id's for the current user (customer). i tried using get_items() but it is throwing errors to me..returns a null.

is there any specific methods to get those? i guess foreach loop is required

Thanks

closed as off-topic by Mark Kaplun, Jacob Peattie, Krzysiek Dróżdż, Andy Macaulay-Brook, Dan Gayle Jul 16 '18 at 22:05

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Your question should be specific to WordPress. Generic PHP/JS/SQL/HTML/CSS questions might be better asked at Stack Overflow or another appropriate Stack Exchange network site. Third party plugins and themes are off topic, they are better asked about at their developers' support routes." – Mark Kaplun, Jacob Peattie, Krzysiek Dróżdż, Andy Macaulay-Brook, Dan Gayle
If this question can be reworded to fit the rules in the help center, please edit the question.

1

This would obviously depend on the context you need these order ids in, is it in a hook, on a page or in the admin panel and so on. But here is the code to get an array of order ids tied to a customer.

$orders = wc_get_orders(array(
    'customer_id' => get_current_user_id(),
    'return' => 'ids',
))

It's important to use these new methods to fetch data in WooCommerce instead of using custom queries because when the WooCommerce team move to custom tables they will still be compatible. Here is some documentation on the wc_get_orders() function which can be used to get orders with various parameters https://github.com/woocommerce/woocommerce/wiki/wc_get_orders-and-WC_Order_Query.

Hope this is helps you!

  • Thanks a lot! worked perfectly for me. Can i ask what get_items() will do and when to use it? – Latheesh V M Villa Jun 27 '18 at 0:55
  • @LatheeshVMVilla get_items() is a method to get all of the line items on the order. You could use it like $order->get_items() and it would return all of the items tied to that order. – Alex Uleberg Jun 27 '18 at 8:04
  • i am still having some issue . i have made another post . kindly have a check : wordpress.stackexchange.com/questions/307151/… – Latheesh V M Villa Jun 28 '18 at 0:37

Not the answer you're looking for? Browse other questions tagged or ask your own question.