May this code will helpful for you. Ihave use a code to get the current logged in users completed (or you may use your status) order data. Then get their product count, now we can obtain the product which will purchased before. Just gone through this code.
$customer_orders = get_posts( array(
'numberposts' => - 1,
'meta_key' => '_customer_user',
'meta_value' => get_current_user_id(),
'post_type' => 'shop_order', // WC orders post type
'post_status' => 'wc-completed' // Only orders with status "completed"
//use status such as "wc-processing"https://wordpress.stackexchange.com/"wc-on-hold"etc.
) );
$previous_count = 0;
// Going through each current customer orders
foreach ( $customer_orders as $customer_order ) {
$order = wc_get_order( $customer_order );
$items = $order->get_items();
// Going through each current customer products bought in the order
foreach ( $items as $item ) {
$id = $item['product_id'];
// If product not in array, add it
if ( ! array_key_exists( $item['product_id'], $products ) ) {
$products[ $id ] = array(
'name' => $item['name'],
'count' => 0,
);
}
// Increment Product <code>count</code> from cart quantity
//$products[ $id ]['count'] += $item->get_quantity();// get the total product
$previous_count += $item->get_quantity();
}
}
$cart_count = WC()->cart->get_cart_contents_count();
$remaining_product_count = $previous_count+$cart_count;
if($remaining_product_count < 10){
$remaining_product_count = 10-$remaining_product_count;
echo $remaining_product_count;
}