Post by markjohnson » 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.

Newbie

Posts

Joined
Tue Aug 15, 2017 5:04 am

Post by IP_CAM » Tue Aug 15, 2017 11:59 am

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

My Github OC Site: https://github.com/IP-CAM
5'200 + FREE OC Extensions, on the World's largest private Github OC Repository Archive Site.


User avatar
Legendary Member

Posts

Joined
Tue Mar 04, 2014 1:37 am
Location - Switzerland

Post by Qphoria » 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.

Image


User avatar
Administrator

Posts

Joined
Tue Jul 22, 2008 3:02 am

Post by cmsroom » Fri Oct 26, 2018 2:28 am


You may like these extension : - https://www.opencart.com/index.php?rout ... r=cmsrooms


New member

Posts

Joined
Fri Sep 28, 2018 1:37 am

Post by kestas » Fri Oct 26, 2018 4:02 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.
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.

Attachments

sold out count


Custom OpenCart modules and solutions. You can write PM with additional questions... Extensions you can find here


Active Member

Posts

Joined
Tue Oct 12, 2010 2:23 am

Post by fredJ » Tue Jul 06, 2021 7:22 pm

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?

New member

Posts

Joined
Fri Jan 18, 2019 11:10 pm

Post by fogma » Wed Feb 07, 2024 2:04 am

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 :-)

New member

Posts

Joined
Wed Aug 13, 2008 4:16 am

Post by paulfeakins » Thu Feb 08, 2024 1:01 am

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.

UK OpenCart Hosting | OpenCart Audits | OpenCart Support - please email info@antropy.co.uk


User avatar
Guru Member

Posts

Joined
Mon Aug 22, 2011 11:01 pm
Location - London Gatwick, United Kingdom

Post by fogma » Wed Mar 13, 2024 5:59 am

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;
}

New member

Posts

Joined
Wed Aug 13, 2008 4:16 am

Post by fogma » Wed Mar 13, 2024 6:00 am

Corrected OCMod file

New member

Posts

Joined
Wed Aug 13, 2008 4:16 am
Who is online

Users browsing this forum: No registered users and 368 guests