Page 1 of 1
How to show the number of items sold
Posted: Tue Aug 15, 2017 5:06 am
by markjohnson
How to show the number of items sold in product page in OpenCart v3? Unfortunately, I don't have any knowledge in php or twig. I tried to find an extension as well, but no luck.
Re: How to show the number of items sold
Posted: Tue Aug 15, 2017 11:59 am
by IP_CAM
If it does not exist, so, you'll have to get it made, if you're in a hurry,
or then wait, until possibly someone will create an Extension, one day ...
Good Luck
Ernie
Re: How to show the number of items sold
Posted: Tue Aug 15, 2017 2:00 pm
by Qphoria
The query you could use on the product page is
Code: Select all
$data['sold_count'] = $this->db->query("SELECT SUM(op.quantity) AS quantity FROM " . DB_PREFIX . "order_product op LEFT JOIN `" . DB_PREFIX . "order` o ON (op.order_id = o.order_id) WHERE o.order_status_id > '0' AND op.product_id = '" . (int)$product_info['product_id'] . "' GROUP BY op.product_id")->row['quantity'];
Then you'd do
<?php echo $sold_count; ?>
on the tpl or whatever the twig format is.
Re: How to show the number of items sold
Posted: Fri Oct 26, 2018 2:28 am
by cmsroom
Re: How to show the number of items sold
Posted: Fri Oct 26, 2018 4:02 am
by kestas
How to show the number of items sold in product page in OpenCart v3? Unfortunately, I don't have any knowledge in php or twig. I tried to find an extension as well, but no luck.
Hi
@markjohnson You can just say thank's Qphoria... and me

. Download attached extension and count products which are sold...
After installing this ocmod extension do not forget refresh modifications...
Cheers
I slightly edited Qphoria code, because when the product is not yet sold, we get an error.
Re: How to show the number of items sold
Posted: Tue Jul 06, 2021 7:22 pm
by fredJ
Qphoria wrote: ↑Tue Aug 15, 2017 2:00 pm
The query you could use on the product page is
Code: Select all
$data['sold_count'] = $this->db->query("SELECT SUM(op.quantity) AS quantity FROM " . DB_PREFIX . "order_product op LEFT JOIN `" . DB_PREFIX . "order` o ON (op.order_id = o.order_id) WHERE o.order_status_id > '0' AND op.product_id = '" . (int)$product_info['product_id'] . "' GROUP BY op.product_id")->row['quantity'];
Then you'd do
<?php echo $sold_count; ?>
on the tpl or whatever the twig format is.
I know this is an old thread but I used this snippet and it works as expected, however, I keep getting the error
Undefined index: quantity
for the variable "->row['quantity'] most likely. Maybe when there are no results.
And this is cluttering my error log. I've tried lots of things but I can't make it go away. (try / catch, etc)
Please, is there some simple way? I just want to avoid the error. but I can't figure out where this code ->row['quantity'] , where does "row" come from?
Re: How to show the number of items sold
Posted: Wed Feb 07, 2024 2:04 am
by fogma
Qphoria wrote: ↑Tue Aug 15, 2017 2:00 pm
The query you could use on the product page is
Code: Select all
$data['sold_count'] = $this->db->query("SELECT SUM(op.quantity) AS quantity FROM " . DB_PREFIX . "order_product op LEFT JOIN `" . DB_PREFIX . "order` o ON (op.order_id = o.order_id) WHERE o.order_status_id > '0' AND op.product_id = '" . (int)$product_info['product_id'] . "' GROUP BY op.product_id")->row['quantity'];
Then you'd do
<?php echo $sold_count; ?>
on the tpl or whatever the twig format is.
Thank you - worked perfectly first time :-)
Re: How to show the number of items sold
Posted: Thu Feb 08, 2024 1:01 am
by paulfeakins
markjohnson wrote: ↑Tue Aug 15, 2017 5:06 am
How to show the number of items sold in product page in OpenCart v3? Unfortunately, I don't have any knowledge in php or twig. I tried to find an extension as well, but no luck.
If you can't find an extension, you could pay a developer such as
ourselves or post a job in the
Commercial Support Forum.
Re: How to show the number of items sold
Posted: Wed Mar 13, 2024 5:59 am
by fogma
Actually.... it does not work perfectly :-)
If you've not sold any of a product, then you'll get an entry in your error log as fredJ says.
So I've changed my code to:
Code: Select all
$sold_count_query = $this->db->query("SELECT SUM(op.quantity) AS quantity FROM " . DB_PREFIX . "order_product op LEFT JOIN `" . DB_PREFIX . "order` o ON (op.order_id = o.order_id) WHERE o.order_status_id > '0' AND op.product_id = '" . (int)$product_info['product_id'] . "' GROUP BY op.product_id");
if ($sold_count_query->num_rows == 1)
{
$data['sold_count'] = $sold_count_query->row['quantity'];
}
else
{
$data['sold_count'] = 0;
}
Re: How to show the number of items sold
Posted: Wed Mar 13, 2024 6:00 am
by fogma
Corrected OCMod file