daily pastebin goal
3%
SHARE
TWEET

WooCommerce - Style cents in prices

lorro Jul 9th, 2017 (edited) 138 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2.   // WooCommerce - show cents within a class so they can be a smaller font
  3.   // Brazilian Reals
  4.   add_filter( 'wc_price', 'my_price', 3, 60 );
  5.   function my_price( $return, $price, $args ) {
  6.     // $return = price with currency symbol
  7.     // $price = price as a number
  8.     // $args = array (inc tax, exc tax)
  9.     $price = str_replace( ',', '.', $price );
  10.     $reals = intval( $price );
  11.     $centavos = ( $price - $reals ) * 100 + 0.0001; // add a little bit for rounding errors
  12.     // ensure $centavos has 2 characters
  13.     $centavos_str = str_pad( $centavos, 2, '0' );
  14.     return 'R$'.$reals.'.<span class="centavos">'.$centavos_str.'</span>';
  15.   }
  16.  
  17. // and in custom css:
  18. .centavos {
  19.   font-size: 70%;
  20. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top