Count Total Sold Products

I use this to count the total number of sold product, the problem is that it doesn't count sold product with the complete status only processed, how can this be fixed? thanks
function fn_product_sold($product_id, $order_status = 'P') {
$count = (int)db_get_field('SELECT SUM(a.amount) FROM ?:order_details a
LEFT JOIN ?:orders b ON a.order_id = b.order_id
WHERE a.product_id = ?i AND b.status = ?s', $product_id, $order_status);
  return $count;
}

Try

function fn_product_sold($product_id, $order_status = 'P') {
    $count = (int)db_get_field('SELECT SUM(a.amount) FROM ?:order_details a
    LEFT JOIN ?:orders b ON a.order_id = b.order_id
    WHERE a.product_id = ?i AND b.status IN ?a', $product_id, [$order_status, 'C']);
    return $count;
}

I tried this but it's not working

Try

function fn_product_sold($product_id, $order_status = 'P') {
    $count = (int)db_get_field('SELECT SUM(a.amount) FROM ?:order_details a
    LEFT JOIN ?:orders b ON a.order_id = b.order_id
    WHERE a.product_id = ?i AND b.status IN ?a', $product_id, [$order_status, 'C']);
    return $count;
}

I tried this but it's not working

Do you receive any error? Or empty result?

Do you receive any error? Or empty result?

Empty value, I ended up doing it this way..

function fn_product_sold($product_id, $order_status = 'C') {
    $count = (int)db_get_field('SELECT SUM(a.amount) FROM ?:order_details a
    LEFT JOIN ?:orders b ON a.order_id = b.order_id
    WHERE a.product_id = ?i AND b.status IN ?a', $product_id, [$order_status, 'C']);
    return $count;
}

function fn_product_sold($product_id, $order_status = ‘P’) {
$count = (int)db_get_field(‘SELECT SUM(a.amount) FROM ?:order_details a
LEFT JOIN ?:orders b ON a.order_id = b.order_id
WHERE a.product_id = ?i AND b.status IN ?a’, $product_id, [$order_status, ‘C’]);
return $count;
}

on tpl
{$complete_orders = $product.product_id|complete_orders_count}
{$paid_orders = $product.product_id|paid_orders_count}
{$sold_amt = $complete_orders+$paid_orders}

Please make sure that the required status uses the "C" letter

Please make sure that the required status uses the "C" letter

yes it does

As far as I can see, your code does not call my function.