Advertisement
lorro

WooCommerce - show cents within a sup tag

Feb 12th, 2018
270
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.58 KB | None | 0 0
  1. <?php
  2.   // WooCommerce - show cents within a sup tag
  3.   add_filter( 'wc_price', 'my_price', 3, 60 );
  4.   function my_price( $return, $price, $args ) {
  5.     // $return = price with currency symbol
  6.     // $price = price as a number
  7.     // $args = array (inc tax, exc tax)
  8.     $price = str_replace( ',', '', $price );
  9.     $dollars = intval( $price );
  10.     $cents = intval( ( $price - $dollars ) * 100 + 0.0001 ); // add a little bit for rounding errors
  11.     // ensure $cents has 2 characters
  12.     $cents_str = str_pad( $cents, 2, '0' );
  13.     return '$'.$dollars.'<sup>'.$cents_str.'</sup>';
  14.   }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement