1

This is the code I have:

in the php file:

if($special>0){
    $lease_price = (($special/1000)*38);
} else {
    $lease_price = (($price/1000)*38);
}
$lease_price = $this->currency->format($lease_price);

and in the front end tpl file:

<p>
   <i class="fa fa-chevron-down"></i>
   <b>Lease To Buy Price:</b>
   <span><?php if($price>500){ ?>
       <?php echo $lease_price; ?>
   <?php } else { echo 'NA'; } ?></span>                     
</p>

At the moment I'm getting this issue after var dump: Notice: Undefined variable: lease_price in /var/www/framec.co.uk/catalog/view/theme/lexus_superstore/template/product/product.tpl on line 160NULL

Here's a link to a pastebin of my php file http://pastebin.com/bTPtvgUQ

What do I need to add to make this work? Do I need some sort of code like this?:

'type' => $option['type'],

Here's a link to the page this is on http://www.framec.co.uk/index.php?route=product/product&path=59_78&product_id=45

Justinas
  • 41,402
  • 5
  • 66
  • 96
BC GB
  • 17
  • 1
  • 5

2 Answers2

0

There are two ways to handle this.

Initialise it:

So it's defined and PHP won't return an error when you try to use it.

$lease_price = '';

if($special>0){
    $lease_price = (($special/1000)*38);
} else {
    $lease_price = (($price/1000)*38);
}
$lease_price = $this->currency->format($lease_price);

Don't use it if not set:

Your code which tries to access the undefined variable won't be accessed if it's not defined.

if ( isset($lease_price) ) {

    if($special>0){
      $lease_price = (($special/1000)*38);
    } else {
      $lease_price = (($price/1000)*38);
    }
  $lease_price = $this->currency->format($lease_price);
}
James
  • 4,644
  • 5
  • 37
  • 48
  • Hi @James I don't mind it just printing NA if the price is less than 500. I've used the code Alex suggested above but it's still printing NA. Do you know what could be the issue here? I'm really struggling getting this working and any help would be greatly appreciated – BC GB Dec 12 '14 at 14:25
  • @BCGB Not sure to be honest. Alex's code is quite a change to your original code, and also this is now a different issue. Your original issue was to resolve an undefined var. Maybe time for a new question? – James Dec 14 '14 at 02:46
0

Try merging it to one, if it won't work any other way :)

The problem will most likely be that it is not passed on properly from your PHP page to your TPL.

<p>
<i class="fa fa-chevron-down"></i>
<b>Lease To Buy Price:</b>
<span>
<?php 
if($special > 0) { 
    $lease_price = (($special/1000)*38); 
} else { 
    $lease_price = (($price/1000)*38); } 
$lease_price = $this->currency->format($lease_price);

if($price > 500) { 
echo $lease_price; 
} else { 
echo 'NA'; } ?></span>                     
</p>

In any normal cases you should check if

if(isset($lease_price)) {
    // this gets executed if $lease_price has a value
} else {
    // and this one if there is no initial value for $lease_price
}

You could handle this error with plenty of different approaches, but I think the key would be to ensure that your PHP code is in the right place, and referenced correctly in your template.

As there was a very similar post earlier on SO, I still think you should check the value of $price by adding this to your template as a temporary error checking <?= $price ?> since it might have already been converted to a string, and the if($price > 500) will never get evaluated as '2,000.00' > 500 will not be true.

Update

$tempPrice = str_replace(',',"", $price); //gets rid of ","
$tempPrice = substr($tempPrice,1); //removes currency from the front
$tempPrice = floatval($tempPrice); //converts to double from string

and replace $price with $tempPrice in if

<p>
<i class="fa fa-chevron-down"></i>
<b>Lease To Buy Price:</b>
<span>
<?php 

$tempPrice = str_replace(',',"", $price); //gets rid of ","
$tempPrice = substr($tempPrice,1); //removes currency from the front
$tempPrice = floatval($tempPrice); //converts to double from string

if($special > 0) { 
    $lease_price = (($special/1000)*38); 
} else { 
    $lease_price = (($tempPrice/1000)*38); } 
$lease_price = $this->currency->format($lease_price);

if($tempPrice > 500) { 
echo $lease_price; 
} else { 
echo 'NA'; } ?></span>                     
</p>
Alex Szabo
  • 3,274
  • 2
  • 18
  • 30
  • Hi @AlexSzabó I've entered the code above in the TPL doc, the lease_price is what the piece of code is trying to work out. So basically, the lease price is the normal price /1000*38. I've added the = $price ?> piece of code and that prints the price no problem. But the lease price is still showing as NA. Is there something I'm missing here? Am I being a complete idiot? – BC GB Dec 12 '14 at 14:02
  • @BCGB What did `= $price ?>` output? `2000` or `2,000.00`? – Alex Szabo Dec 12 '14 at 14:36
  • It outputted the price formatted like this: £2,000.00 – BC GB Dec 12 '14 at 14:38
  • @BCGB Then your `$price` variable is being treated as text, not a number therefore it cannot be compared with the value of 500. Try using what I've just updated above to convert `$price` to a number. – Alex Szabo Dec 12 '14 at 14:46
  • Ah, I see. I've implemented this, but as you can see here: http://www.framec.co.uk/index.php?route=product/product&product_id=50 the product is over £500 but is still showing NA. – BC GB Dec 12 '14 at 15:05