0

I'm trying to modify a bit of PHP in Wordpress theme I did not create and the developer is not very helpful.

I am working on a website for a client to show several property listings and their associated prices. When I enter the number in the editor it strips out all decimals. So that if I enter 16.50 it immediately turns into 1650. I want it to be able to show 2 decimals. I've done a bit of research but I'm not knowledgeable enough in PHP for it to make sense. The developer directed me to adjusting the following code for $price_meta however that simply adds 2 decimals onto the number to show 1650.00. It seems like the decimal is immediately stripped upon entering it. Is there something else I need to be doing to get it to show correctly?

if(!function_exists('ct_listing_price')) {
function ct_listing_price() {
    global $post;
    global $ct_options;

    $ct_currency_placement = $ct_options['ct_currency_placement'];

    $price_prefix = get_post_meta(get_the_ID(), '_ct_price_prefix', true);
    $price_postfix = get_post_meta(get_the_ID(), '_ct_price_postfix', true);

    $price_meta = get_post_meta(get_the_ID(), '_ct_price', true);
    $price_meta= preg_replace('/[\$,]/', '', $price_meta);

    if($ct_currency_placement == 'after') {
        if(!empty($price_prefix)) {
            echo "<span class='listing-price-prefix'>";
                echo esc_html($price_prefix) . ' ';
            echo '</span>';
        }
        if(!empty($price_meta)) {
            echo "<span class='listing-price'>";
                echo number_format($price_meta, 2);
                ct_currency();
            echo '</span>';
        }
        if(!empty($price_postfix)) {
            echo "<span class='listing-price-postfix'>";
                echo  ' ' . esc_html($price_postfix) . ' ';
            echo '</span>';
        }
    } else {
        if(!empty($price_prefix)) {
            echo esc_html($price_prefix) . ' ';
        }
        if(!empty($price_meta)) {
            echo "<span class='listing-price'>";
                ct_currency();
                echo number_format($price_meta, 2);
            echo '</span>';
        }
        if(!empty($price_postfix)) {
            echo  ' ' . esc_html($price_postfix);
        }
    }
}

I have very limited knowledge of PHP so if you have an idea how to help please explain in as simple and prescriptive terms as possible.

Kristin P
  • 35
  • 1
  • 6

2 Answers2

0

Try number_format($price_meta, 2, '.', ''); This should output 16.50

Mimi spo
  • 11
  • 5
0

Depending on international formatting there could be two options.

If you're using , as decimal separator the function would be:

number_format($price_meta, 2, ',', '');

If you require the thousands separator to be . you just add it to the previous function like this:

number_format($price_meta, 2, ',', '.');

Now, on the other hand if you need to use . as decimal separator you have to switch:

number_format($price_meta, 2, '.', '');

And with thousands separator using ,

number_format($price_meta, 2, '.', ',');

Mihail Minkov
  • 2,463
  • 2
  • 24
  • 41