• Resolved youknowmenot

    (@youknowmenot)


    Greetings. ACF has a plugin called “Calculated fields for ACF”. Does Pods have an easy way to perform simple math from values entered in custom fields? For example I add Item 1 and put a cost for it of $10 (in a custom field). Then I add Item 2 and put a price of $5 for it. What I need is a total value number of $15 to be displayed somewhere on my site and be dynamically generated after each value added from another item.

    Any help is very appreciated.

Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Author Jory Hogeveen

    (@keraweb)

    Hi @youknowmenot

    I do not know of any similar Pods addon but you could create a simple tool for this yourself by using hooks.

    Example:

    add_action( 'save_post_YOURPOSTTYPE', function( $id, $post ) {
    	$value_1 = get_post_meta( $id, 'field_1', true );
    	$value_2 = get_post_meta( $id, 'field_2', true );
    
    	$new_value = $value_1 * $value_2; // Do some math here.
    
    	update_post_meta( $id, 'calculation_field_name', $new_value );
    } );

    Cheers, Jory

    Thread Starter youknowmenot

    (@youknowmenot)

    Hmmm. Thank you for the answer but I’m still not sure how to pull this off.

    Here is precisely what I need: I’m a collector and I upload my collection in parts. I have created a custom post type called “spotlight” where I upload single parts of my collection. I have created a custom field “price” where I put how much I’ve spent for each particular piece in my collection (for each spotlight post). What I need is to calculate all those fields together and display them in another loop, or outside of the loop – whatever is easier/better. Like at the top of my template. “Total value of the collection: $ the_total_value”

    In the code you provided doesn’t it just calculate two different fields and display them in a 3rd one?

    Thread Starter youknowmenot

    (@youknowmenot)

    Ok, after a ton of googling I managed to come up with a working code. I’m pretty bad at PHP so I’m fairly sure someone can write a better one, but I’m pasting it anyway in case it might help someone else:

    
    $spotlightNum = 0; 
    $args = array( 'post_type' => 'spotlight', 'posts_per_page' => -1 );
    $loop = new WP_Query( $args );
    while ( $loop->have_posts() ) : $loop->the_post();
        $spotlighttotalvalue       =   get_post_meta(get_the_ID(), "total_value", true);
        if (!empty($spotlighttotalvalue)){
            $spotlightNum += $spotlighttotalvalue;
        }
    endwhile;
    echo $spotlightNum;

    `

    • This reply was modified 1 year, 7 months ago by youknowmenot.
    Plugin Author Jory Hogeveen

    (@keraweb)

    Hi @youknowmenot

    My code was indeed merely an example of how you can automatically populate a field based on another field.
    The mathematics you use in there could be anything.

    So far I can see you just calculate a total value of all the spotlight items in your installation. If that is what you need then yep, that’s the wat to do it!

    If you need this to cache then you could still hook it into an update filter and (for example) populate a global option with the total value: https://developer.wordpress.org/reference/functions/update_option/

    Cheers, Jory

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Simple math calculations from field value?’ is closed to new replies.