Home > database >  place wc_price (formatted price) inside an input text woocommerce
place wc_price (formatted price) inside an input text woocommerce

Time:09-09

I have a text input field that has as its value a shortcode that shows the total price that a customer has spent in our store. This total is returned with a wc_price format so that it respects the decimal format with "," and the currency symbol.

The problem I have is that when the shortcode returns the value, as it returns a formatted price, inside the input text the label is displayed and outside the input text the total price is displayed.

enter image description here

If I return the value without the wc_price format, the value is displayed correctly inside the input text, but the decimal is displayed with "." instead of "," and the currency will not be displayed either.

How can I do so that the correct value is returned within the input text and the span tag is not shown?

This is my code to get the total expense accumulated by a client and create the shortcode:

add_shortcode('user_total_spent', 'get_user_total_spent');
function get_user_total_spent( $atts ) {
    extract( shortcode_atts( array(
        'user_id' => get_current_user_id(),
    ), $atts, 'user_total_spent' ) );

    if( $user_id > 0 ) {
        $customer = new WC_Customer( $user_id ); // Get WC_Customer Object

        $total_spent = $customer->get_total_spent(); // Get total spent amount

        return wc_price( $total_spent ); // return formatted total spent amount
    }
}

And this is my code to display the input text with the value of the total:

add_action( 'woocommerce_account_informes_endpoint', 'ani_populate_informes_page' );
function ani_populate_informes_page() {
    $totalGasto = do_shortcode('[user_total_spent]');
    echo '
        <h3>Informes</h3>
        <p>Aquí puedes encontrar un informe del gasto que has realizado en nuestra tienda.</p>
        
        <div>
            <label for="gastototalclientemicuenta">Gasto total:</label>
            <input type="text" id="gastototalclientemicuenta" name="gastototalclientemicuenta" value="'. $totalGasto .'" readonly> <br>
        </div>
        
        ';
  }

CodePudding user response:

use wc_format_localized_price function instead of wc_price

  • Related