Skip to content

Instantly share code, notes, and snippets.

@damiencarbery
Last active January 2, 2021 17:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save damiencarbery/dae2e4993f62d5ff109c27dc78294de2 to your computer and use it in GitHub Desktop.
Save damiencarbery/dae2e4993f62d5ff109c27dc78294de2 to your computer and use it in GitHub Desktop.
Superscript WooCommerce price decimals - Superscript the decimals of a WooCommerce price. And then rewrite the code to make it better. https://www.damiencarbery.com/2018/10/superscript-woocommerce-price-decimals/
<?php
/*
Plugin Name: Superscript WooCommerce price decimals
Plugin URI: https://www.damiencarbery.com/2018/10/superscript-woocommerce-price-decimals/
Description: Quick experiment with making decimal portion of price superscript.
Author: Damien Carbery
Author URI: https://www.damiencarbery.com
Version: 0.1
*/
add_filter( 'wc_price', 'dcwd_superscript_wc_decimal', 10, 4 );
function dcwd_superscript_wc_decimal( $return, $price, $args, $unformatted_price ) {
$unit = intval( $price );
$decimal = sprintf( '%02d', ( $price - $unit ) * 100 );
return sprintf( '<span class="woocommerce-Price-amount amount"><span class="woocommerce-Price-currencySymbol">%s</span>%d<sup>%s</sup></span>', get_woocommerce_currency_symbol(), $unit, $decimal );
}
<?php
/*
Plugin Name: Superscript WooCommerce price decimals
Plugin URI: Plugin URI: https://www.damiencarbery.com/2018/10/superscript-woocommerce-price-decimals/
Description: Make decimal portion of WooCommerce price superscript.
Author: Damien Carbery
Author URI: https://www.damiencarbery.com
Version: 0.6
WC tested up to: 4.8
*/
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( '<sup>%0'.$num_decimals.'d</sup>', round( ( $price - intval( $price ) ) * 100 ) );
}
return $unit . $decimal;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment