HDP
HDP

Reputation: 4221

How to get the price value without currency symbol?

I want to get price value without currency symbol in Product Page for OpenCart. I have use following code for that. but, it is not work perfectly.

I have found and used following code. in .tpl file.

<?php
 $pricenocurrency = $price;
 $pricenocurrency  = preg_replace( '/\D/', '', $pricenocurrency  );
 echo $pricenocurrency ;
?>

So, I get following result. but, I want to not remove dot(.) from price.

Default Price = 86.02€

I Got = 8602

I want to = 86.02

Upvotes: 3

Views: 8365

Answers (3)

Ahmed Bermawy
Ahmed Bermawy

Reputation: 2530

You can use this also

<?php
    $pricenocurrency = $price;
    $pricenocurrency  = preg_replace( '/[^0-9.]/', '', $pricenocurrency  );
    echo $pricenocurrency ;
?>

Upvotes: -1

Vidhyut Pandya
Vidhyut Pandya

Reputation: 1619

The above solution works fine, but what if there are multiple currencies on your store.I would recommend to use default opencart functionality. Just do this,

In your controller do the following.

$data['price_without_symbol'] = $this->currency->format($amount,$currency_code,$currency_value,false);

Will result in price without currency symbol. And use it in your .tpl file. Recommended if your store supports multi-currency. Rest for knowledge. :)

Upvotes: 2

splash58
splash58

Reputation: 26153

add point to preg_replace condition

 $pricenocurrency  = preg_replace( '/[^.\d]/', '', $pricenocurrency  );

if text around price could contain digit, make it a little complicated to save only points after digit

(?<!\d)\.|[^\d]

Upvotes: 2

Related Questions