Custom Fields
Re-Envisioned
Support General Recipe: Ingredients * calculate portions

  • Creator
    Topic
  • #29423
    Resolved Tobias Haas
    Participant

    I am currently building a page for myself to learn.
    A recipe page where I can save my favorite recipes.

    Everything works so far. I have a "Custom Post Type" called a recipe. And some "Custom Fields" like cooking time, ingredients, portions and cooking instructions

    I would now like to have the number of ingredients calculated
    Ingredients * servings

    Are there already instructions for this or does someone have tips?

    Many Thanks

Viewing 6 replies - 1 through 6 (of 6 total)
  • Author
    Replies
  • #29434
    Long Nguyen
    Moderator

    Hi Tobias,

    If you follow this article to create a Recipe with Meta Box https://metabox.io/wordpress-create-recipe-with-meta-box-plugin/ and want to show the number of ingredients calculated, I recommend using the field number for ingredients instead of WYSIWYG. Then you can calculate the ingredient as well

    $ingredient = rwmb_meta( 'ingredient' );
    $calc_ingredient = $ingredient * 123;

    Or follow this topic https://metabox.io/support/topic/calculation-in-fields-success-error-messages-jquery-version/

    #29462
    Tobias Haas
    Participant

    Hey Long, thank you very much for your answer.
    But what do I do if I want to name my ingredients and specify the number of tomatoes, for example, that are calculated at the same time as the servings?

    1x tomato * number of servings

    If I define the field as a Number, I can no longer enter the name of the ingredient.

    #29466
    Long Nguyen
    Moderator

    Hi,

    Get the number of a specific ingredient in the WYSIWYG is not possible. You can try to use two fields, one number, and one text field in a group and make it cloneable to add more ingredients.

    #29497
    Tobias Haas
    Participant

    Hey Long,
    I have now created a group

    • Label: ingredient_group
    • ID: ingredient_group
    • Type: Group
    • Cloneable: yes

    In this group I have added 4 fields.

    • Label: Number of ingredients
    • ID: number_ingredients
    • Type: number
    • Label: Unit of measure
    • ID: unit
    • Type: Select Advanced
    • Choises: l, ml, g, kg...
    • Label: ingredient
    • ID: ingredient
    • Type: Text
    • Label: calculation
    • ID: calculation
    • Type: number
    • Read only: yes

    Outside of the Group i have another Custom Field

    • Label: portions
    • ID: portions
    • Type: Number

    Im using the Oxygen Builder, so i added this Snippet to the Plugin "Code Snippets"

    
    add_action ( '', function() {
        ?>
       <script type="text/javascript">
          jQuery(document).ready(function($) {
             $('body').on('keyup', '#portions, #number_ingredients', function() {
                var portions = $('#portions').val();
                var number_ingredients = $('#number_ingredients').val();
                var calculation = portions * number_ingredients;
             $('#calculation').val(calculation);
             });
          });
       </script>
    <?php
        });
    

    Unfortunately it does not work... What am I doing wrong?

    Kind Regards and thanks for your Help!

    #29515
    Long Nguyen
    Moderator

    Hi Tobias,

    Use JavaScript code to get the subfield value in a group to calculate is very complicated. The subfield in a group will have an ID like groupID_subfieldID, does not the same as the sample code above. This screen record is an example https://www.loom.com/share/3ec2a43e6dc24cc8ba5e86474d17965d

    Then you will need to create more code if the group field is cloneable. You can contact us here https://metabox.io/contact/ to create a service request for this case. I will assign it to the developer team, they will estimate the job and get back to you with a quote.

    #29520
    Tobias Haas
    Participant

    Hey Long, thank you very much!
    Good to now! 😉

    I created a Service Request.

    Thanks

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