Using Field Values in Calculations - 2 questions

Support General Using Field Values in Calculations - 2 questionsResolved

  • This topic has 4 replies, 3 voices, and was last updated 4 years ago by LeghLegh.
Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #12231
    @mindspark@mindspark
    Participant

    Hi Anh - I'm using MB to create custom fields for a CPT 'Pacakges'. The fields collect data that is then used to create a 'Package' so potential buyers can review a given restaurant that is for sale.

    Aside from the standard form fields included with MB AIO, I also need -

    Percentages (80%, 16%, etc)

    Question #1 - how can I implement fields that recognize numerical input as a percentage? I am using the number field now....

    I also need to create calculations based on field values (100 x 6% = 6). In addition to the data entry fields, I also need to display a few simple calculations to present financial data.

    Question #2 - how do I perform calculations and then display the result?

    thanks!

    #12267
    @mindspark@mindspark
    Participant

    any updates here?

    #12286
    Anh TranAnh Tran
    Keymaster

    Hi Neil,

    Question #1 – how can I implement fields that recognize numerical input as a percentage? I am using the number field now….

    It actually doesn't. You need to write some description to the field to let users know it's a percentage.

    Question #2 – how do I perform calculations and then display the result?

    When you insert the code to the front end, please add some calculation before that:

    $value = rwmb_meta( 'percentage' );
    $value = $value / 100 * $another_field_value;
    echo $value;

    If you're using a page builder like Beaver Builder, I'd suggest writing a custom shortcode to put into a text module in BB. The shortcode might be like this (put in functions.php):

    add_shortcode( 'your_percentage', function( $atts, $content ) {
        $value = rwmb_meta( 'percentage' );
        $value = $value / 100 * $another_field_value;
        return $value;
    } );

    In a text module in BB, simply insert [your_percentage] and your result will appear.

    #12306
    @mindspark@mindspark
    Participant

    Thanks Anh!

    #27762
    LeghLegh
    Participant

    Hi Anh,
    Could you explain how to do the same into a MB View please ?

    Thanks in advance for your help

Viewing 5 posts - 1 through 5 (of 5 total)
  • You must be logged in to reply to this topic.