Recipe: Ingredients * calculate portions
- This topic has 6 replies, 2 voices, and was last updated 3 years, 9 months ago by
Tobias Haas.
-
AuthorPosts
-
July 11, 2021 at 3:43 PM #29423
Tobias Haas
ParticipantI 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 * servingsAre there already instructions for this or does someone have tips?
Many Thanks
July 12, 2021 at 10:33 AM #29434Long Nguyen
ModeratorHi 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://support.metabox.io/topic/calculation-in-fields-success-error-messages-jquery-version/
July 13, 2021 at 5:11 AM #29462Tobias Haas
ParticipantHey 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.
July 13, 2021 at 9:52 AM #29466Long Nguyen
ModeratorHi,
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.
July 14, 2021 at 5:34 PM #29497Tobias Haas
ParticipantHey 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!
July 14, 2021 at 11:47 PM #29515Long Nguyen
ModeratorHi 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/3ec2a43e6dc24cc8ba5e86474d17965dThen 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.
July 15, 2021 at 12:56 AM #29520Tobias Haas
ParticipantHey Long, thank you very much!
Good to now! 😉I created a Service Request.
Thanks
-
AuthorPosts
- You must be logged in to reply to this topic.