Re: Re: Re: Re: Re: Customer who bought this also bought...

torgosPizza's picture
Offline
Bug FinderEarly adopter... addicted to alphas.Getting busy with the Ubercode.
Joined: 08/14/2007
Juice: 4110
Re: Re: Re: Re: Re: Customer who bought this also bought...

Well I got something that works, but only partially. It seems to work great as long as you don't have very many orders - the main issue is that the query takes an incredibly long time to run sometimes. I haven't isolated the issue yet, and I've tried optimizing it, but it still seems to get hung up while copying the query to a temp table.

Anyways, you need to put this into a theme_function override. In my case, the theme name was "RiffTrax" - so the function is called RiffTrax_uc_cart_view_form.

<?php
function RiffTrax_uc_cart_view_form($form) {
 
drupal_add_css(drupal_get_path('module', 'uc_cart') .'/uc_cart.css');

 

$output = '<div id="cart-form-products">'
         
. tapir_get_table('uc_cart_view_table', $form) .'</div>';

  if ((

$page = variable_get('uc_continue_shopping_url', '')) != '<none>') {
    if (
variable_get('uc_continue_shopping_type', 'link') == 'link') {
     
$continue_shopping_link = l(variable_get('uc_continue_shopping_text', t('Continue shopping')), $page);
    }
    else {
     
$continue_shopping_link = drupal_render($form['continue_shopping']);
    }
  }

 

$output .= '<div id="cart-form-buttons"><div id="cart-button-shopping">'
          
. $continue_shopping_link .'</div>'
          
. drupal_render($form) .'</div>';
         
          
$output .= '<div style="padding:8px;" id="cart-related-items">Customers who purchased the items in your shopping cart also bought:<br /> ';
                   
          
$cartitems = uc_cart_get_contents();
          
$random = mt_rand(0, count($cartitems));
          
$random_item = $cartitems[$random];          

           
          

$result = db_query("SELECT that.nid, that.title, COUNT(that.nid) AS frequency
                        FROM {uc_order_products} AS that
                        LEFT JOIN {uc_order_products} AS this
                        USING (order_id)
                        WHERE this.nid = %d AND that.nid != %d
                        GROUP BY that.nid
                        ORDER BY frequency DESC
                        LIMIT 5"
, $random_item->nid, $random_item->nid);
           
               while (
$upsell = db_fetch_object($result)) {           
       
                  
$image = db_result(db_query("SELECT field_image_cache_title FROM {content_field_image_cache} WHERE nid = %d", $upsell->nid));
                  
                  
$output .= '<div id="cart-related-item" style="padding:5px; width:18%; float:left; position: relative; text-align:center;"><a href="/'.drupal_get_path_alias("node/".$upsell->nid) .'">
                           <img src="/files/imagecache/thumbnail/files/'
.$image.'"><br />
                           '
.$upsell->title.'</a><br />';
                       
//<a href="cart/add/i'.$upsell->nid.'Up-p'.$upsell->nid.'_q1?destination=cart"><strong>(Add to cart)</strong></a></div>';
              

         
          
$output .= '</div>';
          
return
$output;
}
?>

Please test this on your sites if you can, and if you know a way to optimize this query so it doesn't hang half the time, I'd be much obliged Smiling

(I've taken it down from the site until I can do more testing on the query. Attached a screenshot as proof that in works, since you can't see it live right now.)

AttachmentSize
upsell-screencap.jpg 159.75 KB

--
Help directly fund development: Donate via PayPal!