some more views integration (uc_views expanded)

Contrib type: 
Code/CSS Snippet
Status: 
Uploaded for testing

Downloads


Compatibility: 
Ubercart 1.x

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');
}
Related threads: 
Lyle's picture
Offline
AdministratoreLiTe!
Joined: 08/07/2007
Juice: 6855
Re: some more views integration (uc_views expanded)

I couldn't get it to actually group by nodes, so all I saw was each order's products. Of course, there wasn't any order data to go with them, so it wasn't very useful. Maybe one of those handlers should be a "query_handler"? That's the impression I got from reading the Views documentation.

kcoworks's picture
Offline
Joined: 11/22/2007
Juice: 103
Re: Re: some more views integration (uc_views expanded)

I don't really understand what you mean... There's a group and order clause already defined in this code, you should use the "sort by" option in the views UI. Of course this only works when there are products sold in the store.
What exactly did you try to do with it? If it's something different, I'll try to include it... I'm also updating our concept store to beta3 at this point, so I still need to verify if this code keeps working...
(I've been busy on some other projects the past weeks, but for the next 3-4 weeks, I'm busy on ubercart stuff for a few days a week, so it's a good time to ask Eye-wink)

Ryan's picture
Offline
Joined: 08/07/2007
Juice: 15459
Re: Re: Re: some more views integration (uc_views expanded)

I also couldn't really get this to be useful... my problem is like Lyle's, I'm just seeing a list of all products on orders, but they're not grouped by nid or anything like that. So I have 15 entries for code transmogrifier, more for nunchucks, and so on. And I'm getting no results in the "Number of Purchases" column I setup.

So, as is, there's nothing to introduce to core, but I am curious to know how you're using this mod. What may be helpful would be to attach an export of your view that I can import and demo.