6 replies [Last post]
mimetic2's picture
Offline
Joined: 08/07/2007
Juice: 478
Was this information Helpful?

Hello,

Is there a way to show my users how many times something has been purchased?

I'd like to make it so that on the product it says "been purchased x amount of times" and of course it should update automatically eveyrtime someone purchases the product.

How would i go about doing this?

mimetic2's picture
Offline
Joined: 08/07/2007
Juice: 478
Re: How to show how many times a product has been purchased?

bump

Ryan's picture
Offline
Joined: 08/07/2007
Juice: 15438
Re: Re: How to show how many times a product has been purchased?

You'd have to use a PHP snippet that got that data from the uc_order_products table in the database... it'll be a little tricky since you have to weed out orders with invalid order statuses.

TR
TR's picture
Offline
Bug FinderFAQ ModeratorGetting busy with the Ubercode.
Joined: 11/05/2007
Juice: 3424
Re: Re: Re: How to show how many times a product has been purcha

There have been a number of previous posts that have asked this question, and some of those posts include the PHP needed to get and display this information.

<tr>.
507 Media Studios's picture
Offline
Joined: 07/04/2008
Juice: 35
[SOLVED] PHP function to pull data from uc_order_products table

This is a PHP function that was placed on the sites template.php file to pull the data from the uc_order_products table, I know this is an old posting, but I needed to do this and didn't find a solution and maybe someone can use it in the future.

Step 1: Create a PHP function in your template.php in your default theme like so:

<?php
/**
  * This is used to display per node the total amount of purchases of each product
  */
function oferta_total_purchases(&$node){
   
$result = db_query("SELECT node.nid AS nid, SUM( uc_order_products.qty ) AS uc_order_products_qty FROM node node LEFT JOIN uc_order_products uc_order_products ON node.nid = uc_order_products.nid LEFT JOIN uc_orders uc_orders ON uc_order_products.order_id = uc_orders.order_id WHERE node.nid = %d AND (uc_orders.order_status IN ('completed'))", $node->nid);
   
// nid = %d', $node->nid
   
while ( $obj = db_fetch_object ($result) ) {
        if (
$obj->uc_order_products_qty == 0){
            print
'0';
        }
        elseif (
$obj->uc_order_products_qty !== 0){
            print
$obj->uc_order_products_qty;
        }
    }
}
?>

Step 2: Call the function from a node.tpl.php file like so:

<?php
 
print oferta_total_purchases(&$node);
?>
507 Media Studios's picture
Offline
Joined: 07/04/2008
Juice: 35
How to show an aggregated value of product count

I really need it to show the aggregated value of the product count from the table uc_order_products.qty on all nodes that were translated as well as the source. I was able to accomplished such thing modifying the above code I posted by using the following code instead on my template.php and calling the function from node-product.tlp.php

I hope others find this useful as well.....

<?php
/**
* Implementation of hook_nodeapi().
* This is used to display per node the total amount of purchases of each product
*/
function oferta_total_purchases(&$node, $tnid){
     global
$language;
    
//$langcode = $langcode ? $langcode : $language->language;

       

if (($node->nid) != ($node->tnid)) {   
       
       
$query= db_query("SELECT node.tnid AS tnid FROM node node WHERE node.nid=%d OR node.tnid=%d GROUP BY node.tnid",$node->nid,$node->nid);
       
$fetch_obj=db_fetch_object($query);

       

$a=$node->nid;
       
$b=$fetch_obj->tnid;
        }
        if ((
$node->nid) == ($node->tnid)) {
       
       
$query= db_query("SELECT node.nid AS nid FROM node node WHERE node.tnid=%d AND node.nid<>%d GROUP BY node.tnid",$node->nid,$node->nid);
       
$fetch_obj=db_fetch_object($query);

       

$a=$fetch_obj->nid;
       
$b=$node->nid;       
        }

        

$result = db_query("SELECT node.nid AS nid, node.tnid AS tnid, SUM(uc_order_products.qty) AS uc_order_products_qty FROM node node INNER JOIN uc_order_products uc_order_products ON node.nid = uc_order_products.nid INNER JOIN uc_orders uc_orders ON uc_order_products.order_id = uc_orders.order_id WHERE node.nid =%d  OR node.nid=%d AND (uc_orders.order_status IN ('completed'))  GROUP BY node.tnid",$a , $b);

        

$obj = db_fetch_object($result);    
        
        
             if (
$obj->uc_order_products_qty == 0){
                 print
'0';
                 }
            elseif (
$obj->uc_order_products_qty != 0){
                print
$obj->uc_order_products_qty;
                }else{
                print
'0';
                }
}
?>
507 Media Studios's picture
Offline
Joined: 07/04/2008
Juice: 35
How to do a product count from within views

I was not able to call the function I created above within Views2, I was however able to create a PHP customfield to look up the values from within Views and display the total aggregated value from all languages of a product. Just create a customfield: php and add the code below to it:

<?php
    $select
= "SELECT node.tnid, sum(qty) as total FROM uc_order_products as uc_op, node WHERE node.nid = uc_op.nid and tnid in (SELECT tnid from node where nid = $data->nid) GROUP by node.tnid  ORDER BY tnid" ;
   
$result = db_query($select) ;
   
$obj = db_fetch_object($result); 
   
$total_qty = $obj->total ;

echo 

$total_qty  ;
?>