In woocommerce, how to check how many times a product has been purchased by a customer throughout an order.
How can I check how many times a product is bought by a customer throughout an order.
An example of a current customer’s product purchase history:
Product one = bought 5 times
Product five = bought 1 times
Product four = bought 2 times
Product two = bought 3 times
Product three = bought 6 times
I have a function to check whether this product is bought by a customer or not
function is_bought_items() {
$bought = false;
$_options = get_option( 'license_page_option_name' );
$ex_product_ids = $_options['ex_product_ids_warranty'];
$targeted_products= explode(",",$ex_product_ids);
$customer_orders = get_posts( array(
'numberposts' => -1,
'meta_key' => '_customer_user',
'meta_value' => get_current_user_id(),
'post_type' => 'shop_order',
'post_status' => 'wc-completed'
) );
foreach ( $customer_orders as $customer_order ) {
$order = wc_get_order( $customer_order );
$order_id = $order->id;
$items = $order->get_items();
foreach ($items as $item) {
if ( in_array( $item['product_id'], $targeted_products) ) {
$bought = true;
}
}
}
if ( $bought ) {
return true;
}
}
source
share