How to get the meta key of a custom field?
-
Hi,
I’m using this plugin combined with WooCommerce Customer / Order / Coupon Export and I need to include my custom fields into CSV, but I need the meta key of the field and I can’t find it.
Here they explain how to add the field, but with the meta key:
https://docs.woocommerce.com/document/ordercustomer-csv-export/#adding-custom-metaBest regards!
-
The meta key is the field name.
Further, you can use the below function to retrieve the data from the database.
get_post_meta($order_id, ‘field_name’, true);
We hope this will help.
Thank you!
I am trying to get one of the fields to appear at the top of email body but I cant get the code above to work.
I am not a coder and do not know the rest of the code that would go around the above to make it work. The field name is: table_number
Sorry but I’m 69 and when I was a young man the cutting edge was a digital watch with two buttons so I do stumble a bit
The below function is used to retrieve the data from the database.
get_post_meta($order_id, 'field_name', true);In which the $order_id is the variable which contains an order ID.
Inorder to display it, you need to echo the output as given below:
<?php echo get_post_meta($order_id, 'table_number', true); ?>Please note that you need to assign order id to the variable $order_id.
We hope this will help.
Thank you!
Hi
Thanks for the very prompt and helpful reply.I guess I need to try and find out the id of the field and replace the “id”
like this if the id was 17.
<?php echo get_post_meta($order_17, ‘table_number’, true); ?>
I did find this (which worked) in the template, I guess it fetches everything (I don’t know what the variables are).
<?php do_action( ‘woocommerce_email_order_meta’, $order, $sent_to_admin, $plain_text, $email );?>
Really appreciate the help
thanks again!
AlanOk I’m understanding a bit more that I did – However
<?php echo get_post_meta($order_id, ‘table_number’, true); ?>
Does not return anything.
<?php echo get_post_meta($order->id, ‘table_number’, true); ?>
That returns the files number but not the value in the field.
<?php do_action( ‘woocommerce_email_order_meta’, $order, $sent_to_admin, $plain_text, $email );?>
Returns the values and labels of all the extra fields created by me.I’m confused?
Alan
Changing the underscore to -> seems to work.
In a Radio button field this does return the ‘Option Value’ so that all seems fine.
Once the underscore thing was sorted the other thing that confused the issue was
In your ‘do action…’ that is added to the emails for a Radio button it returns the ‘Option Text’ (The Label)
This is also the case on your demo site which I checked to see if it was just me.
The text field works is returned properly – I haven’t tried any of the others.
I hope this information is helpful to you – I guess someone who knows very little sometimes views things a little differently.
Thank-you for your patience.
Alan
We believe that you have changed $order_id to $order->id and it is working fine now.
Further, please note that the below hook is the default hook in WooCommerce, which is used to display order meta data. This hook will display the custom meta data as a label and its value.
<?php do_action( 'woocommerce_email_order_meta', $order, $sent_to_admin, $plain_text, $email );?>We hope this helps.
Thank you!
Sadly that does not help – it returns all the meta data fields not just the one I want.
E.G. I want to display the table number in one place on the email and I want to display the Special Notes (a text field) in another
The issue comes when it is either radio buttons or check boxes – the
<?php echo get_post_meta($order->id, ‘table_number’, true); ?>returns the label but not the value in the field.
This means I have to set them both as the same!
-
This reply was modified 5 years, 9 months ago by
mythical.
In the check box and radio fields, each have one field label, field name and options. In the options we have option value and option text. The option value are saved in the order meta against the field name. So If the table_number is the field name, then the below function will display the option value of the saved field.
<?php echo get_post_meta($order->id, 'table_number', true); ?>It will display the option value of the field. So please clarify which parameter you are trying to display using the code.
Further, if you are using the premium version, then as per the WordPress forum policy we can’t answer premium related questions on this forum. Please rasie a ticket through our website. We hope our technical team will be able to help you.
Thank you!
Hi
Thank-you very much for your time to replyI want to be able to have the radio button or checkbox Label to show “Table Three” on the checkout.
I then want the email to show the value “3” as a digit.
I do not have premium version yet as I was trying to get this to work.
Thanks
Alan
Your plugin returns the Field Text not the value
Confirmation email returns the field text
Thanks for your time.
The Option Text provided in the radio field is getting displayed in the email. Unfortunately, it is not possible to show Option Text in the checkout page and Option Value in the email.
We regret we couldn’t be of more help.
If we have misunderstood your exact requirement, please clarify us.
Thank you!
-
This reply was modified 5 years, 9 months ago by
The topic ‘How to get the meta key of a custom field?’ is closed to new replies.