PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, August 7, 2022

[FIXED] How to display unit under the decimal (Woocommerce price)?

 August 07, 2022     decimal, php, price, woocommerce, wordpress     No comments   

Issue

Problem:
I need to display unit under the decimal.
So I separated the decimal part using the following code.
But it shows like this (unit is next to the price):
enter image description here

Would you please let me know how to put the unit under the decimal, like this?
enter image description here

Frontend HTML displays:

<span class="price">
   <span class="woocommerce-Price-amount amount">
      <bdi><span class="woocommerce-Price-currencySymbol">$</span>2<sup>85</sup></bdi>
   </span>
   <span class="uom">ea</span>
</span>

Separate decimal part (functions.php):
add_filter( 'formatted_woocommerce_price', 'ts_woo_decimal_price', 10, 5 );
function ts_woo_decimal_price( $formatted_price, $price, $decimal_places, $decimal_separator, $thousand_separator ) {
    $unit = number_format( intval( $price ), 0, $decimal_separator, $thousand_separator );
    $decimal = sprintf( '%02d', ( $price - intval( $price ) ) * 100 );
    return $unit . '<sup>' . $decimal . '</sup>';
}

Add Unit (WooCommerce Unit Of Measure Plugin: class-wc-uom-public.php):
public function wc_uom_render_output( $price ) {
        global $post;
        // Check if uom text exists.
        $woo_uom_output = get_post_meta( $post->ID, '_woo_uom_input', true );
        // Check if variable OR UOM text exists.
        if ( $woo_uom_output ) :
            $price =  $price . ' <span class="uom">' . esc_attr( $woo_uom_output, 'woocommerce-uom' ) . '</span>';
            return $price;
        else :
            return $price;
        endif;
    }


Thank you.

Solution

Here's a CSS-based solution:

<style>
    .price {
        font-size: 5em;
    }
    sup {
        font-size: .5em;
    }
    .uom {
        position: relative;
        left: -1.4em;
        font-size: .5em;
    }
</style>

Of course, you'll need to play with font-sizes and positioning to suit your specific needs



Answered By - beltouche
Answer Checked By - Robin (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing