Support Forum
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
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://support.metabox.io/topic/calculation-in-fields-success-error-messages-jquery-version/
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.
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.
Hey Long,
I have now created a group
In this group I have added 4 fields.
Outside of the Group i have another Custom Field
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!
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.
Hey Long, thank you very much!
Good to now! 😉
I created a Service Request.
Thanks