To display our prices correctly in a slider on Magento I use this line:
<?php $first_amount_before_split = number_format($this->helper('tax')->getPrice($_product, $_product->getFinalPrice()), '2', '.', ','); $my_array = explode(".", $first_amount_before_split);?>
And I echo that line with: <span class="main-price"><?php echo $my_array[0]; ?>,-</span>
But prices above 1000 are displayed as: 1,100,- and I want to display this as 1.100,-
So I would like to change the symbol , for a .
How can I achieve that?
You want to check out number format to accomplish this:
http://php.net/manual/en/function.number-format.php
Assuming this is a localization related thing you are trying to achieve, you should probably use Magento’s built-in localization methods for displaying prices.
Related
I have a database where I need to store the price of items. Now when I get the values from the database, I would like commas added for the thousand value e.g convert 19000 to 19,000.
<?php echo $row['pprice'];?>
How can I change the format?
You can try using this.
echo number_format($row['pprice']);
Not sure if its correct but wont hurt to try and see if it works.
Or create a new variable for the $row['pprice'] so like this.
$commaPrice = $row['pprice'];
echo number_format($commaPrice);
I suggest to use money_format:
$number = 1234.56;
setlocale(LC_MONETARY, 'it_IT.UTF8');
echo money_format('%i', $number) . "\n";
Note that you need to specify your current locale. If you want to use different locales than yours, you must build them install them on the operating system.
$newNumber = number_format($number);
I am trying to use the Progress Bar plugin in wordpress to create a widget that shows a progress bar under the shopping cart of how much more they need to spend in order to get free shipping.
I need to first take the value of the cart:
global $woocommerce;
$price = $woocommerce->cart->get_cart_total();
Secondly Strip the dollar sign and round to nearest integer
?
Thirdly Output the value into the shortcode, so it displays properly into the HTML. As it is now, it outputs this for the width instead of a number.<span class="red" style="width: .$cartnumber%;">
I need this to output a plain number so it will work with the width. Below is the code so far.
<?php
global $woocommerce;
$total = $woocommerce->cart->get_cart_total();
$cartnumber = str_replace("$", "", $total);
echo do_shortcode('[wppb progress='.$cartnumber.' option=red text="Free Shipping"]');
?>
I have enabled PHP execution within widgets for
I have encountered this before, and below is what I have used last.
http://www.php.net/manual/en/class.numberformatter.php
Before I am using preg_match using [0-9\,\s.]+ pattern. Then I have replaced all special characters and letters to blank to get the integer. (not a good solution)
Example code taken from http://www.pagecolumn.com/tool/pregtest.htm
<?php
$ptn = "/[0-9\,\s\.]+/";
$str = "$9,988,776.65 in the United States";
preg_match($ptn, $str, $matches);
print_r($matches);
?>
Output
Array
(
[0] => 9,988,776.65
)
After I realised that it was not a good solution, I revised it to simpler solution using the code below. from: Remove non-numeric characters (except periods and commas) from a string
$number = "$9,988,776.65";
echo preg_replace("/[^0-9]/", "", $number);
Output
998877665
if you did not remove the thousands and decimal symbol it will get the number format of the currency.
Regarding the progress bar, you need to convert the totals to 100%. I assume that your formula is similar below:
(price/free_shipping_minimum)*100
if result goes over 100 then set it to 100%. if you don't want decimals in your percentage cast it to int or use php's round/ceil/floor function.
http://www.php.net/manual/en/function.round.php
I hope this helps a lot.
Why you don't use
WC()->cart->subtotaĺ
Return a float, so no need to remove $ sign
I have a site that on the home page has a feature splash which consists of a couple of sentences of text.
Say if I had a file with about 5-6 different small blocks of text like the above that I wanted to echo out randomly each time someone hits my homepage, what is the best way to do this in php?
Thanks for your help
Initialize the text blocks in an array and choose one using the rand() function.
Read the documentation about file() and use array_rand():
$sentences = file('your-file.txt');
$keyNr = array_rand($sentences, 1);
var_dump($sentences[$keyNr]);
I would suggest storing your texts in a database and then selecting a random row from it with a query like the following:
select randomQuote from someTable order by rand() limit 1
This way you will never have to change your code, and you can simply update, insert new rows into the database or delete old ones etc.
Just use array_rand() of PHP
<?php
$strings = array('Welcome Ryan', 'Welcome John', 'Welcome Mark', 'Welcome Mike', 'Welcome Jenna');
echo $strings[array_rand($strings)];
I have following code which displays the price in Magento.
<?php echo $this->getPriceHtml($_product); ?>
I have to put this code inside echo instead and I can't get it to work.
I am trying
echo "$this->getPriceHtml ($_product)";
It just displays () on the page.
I tried other combinations and I can't come up with anything else.
What am I doing wrong?
Using single quotes will prevent $vars to be interpreted:
echo '$this->getPriceHtml ($_product)';
Or escape the $ sign:
echo "\$this->getPriceHtml (\$_product)";
http://php.net/manual/en/language.types.string.php
Or if by echo-ing you mean that you want to get something like
The price is 123.00
then do:
echo "The price is {$this->getPriceHtml($_product)}";
or even :
echo sprintf("The price is %s", $this->getPriceHtml($_product));
why not you are using this ?
$_product->getFinalPrice() or
if you want in formatted order then why you are not using this
number_format($pr->getPrice(), 2)
and if you want price with currency format then you can use this also
Mage::helper('core')->currency($pr->getPrice());
Hope it will help you.
:)
im using drupal,
I have a field, where users specify a price.
Ive printed the price field in my tpl.
<?php print $node->my_form['amt'] ?>
but the price comes out like 0.050, How do I get this to display £0.50?
See money_format() or see sprintf() or the printf variant:
<?php printf('£%01.2f', $node->my_form['amt']) ?>
Output: £0.05
Demo
See money_format().
you might want to check the details here http://www.drupalcommerce.org/faq/currency-format and make sure you have a currency formatter configured/chosen.
Depending also on how you're geting your prices, you might want to see this issue http://www.drupalcommerce.org/node/633 with the solution.