Small feature request to add in the store currency admin, the ability to hide empty decimals from the price.

As an example, assuming the decimal is set to 2, the price formats as $10.00 or $10.49. I would love to see an option to hide the decimal if it's empty, so instead of $10.00, it would be $10, but if it was $10.49, show the decimal values.

The idea stems from point #7 in the article: http://blog.hubspot.com/blog/tabid/6307/bid/34118/7-Pricing-Mistakes-Tha...

Comments

tr’s picture

Category: feature » support
Status: Active » Fixed

You can do this by overriding theme_uc_price().

philsward’s picture

You can do this by overriding theme_uc_price().

I don't even know what that means? I don't know how to override anything in the code. I don't code...

DanZ’s picture

The display of almost everything in Drupal is controlled by themes. See https://drupal.org/documentation/theme for an explanation.

A theme function is a bit of PHP code that takes data and outputs HTML to display on a browser. A theme template does the same thing, except in a file, not a function. So, yes. Overriding these things requires some programming. If you can't program, that's OK. People who can program are pretty easy to find.

theme_uc_price() is the built-in function that themes prices. It uses uc_currency_format() to actually build the number string.

See Overriding themable output for directions on how to override that theme function. You'll want something that removes the decimal point if the price is close enough to an even dollar.

Or...you could try going to admin/store/settings/store, clicking on the "Currency format" vertical tab, and setting the "Number of decimal places" to zero.

philsward’s picture

@DanZ Thanks for the general directions. For anyone else looking to do this:

function theme_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;
}

(replace theme_uc_price with the name of your theme... ex. bartik_uc_price)

Works with panels too. Does NOT work with Views when "Display Price -> Format" is set to "Ubercart price". You have to set the format to "Numeric" to have views strip the .00. Bug maybe? If a views price format is set to "Ubercart Price" shouldn't it honor an overridden theme_uc_price?

philsward’s picture

@DanZ Forgot to mention you can disable the decimal, but it doesn't disable it gracefully. Meaning, if you have a number with change ($9.92) it gets rid of the .92 which isn't exactly ideal :-/

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

haysuess’s picture

Issue summary: View changes

The solution in #4 works for me, but ONLY in Views. When I print the price within my node template, I still get prices like $29.00.

Any ideas?