Page 1 of 2

How to show the number of items sold

Posted: Thu Mar 03, 2011 11:28 pm
by dim565
Hello.
I have to do on the page product/product... was displayed - how many times bought this product (ie, the status of "complete transaction"). Help me please do so

Re: How to show the number of items sold

Posted: Fri Mar 04, 2011 1:12 am
by scanreg
This would be interesting, bookmarked

Re: How to show the number of items sold

Posted: Fri Mar 04, 2011 1:43 am
by SXGuy
you would need to write an sql query on the product model file which counts how many rows there are which had the current product id listed, in the orders table i think, or maybe sales, im not sure.

then you could pull the retreived number on the product view file.

Re: How to show the number of items sold

Posted: Fri Mar 04, 2011 2:49 am
by dim565
SXGuy wrote:you would need to write an sql query...
Difficult for me. Is there an easier solution?

Re: How to show the number of items sold

Posted: Fri Mar 04, 2011 3:31 am
by openmycart.com
there is already product quantity, You just need to minus product quantity with last quantity

Re: How to show the number of items sold

Posted: Fri Mar 04, 2011 12:36 pm
by dim565
and whether there is a variable, where the initial value of quantity??

Re: How to show the number of items sold

Posted: Sat Mar 05, 2011 12:53 pm
by dim565
Somebody can give a working complete solution?

Re: How to show the number of items sold

Posted: Sat Mar 05, 2011 6:29 pm
by fido-x
Let's see, the order_product table stores the products that have sold, the order table records the order status. So, you need to count the products in the order_products table based on the product_id where the order status in the order table is set to "complete". Easy!

Insert the following function into "catalog/model/catalog/product.php":

Code: Select all

public function getUnitsSold($product_id) {
    $query = $this->db->query("SELECT COUNT(*) AS total FROM `" . DB_PREFIX . "order_product` op LEFT JOIN `" . DB_PREFIX . "order` o ON (op.order_id = o.order_id) WHERE o.order_status_id = '5' AND op.product_id = '" . (int)$product_id . "'");
    if ($query->row) {
        return $query->row['total'];
    } else {
        return FALSE;
    }
} 
Insert the following into "catalog/language/english/product/product.php":

Code: Select all

$_['text_units_sold'] = 'Units Sold:'; 
Insert the following into "catalog/controller/product/product.php":

Code: Select all

$this->data['text_units_sold'] = $this->language->get('text_units_sold');
$this->data['units_sold'] = $this->model_catalog_product->getUnitsSold($product_id); 
before (line 157):

Code: Select all

$this->load->model('catalog/review'); 
Then insert the following into "catalog/view/theme/default/template/product/product.tpl":

Code: Select all

<?php if ($units_sold) { ?>
<tr>
  <td><b><?php echo $text_units_sold; ?></b></td>
  <td><?php echo $units_sold; ?></td>
</tr>
<?php } ?>
before (around line 35 in the default):

Code: Select all

<?php if ($manufacturer) { ?>

Re: How to show the number of items sold

Posted: Sat Mar 05, 2011 7:21 pm
by dim565
Many thanks for your reply. But I wanted to print the number of purchases on the page, product/category ... (after the price of goods)

Please, if you have free time, please tell us - how to convert code to embed the page /catalog/view/theme/default/template/product/category.tpl ?

Re: How to show the number of items sold

Posted: Sat Mar 05, 2011 8:11 pm
by fido-x
Ummh, you said in your first post:
dim565 wrote:Hello.
I have to do on the page product/product... was displayed - how many times bought this product (ie, the status of "complete transaction"). Help me please do so
Now, you're saying:
dim565 wrote:Many thanks for your reply. But I wanted to print the number of purchases on the page, product/category ... (after the price of goods)

Please, if you have free time, please tell us - how to convert code to embed the page /catalog/view/theme/default/template/product/category.tpl ?
???

OK. As well as the aforementioned changes, do the following:
First of all, put the model function into "catalog/model/catalog/category.php", then --

Insert the following into "catalog/language/english/product/category":

Code: Select all

$_['text_units_sold'] = 'Units Sold:';  
Insert the following into "catalog/controller/product/category.php":

Code: Select all

$this->data['text_units_sold'] = $this->language->get('text_units_sold'); 
after (line 58):

Code: Select all

$this->data['text_sort'] = $this->language->get('text_sort'); 
Then, same file, insert the following:

Code: Select all

'units_sold' => $this->model_catalog_category->getUnitsSold($result['product_id']), 
into the "products" array (around line 170).

And then, insert the following into "catalog/view/theme/default/template/product/category.tpl":

Code: Select all

<?php if ($products[$j]['units_sold']) { ?>
<br />
<?php echo $text_units_sold; ?>: <?php echo $products[$j]['units_sold']; ?>
<?php } ?>
somewhere around line 60/61.

Re: How to show the number of items sold

Posted: Sat Mar 05, 2011 8:52 pm
by dim565
fido-x, sorry - I was wrong in the first message

Thank you for your help, everything works fine!
:) :) :)

Re: How to show the number of items sold

Posted: Sat Mar 05, 2011 10:48 pm
by i2Paq
Would it not be great if this was a vQmod? 8)

Re: How to show the number of items sold

Posted: Sun Mar 06, 2011 12:38 am
by dim565
i2Paq wrote:was a vQmod? 8)
Sorry, but i don't know - what is vQmod

Re: How to show the number of items sold

Posted: Mon Mar 07, 2011 10:34 pm
by dim565
i2Paq, Hello. Noticed this: if I order a single item in multiple quantities, they are not included in your code

ie I have ordered the goods in the "quantity " wrote the 5 units. And in your code (<?php echo $products[$j]['units_sold'];) number increases by only 1.
Can fix it?

I truid to change code:

Code: Select all

public function getUnitsSold($product_id) {
    $query = $this->db->query("SELECT COUNT(*) AS total FROM `" . DB_PREFIX . "order_product` op LEFT JOIN `" . DB_PREFIX . "order` o ON (op.order_id = o.order_id) WHERE o.order_status_id = '5' AND op.product_id = '" . (int)$product_id . "'");
    if ($query->row) {
        return $query->row['total'];
    } else {
        return FALSE;
    }
} 
but nothing happened :(

Re: How to show the number of items sold

Posted: Wed Mar 09, 2011 3:46 pm
by fido-x
My bad. There's a mistake in the db query. Change the query to:

Code: Select all

$query = $this->db->query("SELECT SUM(op.quantity) AS total 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_id . "'"); 
The only difference is the change from "COUNT(*)" to "SUM(op.quantity)".
i2Paq wrote:Would it not be great if this was a vQmod? 8)
viewtopic.php?f=131&t=28362

Re: How to show the number of items sold

Posted: Wed Mar 09, 2011 7:06 pm
by dim565
fido-x, Thank you very much! :) :)

P.S In you code i change

Code: Select all

> '0'
to

Code: Select all

= '5'

Re: How to show the number of items sold

Posted: Sat Mar 12, 2011 10:37 pm
by dim565
Hello.
If the goods never purchased, the meter does not show 0, can we fix it?

Re: How to show the number of items sold

Posted: Tue Mar 15, 2011 12:11 pm
by dim565
Well, at least tell me the place where to insert the test condition on 0

Re: How to show the number of items sold

Posted: Tue Mar 15, 2011 3:56 pm
by fido-x
dim565 wrote:If the goods never purchased, the meter does not show 0, can we fix it?
Sorry, I don't understand what you mean.

Re: How to show the number of items sold

Posted: Tue Mar 15, 2011 7:54 pm
by dim565
Sorry, the solution was simple. Instead

Code: Select all

<?php echo $products[$j]['units_sold']; ?>
it was necessary to write:

Code: Select all

<?php if ($products[$j]['units_sold']) { ?>
<?php echo $products[$j]['units_sold']; ?>
<?php } else echo "0" ?>