Storing text string via email input in Drupal Commerce checkout - drupal

When a buyer goes through the checkout process as anonymous guest, he specifies his email at the "Order information" step in the input
<input data-drupal-selector="edit-contact-information-email" type="email" id="edit-contact-information-email" name="contact_information[email]" value="" size="60" maxlength="254" class="form-email required" required="required" aria-required="true">
The email is stored in the column mail of the table commerce_order of the database.
I would like to store a text string via this input in the database.
If I fill the input by JS with text string and even change the type attribute of the input from email to text, the data is not stored in the DB. So there is email validating somewhere.
Where is the validating in JS or PHP?
Or how can I resolve the issue?

You should check the hook form_alter.
https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Form%21form.api.php/function/hook_form_alter/9
Also, you should take a look into the drupal checkout docs too:
https://docs.drupalcommerce.org/commerce2/user-guide/checkout/configure-checkout
You should be aware that changing a form type may impact all other places that needs to read this data.
Regarding the hook alter, Here goes an example.
function mymodule_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {
if ($form_id == 'myformID') {
$form['myformMachineName']['myfield']['#type'] = 'textfield';
}
}

Related

Contact form 7 required one of 2 different fields

I have a image upload field and a URL field in CF7 and how can i make it that a user needs to use one of the 2 fields. So select a image or put a link in the URL field?
I already search a little and i saw some options to do it on normal txt fields but that was not working for me i think because of the upload field.
I don't think that CF7 can do this out of the box. You would probably have to go down to code to solve this issue.
Since I don't know how big you form is this could maybe help you out:
https://wordpress.org/plugins/cf7-conditional-fields/ - Plugin for conditional field inside CF 7
https://conditional-fields-cf7.bdwm.be/conditional-fields-for-contact-form-7-tutorial/ - Tutorial how to use it.
So with this plugin you could basically create a dropdown in which the user first has to select if he wants to use an image or use the URL field. After that, only his selection would pop up and he can proceed with the selected option.
If you want to do the coding you can add some JS to the site and check on validation if one of the two fields is filled out or not.
If the plugin solves your issue - great, if not let me know so I can help you out with the code validation.
###Edit since we need code###
So to dynamically alter the required function I found a pretty good post:
Dynamically Disable Contact Form 7 Field Validation
Some Background information:
CF7 does the validation on server side, so working with JS or JQuery won't help in this case. What we are going to do is manipulate the server validation. If one of the fields is filled out correctly we are just putting content in the other field on the validation.
I created a form that looks like this:
The form in CF7 module looks like this:
<label> URL
[url* url ]
<label> Image
[file* image ]
[submit "Send"]
Then you just have to add the following code to your functions.php file of your theme.
function alter_wpcf7_posted_data( $data ) {
if($_FILES['image']['size'] != 0) {
$_POST['url'] = "http://fileupload.com";
}
if($_POST['url'] != "") {
$_FILES['image']['tmp_name'] = "urlprovided";
}
return $data;
}
add_filter("wpcf7_posted_data", "alter_wpcf7_posted_data");
Normally both fields will be empty in the beginning, if one of them has contet the other one gets fake data and the use has only to provide one.
Let me know if this helps.
I think i found the solution with a little help of the code from #Aschi33.
I made the image (upload field) required and the url field not. So there needs to be alwasy a image selected. But with this code:
function alter_wpcf7_posted_data( $data ) {
if($_POST['url'] != "") {
$_FILES['image']['tmp_name'] = "urlprovided";
}
return $data;
}
add_filter("wpcf7_posted_data", "alter_wpcf7_posted_data");
if the URL field is filled in then the required image field is faked to be filled in so then the URL field can be used to submit the form.
And then with this code:
function cf7_custom_url_check( $result, $url ) {
if ($result) {
$regex='/^(?:https?:\/\/)?(?:www\.)?(?:youtu\.be\/|youtube\.com\/(?:embed\/|v\/|watch\?v=|watch\?.+&v=))((\w|-){11})(?:\S+)?$/';
if (!preg_match($regex,$url)) $result=FALSE;
}
return $result;
}
add_filter( 'wpcf7_is_url', 'cf7_custom_url_check', 10, 2 );
I check if its a real video link from YouTube and if not the field will give a error.

Retrieving Values From Gravity Forms for Custom User Meta

