1
I made these queries below to get the data from the buyer’s database and the products he bought. With the first query, I got the ID and with these IDS I made a new foreach
that took the products that each ID acquired.
What I needed now was to compare the ID order with other table containing the customer data having that determined ID. This table is with 99% chance this one >>> wp_postmeta. We are talking about Wordpress but the query can be SQL native even.
I’ll show you what I did and what I got:
global $wpdb;
$sqlSelect = "SELECT ID FROM wp_posts WHERE post_type = 'shop_order'";
/* Buscando os IDS da Compra */
$pid = array();
foreach ($wpdb->get_results($sqlSelect) as $dados) {
$pid[] .= $dados->ID;
}
/* Buscando os produtos comprados pelo ID */
foreach ($pid as $uid) {
echo '<strong>Número do pedido: ' . $uid . "</strong><br>";
$sqlDados = "SELECT * FROM wp_woocommerce_order_items WHERE order_id = '$uid'";
foreach ($wpdb->get_results($sqlDados) as $itens) {
echo $itens->order_item_name . '<br>';
}
echo '<br>';
}
This query returned me this:
Now all I need to do is return the customer’s data to be able to do my program. You can help me?
Table preview wp_postmeta
...
wp_posts is a view? you can’t make a view just for this?
– Marco Souza
@Marconciliosouza has nothing to do with view, I’m storing data to later make a "system" on top of this data. Wp_posts is just a table that registered the request.
– Marcos Vinicius
the title of the question.
Como complementar estas consultas para pegar os dados do comprador no banco?
now if you are storing should change the question.– Marco Souza
For what you said would do everything you want with just a query, Some difficulty with JOIN ?
– Fábio Paiva
@Fábiopaiva I have no experience with JOIN but it sure can be done with a single consultation.
– Marcos Vinicius
That would be +- like this
SELECT ID FROM wp_posts as p
join wp_woocommerce_order_items oi
on oi.order_id = p.id
WHERE p.post_type = 'shop_order'
, but as I said see the possibility of being aView
, if the Bank accepts.– Marco Souza