Search code examples
phpwoocommercenumber-formatting

PHP return price with decimal separator


Could you please help me out how I would be able to customize the output of the below code which was added to WordPress/Woocommerce? I would like to have the return values (regular and sales price) separated with a decimal separator (dot) after the thousands, like for example "1.200".

The code:

function sr_change_variable_price($price, $product){
    $html_tag = '<span id="reg_price">%s</span><span id="sale_price">%s</span>';
    if($product->is_type('variable') AND !is_product()){
        $test = $product->get_variation_regular_price('min') . ' Ft' ;
        $min = $product->get_variation_sale_price('min') . ' Ft';
        return(sprintf($html_tag, $test, $min));
    }elseif($product->is_type('variable') AND is_product()){
        return('');
    }else
    {return $price;  // other cases
    }
}
add_filter('woocommerce_get_price_html', 'sr_change_variable_price', 10, 2);

Thank you very much for any help


Solution

  • You can simply use number_format for that.

    Example:

    $price = number_format(1200, 0, '', '.');
    echo $price; // outputs 1.200