CSS for zero price

Contrib type: 
Code/CSS Snippet
Status: 
Initial upload
Moderation: 
Not ready for moderation
Compatibility: 
Ubercart 1.x

Below is a modified version of function theme_uc_product_sell_price($price, $teaser) in uc_product.module that adds a CSS class to the sell_price div on product nodes (and maybe elsewhere?) when the price is $0 so that simple CSS can be optionally used in a theme to not display the price when it's $0, eg div.zero_price { display: none; }

function theme_uc_product_sell_price($price, $teaser) {
  $class = 'sell_price';
  if ($price == null || $price == 0 || $price=="0.00")
    $class .= ' zero_price';
 
  if ($teaser) {
    $output = '<span class="'. $class. '">';
    $output .= uc_currency_format($price);
    $output .= '</span>';
  }
  else {
    $output = '<div class="'. $class. '">';
    $output .= t('Price: !price', array('!price' => uc_currency_format($price)));
    $output .= '</div>';
  }
  return $output;
}

And for grid catalog display (uc_catalog.module) replace the line:

$product_table .= '<span class="catalog_grid_sell_price">'. uc_currency_format($product->sell_price) .'</span>';

with lines:

$sell_price_class = 'catalog_grid_sell_price'. ($product->sell_price == '0.00' ? ' zero_price': '');
$product_table .= '<span class="'. $sell_price_class. '">'. uc_currency_format($product->sell_price) .'</span>';
Joined: 08/07/2007
Juice: 15046

This is a smart idea. For what it's worth, you can accomplish this at the theme level by overriding the core theme function in your theme's template.php so you don't have to hack core. Check out this FAQ for more info.

Joined: 01/09/2008
Juice: 152

Yes, good point. Smiling We have a multi-site install so it made more sense for us to hack core once instead of multiple themes.

Joined: 03/24/2009
Juice: 4

To implement this on Ubercart 2 place this code at the end of your template.php file:

function phptemplate_uc_product_price($price, $context, $options = array()) {
if ($price == null || $price == 0 || $price=="0.00")
  $output = '';
  else {
  $output = '<div class="product-info '. implode(' ', (array)$context['class']) .'">';
  $output .= uc_price($price, $context, $options);
  $output .= '</div>';
  }

  return $output;
}

Joined: 05/27/2009
Juice: 35

keyinsight, I just wanted to thank you for this code. It saved me hours.

Joined: 07/09/2009
Juice: 2

Been trying without success to suppress zeros being displayed for zero cost items in the UC shopping cart. I have tried adding the snippet of code at the bottom of my template.php file as suggested for Ubercart 2 solution and "cleared cache" under performance still with no luck.

Any help in this area would be greatly appreciated.

Joined: 01/10/2010
Juice: 15

Just a note for anyone who has difficulty implementing snippets like these. I noticed that when you copy code from most pages here that some invisible characters often show up (ie •) when pasting back into some text editors.(ie TextWrangler) If you show invisibles in your text area and just delete those invisibles, this code should work for you.