Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the order id from the current user orders In WooCommerce [closed]

Here is the situation. I have a woocommerce site used as a marketplace. I'm selling games on it, for some the purchaser receive a steam key. For that I'm working on a key attribution system, so by going on a page, the key will be attributes to the user.

For that, I want to check all orders made by the current user (the on log-in and on the page) and check the game he has bought.

I find some very usefull information here: How to get WooCommerce order details

However, I don't manage to get all orders the current user. I have first think to make a SQL request, but I don't find the link on the database between order and user.

Have you any lead?

like image 305
Mathieu Roux Avatar asked Feb 14 '17 10:02

Mathieu Roux


People also ask

What is the difference between product and order ID in WooCommerce?

In WooCommerce the product is just a custom post type like any other custom post type in WordPress. These products and orders are assigned page IDs like any other WordPress post type. The order ID is the unique number that is assigned to the order once it is created for identification and reuse in various other WooCommerce functions.

How to get $order in WooCommerce using WC_get_order?

In that scenario, you can “get” the order object with the wc_get_order WooCommerce function. It’s also possible to gain $order information if you are in an email template. This can be helpful to show additional $order information in your transactional communications or trigger custom functions. Either way, enjoy! 1.

Is it possible to display discounted price in WooCommerce email?

Thanks Yes, it’s possible. Do you need to send a custom, additional email – or to customize an existing order email? Hi, I am trying to put a unit price of each item before quantity column on woocommerce email. Below is what I have done and it displays the unit price but if the item is discounted, this will show the discounted price.

What is the Order ID?

The order ID is the unique number that is assigned to the order once it is created for identification and reuse in various other WooCommerce functions.


1 Answers

Updated Added Compatibility with WooCommerce 3+ (January 2018)

Here is the code that you will need to get all customer orders and to go through each items of each customer order:

## ==> Define HERE the statuses of that orders 
$order_statuses = array('wc-on-hold', 'wc-processing', 'wc-completed');

## ==> Define HERE the customer ID
$customer_user_id = get_current_user_id(); // current user ID here for example

// Getting current customer orders
$customer_orders = wc_get_orders( array(
    'meta_key' => '_customer_user',
    'meta_value' => $customer_user_id,
    'post_status' => $order_statuses,
    'numberposts' => -1
) );


// Loop through each customer WC_Order objects
foreach($customer_orders as $order ){

    // Order ID (added WooCommerce 3+ compatibility)
    $order_id = method_exists( $order, 'get_id' ) ? $order->get_id() : $order->id;

    // Iterating through current orders items
    foreach($order->get_items() as $item_id => $item){

        // The corresponding product ID (Added Compatibility with WC 3+) 
        $product_id = method_exists( $item, 'get_product_id' ) ? $item->get_product_id() : $item['product_id'];

        // Order Item data (unprotected on Woocommerce 3)
        if( method_exists( $item, 'get_data' ) ) {
             $item_data = $item->get_data();
             $subtotal = $item_data['subtotal'];
        } else {
             $subtotal = wc_get_order_item_meta( $item_id, '_line_subtotal', true );
        }

        // TEST: Some output
        echo '<p>Subtotal: '.$subtotal.'</p><br>';

        // Get a specific meta data
        $item_color = method_exists( $item, 'get_meta' ) ? $item->get_meta('pa_color') : wc_get_order_item_meta( $item_id, 'pa_color', true );

        // TEST: Some output
        echo '<p>Color: '.$item_color.'</p><br>';
    }
} 

This code is tested and works


Related:

  • How to get WooCommerce order details
  • Accessing Order Items protected data in Woocommerce 3
  • Get Order items and WC_Order_Item_Product in WooCommerce 3
like image 100
LoicTheAztec Avatar answered Oct 27 '22 11:10

LoicTheAztec