34 replies [Last post]
goodeit's picture
Offline
Joined: 05/28/2008
Juice: 322
Was this information Helpful?

Hello,
I was just wondering where the best place would be to do a mod that allowed prices that list as "0.00" to display "please call for pricing". Is there a hook somewhere I can modify so that anywhere the price is displayed it would show the text instead, or do I need to do it in individual pages (node.tpl and... others?).

Yes, I'm in slightly over my head, but I'm going to give it a shot!

leenwebb's picture
Offline
Joined: 06/01/2008
Juice: 248
Re: Change display of $0 price, where to do mod?

I am pretty new at this myself, so this may not be the *best* (read: "drupaliest") way to do this, but it will work:

1) Create a file in themes/yourtheme called node-product.tpl.php . Copy the contents of node.tpl.php (which is currently controlling the display) into the new file, which will now control the display of the product nodes.

2) The sale price lives in the variable $node->sell_price . Seems like the next step is to create an if{} statement. You could do "if sell_price = 0, then reset sell_price to be 'call for details' ". But that seems a little hinky. It is probably more appropriate to break out where the sale price is displayed and say "if sale price != 0, then display price. Otherwise display 'call for details' ".

I'm not sure how it would work to add such an item to the cart? But maybe you are using the catalog part and not the cart part.

Good luck!

Eileen

goodeit's picture
Offline
Joined: 05/28/2008
Juice: 322
Re: Re: Change display of $0 price, where to do mod?

Thanks for the tip! I'm trying to find the right variable to change, but it seems almost as if the entire $content variable is what shows the price (and the picture, description, etc) and that it is built before the page is loaded (e.g. cannot change its values).

I have successfully changed $node->sell_price and $sell_price. I have been trying to access $content->display_price->value or whatever that is, but that does not seem to work.

Ideally I would do this in a contributed module for ubercart... but I have no idea how to make modules Sad

torgosPizza's picture
Offline
Bug FinderEarly adopter... addicted to alphas.Getting busy with the Ubercode.
Joined: 08/14/2007
Juice: 4110
Re: Re: Re: Change display of $0 price, where to do mod?

This will print your sell price. (This code goes either in a Content Template or in your node-product.tpl.php file.

<?php
print $node->sell_price;
?>

So you could say,

<?php
if (intval($node->sell_price) > 0) {
  print
$node->sell_price;
} else {
  print
"Free!";
}
?>

Should work, but untested.

--
Help directly fund development: Donate via PayPal!

goodeit's picture
Offline
Joined: 05/28/2008
Juice: 322
Re: Re: Re: Re: Change display of $0 price, where to do mod?

Thanks. How can integrate this into the way Ubercart already displays products? E.g. it already has all of the fields - price, sku, image, description - cobbled together in one variable ($content) before it is displayed (and there's no way I would parse that to change the sell price display!). How can I change that? Or would I want to change it somewhere else?

I still think the best thing would be if there was a method for displaying the price I could override...

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: Change display of $0 price, where to do mod?

I think you're confusing some things, or you didn't read my post closely. (Or you're not understanding me, for which I apologize).

Let me ask you: are you talking about actual display of the product? As in, you've clicked a link and you're now viewing a product detail page? If so, then the way I outlined will work. You'll need to create a template file, called node-product.tpl.php, which will allow you to modify the rendered output of the Drupal node. This includes the price as it comes from the $node object.

If you are talking about doing it in a View, that is essentially the same thing - you would have to create a View first, and then create a template file called view-YOURVIEWNAME.tpl.php, and then add a function to your template.php file to override the default view. (This can be done easily using the Views Theme Wizard.)

But if neither of these describe your scenario, I'm afraid you'll have to be more specific Smiling

--
Help directly fund development: Donate via PayPal!

goodeit's picture
Offline
Joined: 05/28/2008
Juice: 322
Re: Re: Re: Re: Re: Re: Change display of $0 price, where to do

I'm most definitely confusing some things Smiling

I want "please call..." to show ANYWHERE that the product price is displayed, or at the very least in the catalog and on the product page. This was why I was hoping there was something I could override.

I understand the usage of the "if" statement, so that's not a problem.

In order to use your method, I created node-product.tpl.php, and copied node.tpl.php into it. Unfortunately, it does not display everything separately, in the file in order to print the description, add to cart, and etc. it just says

<?php
print $content
?>

, and that handles everything. I'm guessing that I would have to find exactly what shows inside that variable, and then instead of

<?php
print $content
?>

put all of the html code from $content and substitute in each product variable?

I apologize for being so vague Sad I just need to hide $0 prices wherever possible on the site.

Thanks so much for your help.

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: Re: Re: Change display of $0 price, where to

Ah! I see what's happening.

In this case, you can just use

<?php
print $node->content['body']['#value'];
?>

to print just the body of your product. (The "description"). You can access all parts of the $node object this way. It's an object of arrays.

Here are others you can access. (Using the "Contemplate" module isn't necessary, but it'll help you discover all the different values of your $node object, including values of CCK fields, or custom node types such as Product.) Note, that if you just put

<?php
print_r
($node);
?>

to print the entire Node object. Of course you'll need to learn how to access the Values in the node... they all follow pretty much the same format as the first one I've listed here.

<?php
print $node->field_image_cache[0]['filepath']; // The path of your first Product image
print $node->nid; // The node's nid.
print $node->title; // The node title, which is the name of the Product
?>

In this way you can create a custom product node template which will only show the fields you specify.

Hope this clears things up Smiling

--
Help directly fund development: Donate via PayPal!

goodeit's picture
Offline
Joined: 05/28/2008
Juice: 322
Re: Re: Re: Re: Re: Re: Re: Re: Change display of $0 price, wher

OK, so I do have to create my own layout for the product pages. Which I can just do by copying the current output of the $content and modifying... etc. Then I can do the same for the catalog pages (using views theming)...

any ideas for the shopping cart/checkout pages? They are not as important, but it would be nice...

thanks again for your assistance.

goodeit's picture
Offline
Joined: 05/28/2008
Juice: 322
Re: Re: Re: Re: Re: Re: Re: Re: Re: Change display of $0 price,

In case anyone else needs to be able to do this, I found a function uc_currency_format() that I added an "if" statement to, checking if $value == 0 and setting $format to "Please call for pricing" if it did. Works like a charm in the cart, on my specials block, in the catalog, and on the product pages.

probably not the best way to do things... but it works.

LBD
LBD's picture
Offline
Joined: 07/08/2008
Juice: 2
Re: Change display of $0 price, where to do mod?

I use "zen" as my theme, and I add the following snippet to the template.php inside the sites/all/themes/zen folder. They will take care of all the display and selling price in most of the circumstances.

<?php
function zen_uc_product_display_price($price) {
 
$output = '<div class="display_price">';
    if (
$price!=0) {
     
$output .= uc_currency_format($price);
    } else {
     
$output .= t('P.O.A');
    }
 
$output .= '</div>';
  return
$output;
}

function

zen_uc_product_sell_price($price, $teaser) {
  if (
$teaser) {
   
$output = '<span class="sell_price">';
        if (
$price!=0) {
           
$output .= uc_currency_format($price);
        } else {
           
$output .= t('P.O.A');
        }
   
$output .= '</span>';
  }
  else {
   
$output = '<div class="sell_price">';
        if (
$price!=0) {
           
$output .= t('Price: !price', array('!price' => uc_currency_format($price)));
        } else {
           
$output .= t('Price: P.O.A');
        }
   
$output .= '</div>';
  }
  return
$output;
}
?>

The main idea is overriding the theme functions from the ubercart, i.e. theme_uc_product_display_price() and theme_uc_product_sell_price() in this case. Just find and copy these two functions from uc_product.module inside the sites/all/modules/ubercart folder and paste the code into the template.php you have created for your own theme. Then, rename the function by replacing "theme_" with your theme name, e.g. "mytheme_". You are then free to modify the way of the display logic Smiling

torgosPizza's picture
Offline
Bug FinderEarly adopter... addicted to alphas.Getting busy with the Ubercode.
Joined: 08/14/2007
Juice: 4110
Re: Re: Change display of $0 price, where to do mod?

An even better, (more Drupal-ier) way of doing it. Thanks for posting that Smiling

--
Help directly fund development: Donate via PayPal!

goodeit's picture
Offline
Joined: 05/28/2008
Juice: 322
Re: Re: Change display of $0 price, where to do mod?

great! This is definitely the way to go. There is one issue that I can see: the "grid view" display, while browsing the category, still shows $0.00 instead of "P.O.A.". It looks like it uses "catalog_grid_sell_price" instead. Upon inspection, it looks like uc_catalog.module does not call a theme function and instead directly calls uc_currency_format() with the price. Would this be considered bad Ubercart practice, because it is not themed?

And is it worse that I changed uc_catalog.module to call a theme function instead?

I created a new theme function, theme_uc_catalog_grid_sell_price(), in uc_catalog.module:

/**
* @ingroup themeable
*/
function theme_uc_catalog_grid_sell_price($price) {
  $output = '<span class="catalog_grid_sell_price">';
  $output .= uc_currency_format($price);
  $output .= '</span>';
  return $output;
}

then changed a line in theme_uc_catalog_product_grid() to call the theme function. (then I themed the function in my template.php)

comments welcome, please let me know if this is the wrong way to do it (if it's the right way, can we get it patched?)

TR
TR's picture
Offline
Bug FinderFAQ ModeratorGetting busy with the Ubercode.
Joined: 11/05/2007
Juice: 3424
Re: Re: Re: Change display of $0 price, where to do mod?

You could have just overridden theme_uc_catalog_product_grid() couldn't you? But the real problem is the grid display especially needs to be refactored to allow better theming. The grid display was a contributed module initially, and was pasted into core because it was useful. But the coding architecture, standards, and style for that part of uc_catalog are considerably less developed than most of Ubercart. It would be nice, for instance, to be able to theme each cell of the grid display with one theme rather than having to override theme_uc_catalog_product_grid() and re-do the entire page. This should also be a lot more configurable as to what elements of the product should be displayed. The table tags at a minimum need to have classes assigned to allow theming via CSS, and preferably should be eliminated in favor of divs. I think a lot of people just bypass these issues by going straight to Views to create their catalog, but maybe the default catalog should be more usable to non-programmers.

As far as the price display, I'm of the opinion that uc_currency_format() should be a theme function, because it's the single choke point that prices go through, whether they are displayed as part of a product node or as part of a page (like the catalog, cart, checkout, order pages). And it really is a theme function in disguise right now - you pass in a numerical value and it returns a formatted string with currency symbol etc. - but because it's not defined as theme_uc_currency_format() you lose the ability to override it to display prices how you want. Making it into a theme function turns it into a simple task to suppress prices for anonymous users, for example, or to convert currencies. Otherwise you have to override different theme functions depending on which page is using the price, as you found out, and you can't control how a contributed module uses/displays the price. I will revisit this subject in a different thread after I release my multicurrency module (which works, but I'm waiting for client to sign off on it).

<tr>.
Ryan's picture
Offline
Joined: 08/07/2007
Juice: 15438
Re: Re: Re: Re: Change display of $0 price, where to do mod?

I really like the idea of moving uc_currency_format() to a theme function... I'll be attending a "module developer meet themers" BoF session by mortendk at Drupalcon Szeged where maybe I can bone up on my knowledge of writing good theme functions. I already got some pointers from walkah and would love to get more feedback... for example, should I reference the variables in the theme function itself or keep uc_currency_format() and pass the amount and currency related variables into the theme function where it can all be glued together in an HTML string?

goodeit's picture
Offline
Joined: 05/28/2008
Juice: 322
Re: Re: Re: Re: Re: Change display of $0 price, where to do mod?

I don't know too terribly much about 'best-practice' functions in ubercart (theme or otherwise) but it seems that you would want to keep HTML strings out of uc_currency_format() because of the many different contexts it is called from.(?)

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: Re: Change display of $0 price, where to do

I don't think that's what he meant but I could be mistaken.

I'm a fan of theming everything possible that is output on the site - such as the File Downloads page Smiling But I digress. I think anything that can be themeable, should be.

--
Help directly fund development: Donate via PayPal!

TR
TR's picture
Offline
Bug FinderFAQ ModeratorGetting busy with the Ubercode.
Joined: 11/05/2007
Juice: 3424
Re: Re: Re: Re: Re: Re: Re: Change display of $0 price, where to

Theming can be done at many levels - it's not one-size-fits-all. I'm not suggesting putting HTML into uc_currency_format(), because like goodeit says it's used in many different contexts. The markup properly belongs in functions like theme_uc_product_display_price() which are specific to a given use of the price.

However, theme_uc_product_display_price() and all the other functions like it still have a need to figure out certain things about how to display a currency number, such as what currency symbol to use, what character should separate the units from the decimals, etc. It makes sense to have these functions all use the same algorithm to figure this out, and to allow a developer to override or supplement that algorithm. *This* is where uc_currency_format() written as a theme function comes in.

I'm proposing theme_uc_product_display_price() (for example) should use uc_currency_format() as it does now, except that uc_currency_format() should be renamed to theme_uc_currency_format() and invoked by $string_price = theme('uc_currency_format', $num_price) rather than $string_price = uc_currency_format($num_price). Wrapping the resulting currency string in div's and other markup would still be the job of the theme function that called theme_uc_currency_format(). The big effect this will have is to allow other code to hook into the process and determine how the currency number is displayed before the markup is applied. I admit I have an ulterior motive - this is how I implemented my multicurrency module (which I really must post to drupal.org soon!).

As far as dealing with parameters - I don't think uc_currency_format() should be accepting parameters specifying explicitly which thousands separator to use, for example. uc_currency_format() currently is trying to do too much. The invoking program shouldn't have to worry about all these details. Likewise, letting the invoking program change the defaults through the parameters breaks the encapsulation of functionality provided by uc_currency_format() - there is then no one place to modify the currency format because it can be "ignored" by anyone's code.

I think a better way to go about it is to provide a parameter to specify the *context* in which the currency will be used. A context could be a string like "localized" to return the currency formatted for the user's locale (this is the default), or "store" to format in the store's default, or "unformatted" to return just a number, etc. An advantage to doing it this way is that, again, theme_uc_currency_format() could be overridden to add additional contexts to meet anyone's needs. So there is no loss of flexibility here by leaving out the parameters. Just the opposite - it allows you to do MORE without breaking other modules or other uses of these functions.

As a use case, let's consider the subject of this thread: How can we display a message to anonymous users, such as "Please log in to see prices", instead of the actual price? Right now, you can either 1) modify a core Ubercart function (uc_currency_format()) as in comment #9 above, or 2) attempt to override all the theme functions, if available, for every module that uses prices. But as shown in comment #12 above, not every module that displays a price provides a theme function, so this second method isn't always possible or practical.

If uc_currency_format() were a theme function, however, it would be a simple matter to add this without modifying core by overriding the theme function (in pseudo-code):

function phptemplate_uc_currency_format($args) {
  global $user;
  if ($user == 0) {
    return "Please log in to see prices";
  }
  else {
    return theme_uc_currency_format($args);
  }
}
<tr>.
TR
TR's picture
Offline
Bug FinderFAQ ModeratorGetting busy with the Ubercode.
Joined: 11/05/2007
Juice: 3424
Re: Re: Re: Re: Re: Re: Re: Re: Change display of $0 price, wher

I just posted my multi-currency module at http://www.ubercart.org/contrib/6102

Among other things, it allows the admin to suppress the display of prices to anonymous users, and specify a message to appear instead. This works for *all* places that prices are displayed: the product page and teaser, the catalog page, the product list, product views, etc.

You can see in the code how I've dumped a lot of the functionality into uc_currency_format(), and why I suggest uc_currency_format() should be changed to a theme function to be more useful.

<tr>.
chris33@drupal.org's picture
Offline
Joined: 10/03/2008
Juice: 40
When the price is 0 and POA is displayed, add to cart is disable

Hi, thanks for the code, I just want to add that something like this, when the price is 0 and POA is displayed, I want the "Add to Cart" is disabled. What function in uc_product should be modified.

I hope someone will answer ASAP. Thanks.

torgosPizza's picture
Offline
Bug FinderEarly adopter... addicted to alphas.Getting busy with the Ubercode.
Joined: 08/14/2007
Juice: 4110
Re: When the price is 0 and POA is displayed, add to cart is dis

Don't modify anything in uc_product. You can easily do it with node-product.tpl.php and a conditional wrapped around your add to cart form.

<?php
if ($node->sell_price == 0) {
 
// Don't show the add to cart form.
}
else {
  print
$node->content['add_to_cart']['#value'];
}
?>

--
Help directly fund development: Donate via PayPal!

matcha213's picture
Offline
Joined: 10/02/2009
Juice: 2
Re: Change display of $0 price, where to do mod?

Well, the alternative is that you can change the uc_price function in the ubercart core.

It is located in ubercart/uc_store/uc_price.inc

The function uc_store_price_handler can be modified to change the display. Here's a modified example...

Basically anything $1 or below is changed to "Varies with options." Why I chose $1? no reason Sticking out tongue

<?php
function uc_store_price_handler_format($price, $options) {
 
$output = '';

 

// If the value is less than the minimum precision, zero it.
 
if ($options['prec'] > 0 && abs($price) < 1 / pow(10, $options['prec'])) {
   
$price = 0;
  }

 

// Force the price to a positive value and add a negative sign if necessary.
 
if ($price < 0) {
   
$price = abs($price);
   
$output .= '-';
  }

 

// Format the number, like 1234.567 => 1,234.57
 
if($price <= 1){
  
$output .= "Varies with options";
  }else{
 
 
// Add the currency sign first if specified.
 
if ($options['sign'] && !$options['sign_after']) {
   
$output .= $options['sign'];
  }
 
$output .= number_format($price, $options['prec'], $options['dec'], $options['thou']);
  }
 
// Add the currency sign last if specified.
 
if ($options['sign'] && $options['sign_after']) {
   
$output .= $options['sign'];
  }

  return

$output;
}
?>
torgosPizza's picture
Offline
Bug FinderEarly adopter... addicted to alphas.Getting busy with the Ubercode.
Joined: 08/14/2007
Juice: 4110
Re: Re: Change display of $0 price, where to do mod?

Hacking core is dumb, dumb, dumb. It nullifies your upgrade path. Don't do it!

You can override theme functions, or better yet, just do this type of stuff in your product node template file. For information on how to do this, read the Drupal Handbooks on drupal.org.

--
Help directly fund development: Donate via PayPal!

nyleve's picture
Offline
Joined: 03/15/2010
Juice: 55
Hi, I'm also trying to change

Hi,

I'm also trying to change the output of the sell price in my views. I've tried following the Views 2 theming tutorial (http://drupal.org/node/352970) and came up with the following code but it keeps throwing up syntax errors. Could anyone tell me where I might have gone wrong please?

<?php
if ($fields['sell_price']== 0) {
print
"Free!";
}
else {
print
$fields['sell_price']->content;
?>

Any help is appreciated!

Thanks,

Evelyn

chosig's picture
Offline
Joined: 01/28/2009
Juice: 27
Missing closing curly
<?php
if ($fields['sell_price']== 0) {
    print
"Free!";
} else {
    print
$fields['sell_price']->content;
}
?>
marcus178's picture
Offline
Joined: 04/13/2009
Juice: 113
Can't get it to work in views

I've tried the following

<?php if ($fields['sell_price']->content == 0) {?>
Price: FREE /
<?php } else {?>
Price: <?php print $fields['sell_price']->content?> /
<?php } ?>

but this displays everything as free when using formatted original ubercart price, but works if i change the ubercart price to orginal, but then the price is not formatted.

codebutcher's picture
Offline
Joined: 09/10/2010
Juice: 15
Re: Can't get it to work in views

Might want to swap order so it's if > 0 then print, else free
Also, I'm not sure about how the fields are defined so you might try 0.00 or '0'. Try swapping and using the > sign first.
From what I've learned about programming after doing it for 15 years is that sometimes instead of something not working at all when wrong, it can just act weird.

marcus178's picture
Offline
Joined: 04/13/2009
Juice: 113
Re: Re: Can't get it to work in views

I had already tried every variation of 0 '0' '0.00' on my original code with no success and just tried swapping to use > 0 > '0' > '0.00' and it's still not working. Just doesn't seem to make any sense.

codebutcher's picture
Offline
Joined: 09/10/2010
Juice: 15
Re: Re: Re: Can't get it to work in views

In that case, ECHO / PRINT out the value for the field. This way if it shows 0, then there is something else wrong.

marcus178's picture
Offline
Joined: 04/13/2009
Juice: 113
Re: Change display of $0 price, where to do mod?

When i print this field I get £0.00 but I've tried including the £ symbol and get the same result.

codebutcher's picture
Offline
Joined: 09/10/2010
Juice: 15
Re: Re: Change display of $0 price, where to do mod?

Ok, so at this point, the field is either carrying a currency format or it's simply a character field (not a numeric field of some type).
try converting to number format first...

   1.      <?php
   2.      $str
= "10";
  
3.      $num = (int)$str;
  
4.      
   5.     
if ($str === 10) echo "String";
  
6.      if ($num === 10) echo "Integer";
  
7.
      ?>
nyleve's picture
Offline
Joined: 03/15/2010
Juice: 55
Re: Re: Re: Change display of $0 price, where to do mod?

This is what I use...

<?php

//fetch the raw result from the db
$node = node_load($row->nid); //this is cached so no perf hit
$price = $node->sell_price;

if (

$price == 0) {
 
//t allows for translation
 
print t("IT'S FREE!");
}
else {
  print
$output;
}
?>

Hope this helps.

Evelyn

marcus178's picture
Offline
Joined: 04/13/2009
Juice: 113
Re: Re: Re: Re: Change display of $0 price, where to do mod?

Thankyou, that has worked for me too.

Infinitee's picture
Offline
Joined: 02/19/2010
Juice: 78
Change attribute option price to display True price not +$price

Anyone know how I can either eliminate or have the option price display the true price instead of the Ubercart , +$price?

My client (and I) consider this way of displaying the options Select fields to be confusing to the average visitor.

For example, the current display in an attributes options for a product that cost $10.50 are:

1 Dozen
12 Dozen , +$79.50
5 Gross , +$349.50

I would prefer the options display to either not have any +prices or display the true prices like so:

1 Dozen $10.50
12 Dozen $90.00
5 Gross $360.00

I believe this makes shopping much easier for consumers as the visitors that can't figure out what is really going on might think that the pricing is deceptive or simply confusing.

Thanks in advance,

Ralph

Ralph Manis
Infinitee Web Design
http://www.infiniteewebdesign.com

Infinitee's picture
Offline
Joined: 02/19/2010
Juice: 78
(Solved) Change attribute option price

This should be placed under the Store administration> Attributes but, it is at:

Configuration> Attribute settings
admin/store/settings/attributes

Do not display
Display price adjustment
Display total price

It took me two days to accidentally find this.

Cheers,

Ralph

Ralph Manis
Infinitee Web Design
http://www.infiniteewebdesign.com