I'm a little frustrated with Gravity Forms at the moment.
Here's my setup:
I have a custom registration form created with WPEverest's User Registration plugin. This plugin creates a custom user registration form that seamlessly integrates with WooCommerce, and also saves custom fields to the USER, rather than in a separate table.
I'm trying to use Gravity Forms to allow users to update field's created with WPEverest's Registration Plugin(which at this point is just some custom user meta fields), but I'm having a hell of a time getting the field values.
My biggest issue is that I can't for the life of me figure out what the heck value I'm supposed to pass through get_post_custom_values();.
The code below is what I have so far. This is supposed to pass a value to the user meta field upon form submit, and it does work... but only if I set the value as a manually created variable. It will not work if I try to pull the data from Gravity Forms.
$gravity_form_id = '10'; // Gravity Forms ID
add_action("gform_after_submission_$gravity_form_id", "gravity_post_submission", 10, 2);
function gravity_post_submission ($entry, $form){
$post_id = $entry["post_id"];
//trying to get values from gravity form
$values = get_post_custom_values("field_name???", $post_id);
//updating user meta
update_post_meta($current_user->ID, 'user_registration_body_type', $values);
}
For reference, the form object I'm using is a RADIO type with ID 10, and a parameter name of body_type (I'm using this name as a means to pre-populate the form fields).
Here's how the form field renders:
<div class="ginput_container ginput_container_radio">
<ul class="gfield_radio" id="input_14_10">
<li class="gchoice_14_10_0">
<input name="input_10" type="radio" value="apple" id="choice_14_10_0">
<label for="choice_14_10_0" id="label_14_10_0">Apple</label>
</li>
<li class="gchoice_14_10_1">
<input name="input_10" type="radio" value="full-bust" id="choice_14_10_1">
<label for="choice_14_10_1" id="label_14_10_1">Full Bust</label>
</li>
<li class="gchoice_14_10_2">
<input name="input_10" type="radio" value="hourglass" checked="checked" id="choice_14_10_2">
<label for="choice_14_10_2" id="label_14_10_2">Hourglass</label>
</li>
<li class="gchoice_14_10_3">
<input name="input_10" type="radio" value="pear" id="choice_14_10_3">
<label for="choice_14_10_3" id="label_14_10_3">Pear</label>
</li>
<li class="gchoice_14_10_4">
<input name="input_10" type="radio" value="straight" id="choice_14_10_4">
<label for="choice_14_10_4" id="label_14_10_4">Straight</label>
</li>
</ul>
</div>
TL;DR - What am I supposed to replace form_field??? with in order to retrieve the form field value? What ID or Name am I supposed to use? (ex: input_10, input_14_10, body_type ?) I'll be trying to do the same thing with checkboxes later, but it seemed easier to start with a radio button since it only returns one value.
Or am I approaching this problem all wrong?
Thanks in advance for your help!
Try something like this.
Add form id to the gform_after_submission_. It looks like you are using form id 14 but this code are for form id 10.
use rgar to specify the field id (10)
use update_user_meta
Get the current user ID (This means that you must be logged in before subitting the form.
add_action("gform_after_submission_10", "gravity_post_submission", 10, 2);
function gravity_post_submission ($entry, $form){
//Gets field id 10
$values = rgar( $entry, '10' );
//updating user meta
update_user_meta( get_current_user_id(), 'user_registration_body_type', $values );
}
This is an alternative answer. I know my original question was asking about the ID number, but after a lot of experimentation, I learned that there's no need for this code at all (which addresses the second part of my question, "am I approaching this issue wrong?").
Gravity forms actually has the in-built ability to write data to custom fields (even for users through a registration process), but does not have the ability to create custom fields itself. I finally found this information in the Gravity Forms Documentation.
Doing so is very simple, but you have to have the PRO version.
To do this:
Create a Custom Field for the post type (or in the example's case, the user meta) - you can use something like Advanced Custom Fields for this, or custom code in your functions.php file
Use the Post Custom Field form item from the Advanced form item options in the form builder
From the Post Custom Field's Custom Field Name section, select "Existing" and then the name of the field you created from the dropdown.
Gravity Forms handles the rest.
So while Fredrik's answer is correct, if you are able to create a custom field, you can just use Gravity Forms existing functionality and avoid adding extra code altogether.
Hope this helps someone as the gravity_post_submission hook is... finicky at best - at least in my experience.

How to get a custom value using Contact Form 7 - Dynamic Text Extension

I have a $_SESSION array variable with post ids. Inside foreach loop, I would like to get the posts titles of these ids. Thus so far I have something like this:
sport_title = '';
foreach($_SESSION['sports_post_id'] as $sports_id {
$sport_title = get_the_title($sport_id);
$sports_titles .= $sport_title . "<br />";
}
Now, my problem is that I do not know how to pass it in a custom variable in Contact Form 7 - Dynamic Text Extension plugin.
I have inside my form this field (inside CF7):
[dynamichidden dynamic_sports readonly default:shortcode_attr]
and inside my custom page template php file:
echo do_shortcode('[contact-form-7 id="3561" "CF7_get_custom_field dynamic_sports=\'$sports_titles\'" title="Availability Form EN"]');
Thus, I would like to send these post titles in email.. How can I make it work? thanks in advance
ok I figure it out how to do it! If anyone wants more explanation:
Inside Contact Form 7 - Form tab, I have insert this code:
[dynamichidden dynamic_sports "CF7_GET key='sports_post_id'"]
where key is a standard word (could not change it).
Inside Email tab, you should have this code:
Sports: [dynamic_sports]
Now, inside my Custom template PHP file, I have this shortcode:
echo do_shortcode('[contact-form-7 id="3561" title="Availability Form EN"]');
I also have a form with a hidden input type, with a name sports_post_id and value the id of the current post:
<input type="hidden" value="<?php echo get_the_title( get_the_ID() ); ?>" name="sports_post_id" id="sports_post_id" />
EDITED
Another solution via plugin that extends the CF7, would be the following:
Install Contact Form 7 - Dynamic Text Extension
Copy and paste the form-tag code below and then add it inside the form code block
[dynamichidden page-title "CF7_get_post_var key='title'"]
The above code will add a hidden text input to the form which will pre-populate the page title. This is good to use when you are using the same contact form on multiple pages so you know where the user has submitted the form from. Alternatively, you can display the page URL or slug using one of the below shortcodes instead:
[dynamichidden page-url "CF7_bloginfo show='url'"]
[dynamichidden page-slug "CF7_bloginfo show='url'"]
Displaying the Hidden Dynamic Content Tag Variable in Contact Form 7
Finally, display the hidden dynamic content tag variable in Contact Form 7 form. While you are on the CF7 settings page, click on the "Email" tab and insert this:
[page-title]
If you are using the URL or Slug fields, you these instead:
[page-url]
[page-slug]
In your CF7 Form configuration > Email Tab, you only have to add the desired field between hooks [...]
[dynamic_sports]
This will print the dynamic field value in your email.

Display n characters of Text field from Contact Form 7

I'm not sure if this question was asked but I couldn't find the solution.
I want to display the first 2 characters from the Contact form 7 text field in the mail.
For example, a person types "TODAY" in the text field.
In the email, I would like to display on the first 2 characters which are "TO" in the mail section when the admin receive the email.
How can I achieve this?
You could do something like this
Add this script however you are adding scripts to your page, in a static .js file, or you can add this to the end of your contact form.
<script>
jQuery('input[name="your-name"]').blur(function () {
var s = jQuery(this).val().substr(0, 2);
if (jQuery('#name-value').length) {
jQuery('#name-value').val(s);
} else {
jQuery(this).after('<input name="name-value" id="name-value" type="hidden" value="' + s + '">');
}
});
</script>
Replace 'first-name' with whatever you are using here and replace 'name-value' with what you want to use for the email form.
Then in your admin email, use the form tag
[name-value] and it will show up in the email. Contact form 7 turns all form fields into tags by name.

Dynamicly populate Contact form 7 input fields with current user info when logged in in Wordpress website

I've been trying to figure out how to populate input fields in contact forms on my Wordpress website. I've tried using a plugin called Contact Form 7 dynamic text extension, and tried a pretty simple IF statement with PHP. But i just can't seem to get this to work.
With the plugin, i can only get half of the arguments to work in the shortcode, even then the form doesn't properly validate.
Using PHP as a placeholder="" outputs the code instead of the contents of the variable. for example "$current_user->user_firstname"
Any help on the matter would be much appreciated.
You can install this plugin
Contact Form 7 Dynamic Text Extension
Dynamic text and dynamic hidden fields are available using it.
And you can use the following code to grab the current user info into your field.
[dynamictext dynamicname "CF7_get_current_user"]
Or as mentioned in the answer above:
[dynamictext your-email "CF7_get_current_user key='user_email' "]
Note also that you can use dynamichidden for hidden fields like the following
[dynamichidden your-email "CF7_get_current_user key='user_email' "]
PS: I faced an issue with required hidden fields when I tried to use this dynamichidden*, the shortcode appears on my website as a plain text.
Turns out, i wasn't using the shortcode correctly.
These are the shortcodes i've used in with Contact Form 7 Dynamic text extension:
Naam* <br />
[dynamictext* your-name 'CF7_get_current_user']
Email*
[dynamictext* your-email "CF7_get_current_user key='user_email' "]
Let's say you have an PHP array $gigs, and you want to display it as a drop down list in your form like this:
<label>
<strong>Choose your gig</strong>
[select upcoming-gigs data:gigs]
</label>
[submit]
Simply add this to your functions.php file
add_filter('wpcf7_form_tag_data_option', function($n, $options, $args) {
if (in_array('gigs', $options)){
$gigs = array(
"MAY 07 - NEW ORLEANS, LA",
"MAY 09 - AUSTIN, TX",
"MAY 12 - HOUSTON, TX"
);
return $gigs;
}
return $n;
}, 10, 3);
Now your drop down field should look like this:
Source: https://bdwm.be/dynamically-populate-a-contact-form-7-dropdown-list-or-any-other-input-field/
I have done this before. Instead of using the PHP code as a placeholder. Use jQuery, for example:
$firstname = $current_user->user_firstname;
<script>
$("input").val("<?php echo $firstname; ?>")
</script>

Resources