2

I have made a new attribute called RRP. It has exactly the same settings as the Price attribute. But when I var_dump the value inside the attribute it is displaying as a string when I need it to be a float so I can calculate a percentage.

This is what I currently have:

$RRP = Mage::helper('core')->formatPrice($product->getrrp_price());

Which formats the price.

Then I use

$rrp_String =  $value = preg_replace('/^\D+/u', '', $RRP);

To make it into a string without the £ so I can have an if-statement to check if it is over 0. How could I change this line to convert $RRP into a float instead of a string?

So I want to replace the $rrp_String line with something that will convert $RRP into a float as then I can use it in the same if-statement as well as when I'm trying to calculate percentages using the RRP

Edit

I ended up using:

<?php
$RRP = $product->getrrp_price();
$rrp_float = floatval($RRP);
?>

Without $rrp_String = $value = preg_replace('/^\D+/u', '', $RRP);

This seemed to have solved the problem

4
  • Why do you need a float in order to see if the value is > 0? Commented Feb 23, 2015 at 17:16
  • Sorry for not being clear i don't need it for that reason as the If statement i already have works fine but when it comes to trying to calculate a percentage using the RRP it wont work as it isnt a number
    – Adam Allen
    Commented Feb 23, 2015 at 17:18
  • PHP will convert a string like '1.5' to float automatically when needed: var_dump('1.5' / 2); outputs double(0.75) Commented Feb 23, 2015 at 17:26
  • But when i var_dump but rrp attribute ($RRP) it prints out a string?
    – Adam Allen
    Commented Feb 23, 2015 at 17:28

1 Answer 1

2

Not quite sure if i'm just misunderstanding your question (sorry if I have) but there is no need to format the price if you're just checking the value.

Just use:

$price = $product->getrrp_price();
if ($price > 0) {
    $rrp = Mage::helper('core')->formatPrice($price);
}

Think you're over-complicating what you're trying to do :-)

You can cast an integer (or resulting string provided it is just numbers) as a float with:

(float)$rrp;
9
  • Im displaying the Price as well however your last bit of your answer maybe what im looking for. will just check and let you know
    – Adam Allen
    Commented Feb 23, 2015 at 17:21
  • I already have the attribute $RRP which is being pulled from the backend of magento and it is being pulled in as a string, i need to convert it into a float
    – Adam Allen
    Commented Feb 23, 2015 at 17:27
  • In which case I would has it a guess this is a custom attribute that you've added that has its type set to 'text field'. If you look in manage attributes for the rrp_price attribute, and let me know.
    – evensis
    Commented Feb 24, 2015 at 11:40
  • Yea it is a custom attribute i made and th input type in set to Price
    – Adam Allen
    Commented Feb 24, 2015 at 16:29
  • Okay, so following this down, your using $RRP = Mage::helper('core')->formatPrice($product->getrrp_price()); Which returns a string, you then regex it to take the £ sign off? If: $product->getrrp_price(); is returning a string, the problems further up the line. It should return an int/double.
    – evensis
    Commented Feb 25, 2015 at 9:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.