3

I have a large mysql table (in woocommerce) where people can purchase items more than once. What I would like to do is have a table dynamically generate where I select the product I want to query for (from a dropdown menu in php if possible) and group the quantity of the product purchased by user_id. I found this code, but have been unable to generate the table I would like to get. here is my current test code to try and make it work but no table is generated;

<?php 
if (!is_user_logged_in() || !current_user_can('manage_options')) wp_die('This page is private.');

$con=mysqli_connect("ip","user","pass","database");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,
"select p.user_id,
       max( CASE WHEN pm.meta_key = '_billing_first_name' and p.ID = pm.post_id THEN pm.meta_value END ) as _billing_first_name,
       max( CASE WHEN pm.meta_key = '_billing_last_name' and p.ID = pm.post_id THEN pm.meta_value END ) as _billing_last_name,
       max( CASE WHEN pm.meta_key = '_billing_address_1' and p.ID = pm.post_id THEN pm.meta_value END ) as _billing_address_1,
       group_concat(distinct oi.order_item_name separator '|' ) as order_items
from wp_posts p join
    wp_postsmeta pm
    on p.ID = pm.post_id join
    wp_woocommerce_order_items oi
where p.post_type = 'shop_order' and
      p.post_status = 'wc-completed' and
      oi.order_item_name = 'Product Name'
group p.user_id");
echo "<table>";
while($row = mysqli_fetch_array($result))
{
echo "<tr style='font-size: 0.665em'>";
echo "<td>"  . $row['billing_first_name'] . "</td>";
echo "<td>" . $row['_billing_last_name'] . "</td>";
echo "<td>" . $row['billing_address_1'] . "</td>";
echo "</tr>";
}
echo "</table>";
?>

my ultimate goal would be this with a dropdown menu to select product in question based on product ID so I can see how much each user has purchased of the product queried (completed orders only as per code above);

user ID | Product | quantity | billing address 
23      | chair   | 4        | 22 Bank street
42      | chair   | 12       | 123 Smith Road
88      | chair   | 5        | 3 Parker avenue

etc

NRav
  • 407
  • 1
  • 6
  • 18

1 Answers1

0

I'm not sure why you are using a subquery in the select. You are also selecting columns that you don't want/need.

Does this do what you want?

select p.user_id,
       max( CASE WHEN pm.meta_key = '_billing_first_name' and p.ID = pm.post_id THEN pm.meta_value END ) as _billing_first_name,
       max( CASE WHEN pm.meta_key = '_billing_last_name' and p.ID = pm.post_id THEN pm.meta_value END ) as _billing_last_name,
       max( CASE WHEN pm.meta_key = '_billing_address_1' and p.ID = pm.post_id THEN pm.meta_value END ) as _billing_address_1,
       group_concat(distinct oi.order_item_name separator '|' ) as order_items
from wp_posts p join
     wp_postmeta pm
     on p.ID = pm.post_id join
     wp_woocommerce_order_items oi
     on p.ID = oi.order_id
where p.post_type = 'shop_order' and
      p.post_status = 'wc-completed' and
      oi.order_item_name = 'Product Name'
group by p.user_id;

I had to speculate about where some columns come from, because your question is not explicit.

Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786
  • unfortunately it did not work; I have updated my question with the php code I used with the table creation – NRav Jun 09 '18 at 15:52