Customizing price format
You may find you need to change the way prices are displayed in your Comparison Sets. Pricing formats are completely customizable. Below are examples you can use to add to your Custom Plugin.
Always trim trailing double zeros
If you always want trailing zeros (ie. $20.00 or kr. 5.000,00) to be remove (ie. $20 or kr. 5.000) then you can use this code:
add_filter( 'dfrapi_price_trim_trailing_double_zeros', '__return_true' );
Remove trailing double zeros only in specific context
This will remove trailing zeros only in your Comparison Sets.
add_filter( 'dfrapi_price_trim_trailing_double_zeros', function ( $trim, Dfrapi_Price $price ) { return ( $price->get_context() === 'compset' ); }, 20, 2 );
Remove trailing double zeros only for Danish currency in specific context
This will remove trailing double zeros only for DKK prices in the your Comparison Sets.
add_filter( 'dfrapi_price_trim_trailing_double_zeros', function ( $trim, Dfrapi_Price $price ) { return ( $price->get_context() === 'compset' && $price->currency->get_currency_code() === 'DKK' ); }, 20, 2 );
Append currency code to end of price in a specific context
This will append the currency code (ie. USD, GBP, EUR, etc...) to the end of your prices in your Comparison Sets.
add_filter( 'dfrapi_currency_format', function ( $format, Dfrapi_Currency $currency ) { return ( $currency->get_context() === 'compset' ) ? $format . ' ({code})' : $format; }, 20, 2 );
Change thousands separator for DKK currency in Comparison Sets
This will change the thousands separator from a "," to an empty space " " for all prices in DKK in your Comparison Sets.
add_filter( 'dfrapi_currency_thousands_sep', function ( $thousands_sep, Dfrapi_Currency $currency ) { return ( $currency->get_context() === 'compset' && $currency->get_currency_code() === 'DKK' ) ? ' ' : $thousands_sep; }, 20, 2 );
Remove symbol from price when viewing a Comparison Set
This removes the symbol (ie. $, ฿, €, etc...) from the price when viewing a Comparison Set.
add_filter( 'dfrapi_currency_format', function ( $format, Dfrapi_Currency $currency ) { return ( $currency->get_context() === 'compset' ) ? '{price}' : $format; }, 20, 2 );
Format price with more complex HTML (v1)
This code gives us more control of the output of the price. This code will replace ",00" with ",-" when viewing DKK prices in a Comparison Set.
add_filter( 'dfrapi_price_formatted_number', function ( $formatted_number, Dfrapi_Price $price ) { if ( $price->get_context() !== 'compset' || $price->currency->get_currency_code() !== 'DKK' ) { return $formatted_number; } return str_replace( ',00', ',-', $formatted_number ); }, 20, 2 );
Format price with more complex HTML (v2)
This formats the whole number and fraction number and symbol with custom HTML markup in all Comparison Sets.
add_filter( 'dfrapi_currency_format', function ( $format, Dfrapi_Currency $currency ) { if ( $currency->get_context() !== 'compset' ) { return $format; } $replacements = [ '{symbol}' => '<strong style="color:red;">{symbol}</strong>', '{price}' => '{whole}<sup style="color:silver;">{fraction}</sup>', ]; return str_replace( array_keys( $replacements ), array_values( $replacements ), $format ); }, 20, 2 );
Don't show decimals in Comparison Sets
This removes the decimal and all numbers after the decimal in all Comparison Sets
add_filter( 'dfrapi_price_decimal_places', function ( int $decimal_places, Dfrapi_Price $price ) { return ( $price->get_context() === 'compset' ) ? 0 : $decimal_places; }, 20, 2 );
Change format for AUD in ALL contexts
This changes the format of how an AUD price is formatted in all contexts. Here we add a space between the symbol and the price
add_filter( 'dfrapi_currencies', function ( $currencies ) { $currencies['AUD']['format'] = '{price} {code}'; return $currencies; } );
Change decimal places for a specific currency
Here we will change the number of decimal places to show from 2 to 0 for UAH prices only.
add_filter( 'dfrapi_price_decimal_places', function ( $decimal_places, Dfrapi_Price $price ) { return ( $price->currency->get_currency_code() === 'UAH' ) ? 0 : $decimal_places; }, 20, 3 );