Changing product display if sale price = £0.00

bogorman
bogorman used Ask the Experts™
on
Am creating an online store for my website using Ubercart.
Basically, it all seems to work but I am stuck one one thing.
Some of the products have the sale price entered as 0.00. This is done deliberately as with these products there are a range of prices (e.g. events where there are two ticket prices - a standard one and one for students or unwaged people) so I cannot state one price when entering the product.
What I want to do is, where the sale price is normally displayed as £0.00 I want to display "click link for prices"
I have created a template file for the field concerned (views-view-field--uc-products--sell-price.tpl.php) and have changed the coding:


<?php

$data = $row->{$field->field_alias};
if(intval($data)==0){
      $output = "Click link for prices";
      }
/*print $data;*/
print $output;
?>

This shows the text string "Click link for prices" for all products even those where the price is £1.00

If I comment the last line and uncomment print $data I get a null.

Can anyone help me in this?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
I would check to make sure $data is numeric. The intval function will return 0 if the call fails, and running intval(null) is going to fail, so in any situation where $data is null, it is going to print the link.

$data = $row->{$field->field_alias};
if(is_numeric($data) && intval($data)==0){
      $output = "Click link for prices";
      }
/*print $data;*/
print $output;

Author

Commented:
Thanks.
When I use, as you suggest:


<?php

$data = $row->{$field->field_alias};
if(is_numeric($data) && intval($data)==0){
      $output = "Click link for prices";
      }
/*print $data;*/
print $output;

?>

The prices fields are unchanged (i.e. "Click link for prices" does not appear).
I assume this means that $data is not numeric. How can I code the other alternatives? If it is not numeric, would it be "£0.00" or '£0.00' or is this the way to go?

Author

Commented:
Have now added print gettype($data) to the coding and it prints NULL. Is there something wrong with the assignment as I also get nothing printed when I try to print($data) ?
Avinash ZalaWeb Expert

Commented:
is your DB value for price is saved as £1.00 or just 1.00?

if its saved as £1.00 the do below:

<?php

$data = $row->{$field->field_alias};
if($data==£0.00){
      $output = "Click link for prices";
      }
/*print $data;*/
print $output;

?>

Hope this helps,
Addy

Author

Commented:
Thanks.
If I try your coding I get a white screen. If, however, I enclose the currency figure in '  '  (i.e. '£0.0') the page appears but only the original prices appear, not the string.
Do not know how the price is saved in the db. On the product form it appears with the £ sign in front of it. I have two attributes set for this product and, when the form first appears I THINK that the data on the form is £0.00. Unfortunately it is rapidly replaced with the default settings of the attributes so £105 appears. Presumably the default data is £0.00.
How can I determine how this data is saved and so detect the £0.00 value?
when you print $data is it null for every row even ones with a price? If so then yes, there is probably a problem with the assignment.

how are you populating $row, in most cases I see code to access fields with square brackets not curly brackets.

$row->[$field->field_alias];

what do you get if you do

print $row->{$field->field_alias};
print $row->[$field->field_alias];
Avinash ZalaWeb Expert

Commented:
just check in you Db table how the data actually stored in that.

Author

Commented:
Sorry, I should have done that.
If I use phpMyAdmin I can find sell_price in the uc_products table. Its type is decimal(16,5). Its default value is 0.00000
Avinash ZalaWeb Expert

Commented:
use float as the type and try again....

Author

Commented:
Hi Addy:

Have tried:

<?php

$data = $row->{$field->field_alias};
if(is_float($data) && floatval($data)==0){
      $output = "Click link for prices";
      }
/*print $data;*/
print $output;

?>

which I think is what you mean but it still doesn't change the output to the string.

Hi jrm.
Sorry, I did not notice your second reply which came in before Addy's.
In the ubercart comments they suggest the coding using curly brackets.
If I try:

print $row->{$field->field_alias};
print $row->[$field->field_alias];

I get a white screen.

Author

Commented:
Have also tried adding a final line:

print floatval($data);

and I get an additional 0
Avinash ZalaWeb Expert

Commented:
i was mean that change the data type to float (If you have not problem with it) and try again..

Hope this helps
Addy

Author

Commented:
Hi Addy,

The table is created by Ubercart and I don't think I should change it (if that's what you mean).

Have now found this:

http://drupal.org/node/764768

Think I will try and install the patch. Trouble is it will just make zero field disappear which is not really what I want - I want to replace them with text.

It is strange that it is so difficult.
Commented:
Hi everyone,

I have been unable to solve this problem. What I am going to do is to create a different view for Events (where the price is variable) and Products. In the Events view I will simply exclude the price field.

Thanks, anyway, for your efforts.

Brian

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial