How to complement these queries to get the buyer’s data in the bank?

Asked

Viewed 180 times

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:

inserir a descrição da imagem aqui

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 ...

inserir a descrição da imagem aqui

  • wp_posts is a view? you can’t make a view just for this?

  • @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.

  • 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.

  • 3

    For what you said would do everything you want with just a query, Some difficulty with JOIN ?

  • @Fábiopaiva I have no experience with JOIN but it sure can be done with a single consultation.

  • That would be +- like this SELECT ID FROM wp_posts as p&#xA;join wp_woocommerce_order_items oi&#xA;on oi.order_id = p.id&#xA;WHERE p.post_type = 'shop_order' , but as I said see the possibility of being a View, if the Bank accepts.

Show 1 more comment

1 answer

3

Example of JOIN

$sqlSelect  = 'SELECT pt.ID, woi.*, cli.* '
          . 'FROM wp_posts pt '
          . 'INNER JOIN wp_woocommerce_order_items woi ON (woi.order_id = pt.id) '
          . 'INNER JOIN wp_nome_tabela_clientes cli ON (cli.id = pt.cliente_id) '
          . 'WHERE pt.post_type = "shop_order" ';
  • Dude I was just looking at the code of your answer here and I don’t think it helps me because you ignored the main part of the question, the table wp_postmeta containing all customer data per order ID.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.