Skip Navigation

[Resolved] Types Custom field values as a drop-down in contact form 7

This thread is resolved. Here is a description of the problem and solution.

Problem: I would like to create a select field with options based on a post's custom field values, and use that field in Contact Form 7.

Solution: Use get_post_meta to get all the values of the custom field for the current post, then loop over those values to build the markup for a select field.

Get the colors postmeta values for this post as an array, then loop over the array items to concatenate the options.
[php]
global $post;
$output = '<select name="color">';
$colors = get_post_meta($post->ID, 'wpcf-color', true); // 3rd param 'true' because we want an array
foreach( $colors as $color) {
  $output .= '<option value="' . $color . '">' . $color . ' </option>';
}
$output .= '</select>';
return $output;

Relevant Documentation:
https://codex.wordpress.org/Function_Reference/get_post_meta
http://php.net/manual/en/language.operators.string.php

This support ticket is created 6 years, 3 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 3 replies, has 2 voices.

Last updated by Christian Cox 6 years, 3 months ago.

Assisted by: Christian Cox.

Author
Posts
#956504
Screenshot_2.png
Screenshot_1.png

I am trying to: I have a contact form on the single page of products (hidden link). in that contact form, there is a drop-down to list the values of a custom field (colour). I'm able to get the values of the custom field using this solution (https://toolset.com/forums/topic/im-trying-to-load-post-titles-in-my-contact-form-im-using-contact-form-7/). But the issue is that the dropdown is listing the values for all products. I need to get the value of the particular product in that custom field.

This product is added under the woocommerce product-cat taxonomy.

Link to a page where the issue can be seen: hidden link and click on enquire button

Can you please help me to achieve this desired output.

#956678

But the issue is that the dropdown is listing the values for all products. I need to get the value of the particular product in that custom field.
Can a single Product have more than one color? Do you want your Users to be able to select a different color? If neither of those is true, then you can do something like this:

global $post;
$output = '<select name="color">';
$output .= '<option value="' . get_post_meta($post->ID, 'wpcf-color', true) . '">' . get_post_meta($post->ID, 'wpcf-color', true) . ' </option></select>';
return $output;
#957225

Hi,
Can a single Product have more than one colour? Yes, for eg: if the product is a pen drive it will be in multiple colours.
and custom field color is a repeated field.

Your solution is only giving me one color of the particular product, even if the product has multiple colors. i want to show all the colors for that particular product in the dropdown.

Please help me on the same.

#958289

Get the colors postmeta values for this post as an array, then loop over the array items to concatenate the options.

global $post;
$output = '<select name="color">';
$colors = get_post_meta($post->ID, 'wpcf-color', true); // 3rd param 'true' because we want an array
foreach( $colors as $color) {
  $output .= '<option value="' . $color . '">' . $color . ' </option>';
}
$output .= '</select>';
return $output;

https://codex.wordpress.org/Function_Reference/get_post_meta
http://php.net/manual/en/language.operators.string.php