Advertisement
lorro

WooCommerce - Style cents in prices

Jul 9th, 2017
744
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.71 KB | None | 0 0
  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. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement