Post by STN » Sun Oct 23, 2011 7:40 am

H,

I am trying to modify the price display such that if the price is 0, it will display FREE instead of the value. I have tried this in category.tpl

<?php if ($display_price) { ?>
<?php ?>
<?php if (!$products[$j]['special']) {
$pricee = $products[$j]['price']; if(strcmp($pricee, "0.00€" ) == 0) {echo "succ";}
?>
But it doesn't seem to work and print nothing. I did a gettype check and it returned string so i am doing a string comparison.

Please help.

Game Trainers


STN
New member

Posts

Joined
Fri Jul 01, 2011 6:45 am


Post by hawkagent » Sun Oct 23, 2011 8:10 am

in php you compare strings like this:

Code: Select all

if($price == "0")
{
 echo "Free"
}

Newbie

Posts

Joined
Sat Oct 22, 2011 4:33 am

Post by STN » Sun Oct 23, 2011 9:03 am

I know php and you should know that the function strcmp has a better chance and provide more details about the string comparison such if strings matched, greater than or less than. I tried it first before using strcmp. It seems like the price is something else, the trouble is echoing it prints the price+currency and comparing to that doesn't work.

Thanks anyway

Game Trainers


STN
New member

Posts

Joined
Fri Jul 01, 2011 6:45 am


Post by SXGuy » Sun Oct 23, 2011 3:36 pm

hawkagent is correct, his method will work, although i agree with your argument, it wont work lol.

Active Member

Posts

Joined
Sun Nov 08, 2009 2:07 am

Post by rph » Sun Oct 23, 2011 6:39 pm

hawkagent's method won't work because price is a string with the currency symbol on it.

STN, your sample code is missing the closing } so it's probably worth checking your real code for little errors like that. If not, there are tons of ways to do this. I'd consider using if/then shorthand and going with something like:

EDIT: The old code broke on some prices. This is a better solution:

Code: Select all

echo ((preg_replace('/[^0-9\.]/', '', $products[$j]['price']) == '0.00') ? 'Free' : $products[$j]['price']); 
Last edited by rph on Mon Oct 24, 2011 1:26 am, edited 4 times in total.

-Ryan


rph
Expert Member

Posts

Joined
Fri Jan 08, 2010 5:05 am
Location - Lincoln, Nebraska

Post by STN » Sun Oct 23, 2011 10:34 pm

Thanks for the reply rph, it works for dollars and euro currency. However, with pound sterling, i couldn't get it to work (i just couldn't think how). Instead i have used this code and it works perfect for values even as low as 0.10. Here's the code

Code: Select all

	  <?php if (!$products[$j]['special']) {
		  ?>
          <span style="color: #900; font-weight: bold;"><?php
		  $pricee =substr($products[$j]['price'], 0, 5); $poss = strpos($pricee, '0.0');
		  if (($poss === 0) || ($poss === 1) || ($poss === 2) ) { echo 'FREE'; }
		  else { echo $products[$j]['price']; }?></span>
          <?php } else { ?>
Anyone feel free to use and abuse.

[RESOLVED]

Game Trainers


STN
New member

Posts

Joined
Fri Jul 01, 2011 6:45 am


Post by rph » Mon Oct 24, 2011 1:22 am

Boy, you're really taking the long way around there.

The old code I posted broke in some places so I updated it. At any rate, this is really the sort of thing that should be done on the controller level before the values get formatted.

-Ryan


rph
Expert Member

Posts

Joined
Fri Jan 08, 2010 5:05 am
Location - Lincoln, Nebraska

Post by STN » Mon Oct 24, 2011 2:44 am

hey, i am not claiming its the best solution. I am a pretty bad coder as in writing efficient code but i get the job done somehow hehe.

I will use your code since its just one line. Thanks

Game Trainers


STN
New member

Posts

Joined
Fri Jul 01, 2011 6:45 am


Post by rph » Mon Oct 24, 2011 5:01 am

I'm not criticizing. Shorter code is usually just easier to work with and faster to execute. I think the real issue is low cost items (< 0.10€) would be listed as free but then the code I originally posted was badly bugged for right-side currency symbols and would have resulted in anything ending in 0.00 (e.g. 10.00€) being marked as free.

-Ryan


rph
Expert Member

Posts

Joined
Fri Jan 08, 2010 5:05 am
Location - Lincoln, Nebraska

Post by STN » Tue Oct 25, 2011 2:30 am

Yeah, that won't matter though as there are not going to be any items < 0.10€, they would rather be free if priced so low.

Game Trainers


STN
New member

Posts

Joined
Fri Jul 01, 2011 6:45 am


Post by rollingtoad » Thu Apr 05, 2012 6:32 am

Hey,

I have somewhat of a similar query, if I have a special price applied to a product, it displays 2 separate prices:

ie:

Main Price: €150
Special Price: <del>€150</del> €75

my problem is that if a special price is applied to a product, I need it to overwrite the "Main Price".
my current php is as follows:

<div class="right">
<div id="price-bg">
<?php echo $text_price; ?><br />
<?php echo $price; ?>
</div>

i'm guessing there needs to be an else function in there somewhere, but for the life of me, cannot get right!

<div id="price-bg">
<?php echo $text_price; ?><br />
<?php echo $price; ?>
<?php if (!$special) { ?>
<!--<?php echo $price; ?>-->
<?php } else { ?>
<span class="price-new"><?php echo $special; ?></span>
<?php } ?>

however the above shows both prices in the Main Price section, I only need it to show the Special Price if a special price applies, and not remove or hide the price of products that do not have a special price applied! I hope all this made sense!

Cheers for looking!

RT

Newbie

Posts

Joined
Thu Apr 05, 2012 5:52 am

Post by rollingtoad » Thu Apr 05, 2012 8:59 pm

not to worry got it sorted, just needed a bit of common sense in relation to the coding, this is what i used.

in catalog/view/theme/Your Theme/template/product/product.tpl
find line 26:
<?php echo $text_price; ?><br />
<?php echo $price; ?>

replace with:

<?php echo $text_price; ?><br />
<?php if (!$special) { ?>
<?php echo $price; ?>
<?php } else { ?>
<span class="price-new"><?php echo $special; ?></span>
<?php } ?>

works a treat. :)

Newbie

Posts

Joined
Thu Apr 05, 2012 5:52 am
Who is online

Users browsing this forum: No registered users and 97 guests