• Hi!

    I have this code to add a shortcode to display Product price:

    function so_30165014_price_shortcode_callback( $atts ) {
    
    $atts = shortcode_atts( array(
    
    'id' => null,
    
    ), $atts, 'bartag' );
    
    $html = '';
    
    if( intval( $atts['id'] ) > 0 && function_exists( 'wc_get_product' ) ){
    
    $_product = wc_get_product( $atts['id'] );
    
    $number = number_format($_product->get_price(), 2, ',', '.');
    
    $html = $number . " €";
    
    }

    And this code to hide the decimal number in product price if they would be 0:

    add_filter('woocommerce_price_trim_zeros', 'wc_hide_trailing_zeros', 10, 1);
    
    function wc_hide_trailing_zeros( $trim ) {
    
    return true;
    
    }

    Now, the product price that WooCommerce shows is correct. However on the single product page, which I made custom, the price still displays the decimals (for example: 2.460,00 €).

    How would I make the second code take effect on the first one?

    Thank you!

    • This topic was modified 3 years, 4 months ago by Jan Dembowski. Reason: Moved to Fixing WordPress, this is not an Everything else WordPress topic

    The page I need help with: [log in to see the link]

Viewing 2 replies - 1 through 2 (of 2 total)
  • you need to use wc_price() to format the price. woocommerce_price_trim_zeros hook is part of that function so hiding zeroes will only work when you’ll use the wc_price function to format price.

    instead of using these 2 lines

    $number = number_format($_product->get_price(), 2, ',', '.');
    
    $html = $number . " €";

    use this only

    $html = wc_price( $_product->get_price() );

    Thread Starter jazzu

    (@jazzu)

    Hi @vijayhardaha !

    This works great, thank you for taking your time to write this!

    Thank you very much!

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Added 2 functions, how do I make 2nd one target the 1st one?’ is closed to new replies.