aschmelyun
aschmelyun

Reputation: 13

How can I find the product a customer ordered in a WooCommerce database?

I know that the orders reside in the wp_posts table, with the post_type of shop_order. And the wp_postmeta table holds the information about each order such as name, address, status, etc. However, what I'm missing is the item that the user ordered.

I can't find what the relationship is in the database to pull that info.

Could someone point me in the right direction?

Upvotes: 1

Views: 3025

Answers (3)

XciD
XciD

Reputation: 2617

Orders are stored as posts as you said.

For products, you have to go on wp_woocommerce_order_items and wp_woocommerce_order_itemmeta

There is an example of join

inner join wp_woocommerce_order_items as woi on ( woi.order_id = posts.ID )
inner join wp_woocommerce_order_itemmeta as woim on ( woim.order_item_id = woi.order_item_id )

Upvotes: 0

helgatheviking
helgatheviking

Reputation: 26319

Why not let WooCommerce get the items for you?

// create the order object
$order = wc_get_order( $order_id );
// retrieve the items associated with that order
$order_items = $order->get_items();
// dump the array of returned  items
var_dump($order_items);

Upvotes: 1

vaso123
vaso123

Reputation: 12391

If i understood your question this is what you need:

$sql = "SELECT * FROM wp_posts "
    . " INNER JOIN wp_postmeta ON wp_posts.id = wp_postmeta.post_id"
    . " WHERE wp_posts.ID = " . $postId;

Then see the wp_postmeta keys.

Upvotes: 1

Related Questions