How to remove the trailing .00 from the price in Ubercart

Last updated on
27 October 2017

Drupal 7 will no longer be supported after January 5, 2025. Learn more and find resources for Drupal 7 sites

Introduction

Have you ever wanted to remove the trailing zero's ($xx.00) from the price of an Ubercart product? There IS a way, and while it might seem a bit daunting to the non-programmer types, it really isn't that difficult. We just have to "Override the Theme" to make it work.

The Basics

Before we begin, let's go through a few of the basics on theme overrides to get started with.

template.php

The first thing we need to address is the template.php file. This should be in the root folder of whatever your theme is. For simplicity, I'm going to reference the stock "bartik" theme which ships with Drupal core.

If your theme doesn't have a template.php file (yes, some themes don't include them by default), then you'll have to create it first.

The template.php file is looked at for overriding various theming functions (and other stuff) of modules or Drupal core itself and if there is an override in the template.php file, Drupal uses this override instead of whatever the module or core function is. (There's a bit more to it than that, but that's enough to explain the purpose of the file)

Theme Overrides

The thing you need to know about any theme override is that the place where it says theme_do_something(), theme has to be replace by the "machine name" of your theme name. So going back to the "bartik" theme I mentioned earlier, we would change:

theme_do_something()

to

bartik_do_something()

The theme machine name can usually be found in the .info file but it's usually going to be the same as the folder name that holds your theme files. Keep in mind you have the "fancy" theme name like Hello Kitty and you have the "machine name" theme name like hellokitty. You need the machine name.

theme_uc_price()

The next thing to explain is the function for theme_uc_price() which is part of Ubercarts API for overriding prices throughout the site (except store admin areas) and adding various whistles and bells that aren't there by default. (Like removing the trailing .00)

The Code

Now that you have a basic understanding of how theme overrides work, here comes the easy part...

Removing the .00

Just copy and paste the code below and paste it somewhere into your template.php. Make sure you replace bartik with the name of your theme.

/* change uc price to remove .00 */
function bartik_uc_price($variables) {
$output = '<span class="uc-price">' . str_replace(".00","",uc_currency_format($variables['price'])) . '</span>';
if (!empty($variables['suffixes'])) {
$output .= '<span class="price-suffixes">' . implode(' ', $variables['suffixes']) . '</span>';
}
return $output;
}

Conclusion

It's that easy! This works fine for normal product displays (core) and Panels (not sure about Display Suite), but doesn't quite work yet with Views. See this issue for an update on getting the theme override and views on the same page with the Ubercart price format. The workaround is to set the Price Format to "Numeric".

Help improve this page

Page status: No known problems

You can: