Price format

Support General Price format

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #40083
    JoJo
    Participant

    Hi! I'm trying to format a price number to get the text field look like "4 200 000" in the frontend.
    I try to use the custom sanitize callback as you mentioned here

    I added formatage_prix in the callback field of my price text field and this code:

    <?php 
    add_filter( 'rwmb_meta_boxes', function( $meta_boxes ) {
        $meta_boxes[] = [
            'title' => 'formatage_prix',
            'fields' => [
                [
                    'type'              => 'text',
                    'id'                => 'prix_vente_bien',
                    'name'              => 'Prix de vente',
                    'sanitize_callback' => 'formatage_prix',
                ]
            ],
        ];
        return $meta_boxes;
    } );
    function formatage_prix( $value, $field, $old_value, $object_id ) {
        $number = rwmb_meta( 'prix_vente_bien' );
     // French notation
     $nombre_format_francais = number_format( $number, 2, ',', ' ' );
     echo $nombre_format_francais;
    }

    It doesn't work. Would you be able to help me figure that out? THanks a lot and happy new year!

    #40084
    JoJo
    Participant

    I don't see how to delete my post. SO here is how I finally made it work:

    <?php
    $number = rwmb_meta( 'prix_vente_bien' );
    $nombre_format_francais = number_format( $number, 0, ',', ' ' );
    echo $nombre_format_francais;
    ?>

    in the output field. I still wish I could make it work with the sanitize callback though, so if you can spot my mistake I would be very grateful!

    #40095
    PeterPeter
    Moderator

    Hello Jo,

    To use the sanitize callback, please read more on the documentation https://docs.metabox.io/sanitization/#custom-sanitize-callback

    The callback function will look like this

    function formatage_prix( $value, $field, $old_value, $object_id ) {
     // French notation
     $value = number_format( $value, 2, ',', ' ' ); // use $value to get the field value
     return $value; // and return the field value after formatting
    }
Viewing 3 posts - 1 through 3 (of 3 total)
  • You must be logged in to reply to this topic.