Customers who bought this item also bought

Posts: 63
Joined: 01/23/2008
Bug Finder

Hi

I have added this feature to the shop i am developing and I wanted to share it with you, maybe someone finds it useful

Ingredients
Drupal + Ubercart Eye-wink
CRE module -> http://drupal.org/project/cre
VotingApi module -> http://www.drupal.org/project/votingapi
Workflow-NG module and UI activated -> http://drupal.org/project/workflow_ng

You will need to create a custom module (or use any "auxiliar" modules you have). The code uses workflow-ng & votingapi hooks. It creates an action and a block.

After creating and activating the module, you should create a new configuration from workflow-ng UI screen, with the event set to Customer completes checkout, with no condition and with the action defined by the module (for the example below 'Vote the items'), so whenever a customer completes the checkout, a custom vote is issued.
The block is restricted to show 5 elements by the db_query_range.
Then you activate the block in the block administer settings wherever you want it.

Here is the code

<?php

/**
* Implementation of hook_action_info().
*/
function customer_related_action_info(){
  $actions = array();
  $actions['customer_related_vote_item'] = array(
    '#label' => t('Vote the items'),
    '#arguments' => array(
      'order' => array('#entity' => 'order', '#label' => t('Order')),
    ),
    '#module' => t('Name of the module'),
  );
  return $actions;
}

function customer_related_vote_item ($order) {

  $products = $order->products;
  foreach ($products as $product) {
    $votes->value = 1;
    $votes->value_type = 'points';
    $votes->tag = 'customer_related';
    $test = votingapi_set_vote('product',$product->nid,$votes,$order->uid);
  }
}

/**
* Hook block
*/
function customer_related_block($op = 'list', $delta = 0, $edit = array()) {
 
    switch ($op) {
      case 'list':
        $blocks[0]['info'] = t('Customers that bought this item also bought');
        return $blocks;
       
      case 'view':
        $block['subject'] = t('Customers that bought this item also bought');
        $block['content'] = _calculate_other_items();
        break;
    }
    return $block;
}

function _calculate_other_items() {

  $sql = "SELECT d.content_id1 as 'content_id',sum(d.sum+d.count*r.value)/sum(d.count) as 'score',n.title
    FROM {cre_similarity_matrix} d,{votingapi_vote} r,{node} n
    WHERE d.content_type1='product'
    AND d.content_id1<>r.content_id
    AND d.content_id2=r.content_id
    AND n.nid=d.content_id1
    AND r.tag = 'customer_related'
    AND n.type = 'product'
    AND n.nid<> %d
    AND d.content_id2 = %d
    GROUP BY d.content_id1 ORDER BY score DESC";
 
  $result = db_query_range ($sql,arg(1),arg(1),0, 5);
  $data_array = array();
  while ($data = db_fetch_object($result)) {
    $rows[] = array(l($data->title,'node/'.$data->content_id));
  }
  if (count($rows)) {
    return theme('table', array(), $rows, array(), NULL);
  }
  else {
    return NULL;
  }
}

Any comments or modifications are welcome!!!

Posts: 871
Joined: 11/05/2007
Bug FinderFAQ ModeratorGetting busy with the Ubercode.

Very nice!

If you could turn it into a module, you could add the dependencies on the other modules and implement the workflow hook to automatically generate the configuration - that way someone could install your contribution just by enabling it.

--

<tr>.

Posts: 63
Joined: 01/23/2008
Bug Finder

Thanks! I will work on that.

Posts: 1217
Joined: 08/14/2007
Bug FinderEarly adopter... addicted to alphas.Getting busy with the Ubercode.

Sounds like it'd be an awesome feature to add to my Upsell Contrib. It's one of those things I intended to include, but haven't gotten around to yet.

If you'd like to work on combining them, that'd be awesome, or I could give it a shot in the coming days.

Unless others think these two are better off separate?

--

"Pain don't hurt." - Dalton

Mike Nelson's RiffTrax! www.rifftrax.com

Posts: 871
Joined: 11/05/2007
Bug FinderFAQ ModeratorGetting busy with the Ubercode.

I would keep them as separate modules, to fit with the Drupal philosophy, but certainly group them into the same package so they show up together at admin/build/modules. Also goes well with the stuff Lyle moved to the Marketing contrib.

--

<tr>.

Posts: 1217
Joined: 08/14/2007
Bug FinderEarly adopter... addicted to alphas.Getting busy with the Ubercode.

Actually you're right, after thinking about it, I feel like the "Customers also bought..." seems more suited for a product detail page, whereas mine is fine for the Cart screen.

--

"Pain don't hurt." - Dalton

Mike Nelson's RiffTrax! www.rifftrax.com

Posts: 63
Joined: 01/23/2008
Bug Finder

Hi

Yes, is a block thought for product details page. I've adapted the code for being a module under your suggestions, it has a configuration for workflow by default and it is "part" of the package "Ubercart - extra".

I don't know where should I include the module for being a contribution, so I upload the module here until I discover that! Smiling

Thanks to both!

AttachmentSize
custom_like.zip1.73 KB
Posts: 871
Joined: 11/05/2007
Bug FinderFAQ ModeratorGetting busy with the Ubercode.

You can use the "Create Content" menu link to submit your contribution. Here's the direct link: http://www.ubercart.org/node/add/contrib

--

<tr>.

Posts: 63
Joined: 01/23/2008
Bug Finder

I am pending of a "feature" I've request to CRE module support, as they are not supporting (from my point of view) different tags for votingapi, because the queries don't restrict only to the custom tags when a node is voted by more than a voting-module.

here is the request: http://drupal.org/node/245349

Until this is fixed, the module i've posted only works if you don't have another module that uses votingapi or if you have one, but the content type "product" is not voted.

I'll keep you informed about this

Thanks!