Which is the correlation between an ORDER and the USER who bought in WooCommerce/WordPress database

I need to know if a user has bought a WooCommerce product. And if is it possible how many order he did.

So I checked in the WordPress database, and find the “wp_woocommerce_order_items” table.

Read More

here I can find these data
enter image description here

The order_item_id field is used to join this table with the products meta. I don’t find the reason for the order_id.

And I can’t understand how I can find the user who made the order.

enter image description here

As you can see in the second picture (wp_usermeta table) I don’t have no one field common with the order table.

So which is the common filed to join this 2 table?

I need only the field name that contain this info/value.

Virtual hugs for those who help me!

Related posts

2 comments

  1. I just found the solutions for my own question! Thanks to everyone who answer me, you all helped me find the solution!

    I didn’t know how WooCommerce save the order.

    I just discover that the orders are saved using a post_type called shop_order.

    So basically I find the user ID in the shop_order metas in wp_postmeta!

    Here some screen with the SQL queries I did:

    SELECT * FROM wp_posts WHERE post_type = ‘shop_order’

    enter image description here

    SELECT * FROM wp_postmeta WHERE post_id = 1949

    enter image description here

    Thank you all!!

  2. Try this

    $customer_orders = get_posts( 
        array(
            'numberposts' => $order_count,
            'meta_key'    => '_customer_user',
            'meta_value'  => get_current_user_id(),
            'post_type'   => wc_get_order_types( 'view-orders' ),
            'post_status' => array_keys( wc_get_order_statuses() )
        )
    );
    

    This is basically from the template file my-orders. (woocommerce/templates/myaccount/my-orders.php). You can customize this code for your requirement. This gets you orders for the customer. You will need to change get_current_user_id() with the desired customers user id.

    Hope this helps.

Comments are closed.