Ask your WordPress questions! Pay money and get answers fast! (more info)

Need to find customers who bought certain products (Woocommerce)

In Woocommerce today, I have no way of seeing which customers bought what, in a nice overview.

But for some products that we need to withdraw from customers (who have already bought them), I need a small script that can give me this information.

[[LINK href="https://www.dropbox.com/s/dwmgv28ez2t60di/Screenshot%202016-01-25%2016.39.22.png?dl=0"]]I am thinking something like this[[/LINK]]

I should be able to place this script on our webspace when I need it, and then I can remove it again afterwards, so no security or login is needed for this right now.

So basically the script should give me every order made in Woocommerce for a specific product, presented in the same way that the above screenshot shows.

Answers (3)

2016-01-25

Rempty answers:

View This
http://codecanyon.net/item/woocommerce-customers-manager/10965432

2016-01-25

Arnav Joy answers:

Hi ,

This is definitely doable but it requires coding ..

I have sent you pm , please check.

2016-01-25

Reigel Gallarde answers:

hello...

try adding this to your functions.php in your theme... "Custom Search Order" menu will appear under "WooCommerce" menu..


add_action('admin_menu', 'mt_add_pages', 10);

function mt_add_pages() {
add_submenu_page(
'woocommerce',
__('Custom Search Order','woocommerce'),
__('Custom Search Order','woocommerce'),
'manage_options',
'search_order',
'mt_sublevel_page'
);
}


function mt_sublevel_page() {

$product_id = !empty($_GET['product_id'])?$_GET['product_id']:'';

?>
<h3><?php echo __( 'Custom Search Order', 'woocommerce' ); ?></h3>

<form method="get">
<input type="hidden" name="page" value="<?php echo $_GET['page']?>" />
<label><?php echo __( 'Product Search', 'woocommerce' ); ?></label>
<input type="text" name="product_id" id="product_id" value="<?php echo $product_id;?>" />
</form>

<h3><?php echo __( 'Result for', 'woocommerce' ); ?></h3>

<?php

$product = new WC_Product($product_id);
if ($product) {
echo '<h4>'. $product->get_title() . '</h4>';
echo '<h4>SKU'. $product->get_sku() . '</h4>';

$orders = rei_get_orders_by_product_id($product_id);

// The Loop
if ( !empty($orders) ) {
echo '<table border="1">';
echo '<thead><tr>';
echo '<th>Date</th>';
echo '<th>Customer Name</th>';
echo '<th>Address</th>';
echo '<th>Zip</th>';
echo '<th>Phone</th>';
echo '<th>Email</th>';
echo '</tr></thead>';
foreach ( $orders->posts as $order_id ) {
$order = new WC_Order( $order_id );


// Formatted Addresses.
$address = array(
'first_name' => $order->billing_first_name,
'last_name' => $order->billing_last_name,
'company' => $order->billing_company,
'address_1' => $order->billing_address_1,
'address_2' => $order->billing_address_2,
'city' => $order->billing_city,
'state' => $order->billing_state,
'postcode' => $order->billing_postcode,
'country' => $order->billing_country
);

$formatted_billing_address = WC()->countries->get_formatted_address( $address );

echo '<tr>';
echo sprintf('<td>%s</td>' , $order->order_date);
echo sprintf('<td>%s %s</td>' , $order->billing_first_name, $order->billing_last_name);
echo sprintf('<td>%s</td>' , $formatted_billing_address);
echo sprintf('<td>%s</td>' , $order->billing_postcode);
echo sprintf('<td>%s</td>' , $order->billing_phone);
echo sprintf('<td>%s</td>' , $order->billing_email);

echo '</tr>';
}
echo '</tbody></table>';
}
} else {
echo '<h3>No Result</h3>';
}

}

function rei_get_orders_by_product_id($product_id){
global $wpdb;
$tabelaOrderItemMeta = $wpdb->prefix . 'woocommerce_order_itemmeta';
$tabelaOrderItems = $wpdb->prefix . 'woocommerce_order_items';

$results = $wpdb->get_results(
$wpdb->prepare(
"SELECT b.order_id
FROM {$tabelaOrderItemMeta} a, {$tabelaOrderItems} b
WHERE a.meta_key = '_product_id'
AND a.meta_value = %s
AND a.order_item_id = b.order_item_id
ORDER BY b.order_id DESC",
$product_id
)
);

if($results)
{
$order_ids = array();

foreach($results as $item)
array_push($order_ids, $item->order_id);

if($order_ids)
{
$args = array(
'post_type' =>'shop_order',
'post_status' => array( 'wc-processing', 'wc-completed' ),
'posts_per_page' => -1,
'post__in' => $order_ids,
'fields' => 'ids'
);
$query = new WP_Query($args);

return $query;
}
}
}


Reigel Gallarde comments:

have you tried this?