Illustration of Yeti character writing with a pencil.

Using a Theme Override to Add a Teaser to UberCart's Grid View

Profile picture for user Silas
Silas Albrecht
Senior Developer

I spent a bit of time helping somebody on freenode’s #drupal-support IRC channel add teaser text to Ubercart’s product grid view. Here’s an example of this from a site developed by left-click: http://www.biolateral.com/catalog/biolateral-cds It also includes audio, which also usually isn’t in the grid display. I’ve removed that part from the code below to simplify things. Awhile ago I posted a patch to add a checkbox to Ubercart to allow for this functionality. People seem to like it: http://www.ubercart.org/contrib/5395 However, that patch modifies core Ubercart, which isn’t ideal. Each time you upgrade Ubercart, you’ll have to remember to reapply my patch. A better solution is to add a theme override to your theme’s template.php file. This is a common practice in Drupal, so if you haven’t done it before, now’s a good time to learn. Just make sure you’re ready to dig into a bit of php. First check out this link for information on overriding themable output in Drupal 6: https://www.drupal.org/node/457740 Here are the steps for getting the teaser to show: 1) Grab entire function theme_uc_catalog_product_grid() from ‘ubercart/uc_catalog/uc_catalog.module’. Substitute ‘theme’ with the name of your theme. I’ve included this code below for reference, including the teaser part. 2) Throw that in your theme’s template.php file (‘/sites/all/themes/MYTHEME’ for example). 3) Here’s the code you add:

Sample code: The line you’ll need to add toUbercart’s default themable grid display function

$product_table .= ''. node_teaser($product->body) .'';

It’s already in the example at the bottom of this post. 4) On the “Administer > Site configuration > Performance” page, click on the “Clear cached data” button. And that should cover it. In my opinion this should be in Ubercart’s core, as a checkbox, but overriding themable output is always a good exercise.

Sample code: Ubercart’s default themable grid display function plus product’s teasers

function YOURTHEMENAME_uc_catalog_product_grid($products) {
  $product_table = '<div class="category-grid-products"><table>';
  $count = 0;
  foreach ($products as $nid) {
    $product = node_load($nid);

    if ($count == 0) {
      $product_table .= "<tr>";
    }
    elseif ($count % variable_get('uc_catalog_grid_display_width', 3) == 0) {
      $product_table .= "</tr><tr>";
    }

    $titlelink = l($product->title, "node/$nid", array('html' => TRUE));
    if (module_exists('imagecache') && 
          ($field = variable_get('uc_image_'. $product->type, '')) && 
          isset($product->$field) && 
          file_exists($product->{$field}[0]['filepath'])) {
      $imagelink = l(theme('imagecache', 'product_list', 
                             $product->{$field}[0]['filepath'], 
                             $product->title, $product->title), 
                             "node/$nid", array('html' => TRUE));
    }
    else {
      $imagelink = '';
    }

    $product_table .= '<td>';
    if (variable_get('uc_catalog_grid_display_title', TRUE)) {
      $product_table .= '<span class="catalog-grid-title">'. $titlelink .'</span>';
    }
    if (variable_get('uc_catalog_grid_display_model', TRUE)) {
      $product_table .= '<span class="catalog-grid-ref">'. $product->model .'</span>';
    }
    $product_table .= '<span class="catalog-grid-image">'. $imagelink .'</span>';
    if (variable_get('uc_catalog_grid_display_sell_price', TRUE)) {
      $product_table .= '<span class="catalog-grid-sell-price">'.
              uc_currency_format($product->sell_price) .'</span>';
    }

    $product_table .= ''. node_teaser($product->body) .'';

    if (variable_get('uc_catalog_grid_display_add_to_cart', TRUE)) {
      if (variable_get('uc_catalog_grid_display_attributes', TRUE)) {
        $product_table .= theme('uc_product_add_to_cart', $product);
      }
      else {
        $product_table .= drupal_get_form('uc_catalog_buy_it_now_form_'. 
                                     $product->nid, $product);
      }
    }
    $product_table .= '</td>';

    $count++;
  }
  $product_table .= "</tr></table></div>";
  return $product_table;
}
comments powered by Disqus