OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Woocommerce - Style the Price decimals and hide if .00

  • Thread starter Thread starter Jaime Matos
  • Start date Start date
J

Jaime Matos

Guest
I have been trying to achieve the following, stylize the woocommerce price decimals (wrap them in a class) in order to make them superscript through CSS editing, but also hide/remove them in case they are .00

There are multiple solutions online unfortunately none seems to work effectively, This is the closest I have managed to make it work:

Code:
add_filter( 'formatted_woocommerce_price', 'dcwd_superscript_wc_formatted_price', 10, 5 );
function dcwd_superscript_wc_formatted_price( $formatted_price, $price, $decimal_places, $decimal_separator, $thousand_separator ) {
    // Leave prices unchanged in Dashboard.
    if ( is_admin() ) {
        return $formatted_price;
    }

// Format units, including thousands separator if necessary.
$unit = number_format( intval( $price ), 0, $decimal_separator, $thousand_separator );
// Format decimals, with leading zeros as necessary (e.g. for 2 decimals, 0 becomes 00, 3 becomes 03 etc).
$decimal = '';
$num_decimals = wc_get_price_decimals();
if ( $num_decimals ) {
    $decimal = sprintf( '%0'.$num_decimals.'d', round( ( $price - intval( $price ) ) * 100 ) );
}

return $unit . '<span class="decimalsev">' . $decimal_separator. $decimal . '</span>';
}


// Remove decimals if .00
add_filter( 'formatted_woocommerce_price', 'dcwd_remove_zero_decimals', 10, 5 );
function dcwd_remove_zero_decimals( $formatted_price, $price, $decimal_places, $decimal_separator, $thousand_separator ) {
/*
    // Leave decimals in on single product page.
    if ( is_product() ) {
        return $formatted_price;
    }
*/ 

    if ( $price - intval( $price ) == 0 ) {
        // Format units, including thousands separator if necessary.
        return $unit = number_format( intval( $price ), 0, $decimal_separator, $thousand_separator );
    }
    else {
        return $formatted_price;
    }
}

The issue with this is that when price is set inclusive of tax and there is a Tax set (in my case 23%) a price that should show as 4€ can show as 3.100€ (3€ and 100 cents), or 10 units of 4€ will show as 39.100€ (39€ and 100 cents)

Does anyone else suggest a better or different way to achieve this? Thank you in advance for the attention and advice.
<p>I have been trying to achieve the following, stylize the woocommerce price decimals (wrap them in a class) in order to make them superscript through CSS editing, but also hide/remove them in case they are .00</p>
<p>There are multiple solutions online unfortunately none seems to work effectively,
This is the closest I have managed to make it work:</p>
<pre><code>add_filter( 'formatted_woocommerce_price', 'dcwd_superscript_wc_formatted_price', 10, 5 );
function dcwd_superscript_wc_formatted_price( $formatted_price, $price, $decimal_places, $decimal_separator, $thousand_separator ) {
// Leave prices unchanged in Dashboard.
if ( is_admin() ) {
return $formatted_price;
}

// Format units, including thousands separator if necessary.
$unit = number_format( intval( $price ), 0, $decimal_separator, $thousand_separator );
// Format decimals, with leading zeros as necessary (e.g. for 2 decimals, 0 becomes 00, 3 becomes 03 etc).
$decimal = '';
$num_decimals = wc_get_price_decimals();
if ( $num_decimals ) {
$decimal = sprintf( '%0'.$num_decimals.'d', round( ( $price - intval( $price ) ) * 100 ) );
}

return $unit . '<span class="decimalsev">' . $decimal_separator. $decimal . '</span>';
}


// Remove decimals if .00
add_filter( 'formatted_woocommerce_price', 'dcwd_remove_zero_decimals', 10, 5 );
function dcwd_remove_zero_decimals( $formatted_price, $price, $decimal_places, $decimal_separator, $thousand_separator ) {
/*
// Leave decimals in on single product page.
if ( is_product() ) {
return $formatted_price;
}
*/

if ( $price - intval( $price ) == 0 ) {
// Format units, including thousands separator if necessary.
return $unit = number_format( intval( $price ), 0, $decimal_separator, $thousand_separator );
}
else {
return $formatted_price;
}
}
</code></pre>
<p>The issue with this is that when price is set inclusive of tax and there is a Tax set (in my case 23%) a price that should show as 4€ can show as 3.100€ (3€ and 100 cents), or 10 units of 4€ will show as 39.100€ (39€ and 100 cents)</p>
<p>Does anyone else suggest a better or different way to achieve this?
Thank you in advance for the attention and advice.</p>
Continue reading...
 

Latest posts

Top