• Resolved Jeshua

    (@jeshuavs1)


    Hello team of pods. I have a big question. I would like to achieve the following and I have already researched with the documentation but I have not been able to: I have two post types (pods) # 1 called “rate” and # 2 called “currency” the question is that I created a custom field of numerical type in the rate pod (or custom post type) that is called “actualrate” and I want it to be multiplied with a custom field of the pod (or custom post type) “currency” where the custom field is called “value” and then, the result of that, insert it in another custom field of the same pod (currency) where that resutlado I can use it to my liking with a page builder as elementor or others. Thanks in advance.

    • This topic was modified 6 years, 10 months ago by Jeshua.
Viewing 5 replies - 1 through 5 (of 5 total)
  • Thread Starter Jeshua

    (@jeshuavs1)

    I’ve created an initial function and shortcode to only show the value of the “value” field of the “currency” cpt and then use it in the page builder or any place:

    function fields_multiply() {   
        $currency = pods( 'currency' );
        $value = $currency->display('value');
        echo $value;
    }
    add_shortcode( 'fields_total', 'fields_multiply' );

    This is only for test purposes, but didn’t work. The rest is create de variables to create another that shows the result of the multiplication of the “actualrate” field from the “rate” cpt of a certain post of that cpt and “value” field of the “currency” cpt (this is for individual posts that will be created for that cpt).

    The code is inside a custom pods-starter plugin that I found googling. The shortcode woks becuase if I change i.e.: echo 'ANYTHING HERE'; rather than the code above inside the function, the echo shows that result anywhere I want with the shortcode “fields_total”.

    • This reply was modified 6 years, 10 months ago by Jeshua.
    • This reply was modified 6 years, 10 months ago by Jeshua.
    • This reply was modified 6 years, 10 months ago by Jeshua.
    Plugin Contributor Jim True

    (@jimtrue)

    You aren’t passing a post id in that function, so you’ve only opened the object. There is no ‘connection’ to your existing post at that point.

    Check out our Pods object documentation:
    https://pods.io/docs/code/pods/

    You need to at least pass get_the_id() or $post->ID into your pods('currency') object opening. Check the parameters in the documentation above and the examples on that page.

    Thread Starter Jeshua

    (@jeshuavs1)

    Ok… I’ll try it.

    • This reply was modified 6 years, 10 months ago by Jeshua.
    Thread Starter Jeshua

    (@jeshuavs1)

    Thanks!! It works, I was missing the dynamic id to call the custom field for the individual posts of the cpt.

    My final function was this:

    function fields_multiply () { //START FUNCTION
    
    //GET THE MANUAL RATE
        $rate = pods( 'rate', 'manualrate')->find( $params );
        $therate = $rate->field( 'therate' );
    
    //FIND CURRENCIES
        $currency = pods( 'currency', get_the_id() /* THIS IS WHAT I WAS MISSING */ );
        $price = $currency->field( 'price' );
    
    //MULTIPLY AND RESULT
        $totalprice = $therate * $price;
        echo '<p>' . $totalprice . '</p>';
        
    } //END FUNCTION
    // CREATE SHORTCODE
    add_shortcode( 'fields_total', 'fields_multiply' );

    If I was missing something more, please, I’ll be grateful if you tell me.

    • This reply was modified 6 years, 10 months ago by Jeshua.
    • This reply was modified 6 years, 10 months ago by Jeshua.
    • This reply was modified 6 years, 10 months ago by Jeshua.
    • This reply was modified 6 years, 10 months ago by Jeshua.
    Plugin Contributor Jim True

    (@jimtrue)

    Awesome, nice work 😉 And nope, the code looks great.

Viewing 5 replies - 1 through 5 (of 5 total)

The topic ‘Math with custom fields.’ is closed to new replies.