Downloads
I needed a block to show the top X products, so I expanded the views compatibility a little...
I didn't post this as a module, because in this post simular code got adopted in uc_products, so I'll leave it up to core devs to decide where to go with this...
I added the order table in function uc_views_views_tables()
$tables['uc_order_products'] = array(
'name' => 'uc_order_products',
'join' => array(
'left' => array(
'table' => 'node',
'field' => 'vid'
),
'right' => array(
'field' => 'nid'
),
),
'fields' => array(
'title' => array(
'name' => t('Purchased Product: Title'),
'sortable' => TRUE,
),
'qty' => array(
'name' => t('Purchased Product: Quantity'),
'sortable' => TRUE,
),
'price' => array(
'name' => t('Purchased Product: Price'),
'sortable' => TRUE,
),
'product_count' => array(
'name' => t('Purchased Product: Number of Purchases'),
'sortable' => true,
'notafield' => true,
'sort_handler' => 'uc_views_query_handler_product_count',
),
),
'sorts' => array(
'title' => array(
'name' => t('Purchased Product: Title'),
),
'qty' => array(
'name' => t('Purchased Product: Quantity'),
),
'price' => array(
'name' => t('Purchased Product: Price'),
),
'product_count' => array(
'name' => t('Purchased Product: Number of Purchases'),
'handler' => 'uc_views_handler_product_count',
'help' => t('Sort by the number of purchases'),
),
),
);and put these in as extra handlers.
function uc_views_query_handler_product_count($field, $fieldinfo, $query) {
$alias = $field['field'];
$query->ensure_table($field['tablename']);
$query->add_field('count( ' . $field['tablename'] . '.nid )', '', $alias);
$query->add_groupby('node.nid');
}
/**
* Sort handler for count of purchased products
*/
function uc_views_handler_product_count($op, &$query, $sortdata, $sort) {
$alias = explode('.', $sort['id']);
$alias = $alias[1];
$query->ensure_table($sortdata['table']);
$query->add_orderby(NULL, 'sum(qty)', $sort['sortorder'], $alias );
$query->add_groupby($sortdata['table'].'.nid');
}

)