I'm trying to make some kind of password recovery feature (don't ask why).
I have a WordPress website and use a gravity form to ask customers for their email addresses. I then want to use that address to find the corresponding woo-commerce product that has the same email address added as a custom attribute (pa_e-mail). I then want to use another attribute, which is to save the password (no need for super strong security, md5, hashing, etc.) as a custom attribute (pa_pw) and send that password back to Gravity Forms to email it to user. Also, I want to send a link to the product using a permalink.
My code so far is in functions.php, and in the gravity form I have two text fields that can receive dynamic fill. (edit_pw and edit_link)
function get_pw_by_email( $email ) { // Get all products $products = wc_get_products( array( 'limit' => -1, 'status' => 'publish', ) ); // Loop through the products foreach ( $products as $product ) { // Get the value of the "pa_e-mail" attribute $pa_email = $product->get_attribute('pa_e-mail'); // Check if the "pa_e-mail" attribute matches the email if ( $pa_email == $email ) { // Get the value of the "pa_pw" attribute $pa_pw = $product->get_attribute('pa_pw'); // Return the value of the "pa_pw" attribute return $pa_pw; // Break the loop break; } } } function get_product_permalink_by_email( $email ) { // Get all products $products = wc_get_products( array( 'limit' => -1, 'status' => 'publish', ) ); // Loop through the products foreach ( $products as $product ) { // Get the value of the "pa_e-mail" attribute $pa_email = $product->get_attribute('pa_e-mail'); // Check if the "pa_e-mail" attribute matches the email if ( $pa_email == $email ) { $permalink = $product->get_permalink(); // Return the permalink return $permalink; // Break the loop break; } } } add_filter( 'gform_field_value_edit_link', 'my_custom_population_function1' ); function my_custom_population_function1($value) { if ( rgpost( 'is_submit_6') ) { // Form has been submitted, so retrieve the values from the database // get mail address from gf field $email = rgar($entry, '4'); // Permalink $link = get_product_permalink_by_email($email); // Output the value of $link to the PHP error log error_log( 'edit_link: ' . $link ); return $link; } else { // Form has not been submitted, so return an empty value return ''; } } add_filter( 'gform_field_value_edit_pw', 'my_custom_population_function2' ); function my_custom_population_function2($value) { if ( rgpost( 'is_submit_6') ) { // Form has been submitted, so retrieve the values from the database // get mail address from gf field $email = rgar($entry, '4'); // Password $password = get_pw_by_email($email); // Output the value of $password to the PHP error log error_log( 'edit_pw: ' . $password ); return $password; } else { // Form has not been submitted, so return an empty value return ''; } }
But the emails I receive only have null values, where I want {Link:6} and {Passwort:7} to be displayed.
P粉2621135692024-03-30 19:40:11
I rewrote the code.
function get_product_permalink_by_email($email) { $products = get_posts(array('post_type' = >'product', 'tax_query' = >array(array('taxonomy' = >'pa_e-mail', 'field' = >'name', 'terms' = >$email)))); if (empty($products)) { return ''; } return get_permalink($products[0] - >ID); } function get_pw_by_email($email) { $products = get_posts(array('post_type' = >'product', 'tax_query' = >array(array('taxonomy' = >'pa_e-mail', 'field' = >'name', 'terms' = >$email)))); $terms = wp_get_post_terms($products[0] - >ID, 'pa_pw', true); if (empty($terms)) { return ''; } return $terms[0] - >name; } add_filter('gform_field_value_edit_link', 'my_custom_population_function1'); function my_custom_population_function1($value, $entry) { // Form has been submitted, so retrieve the values from the database // get mail address from gf field $email = rgar($entry, '4'); // Permalink $link = get_product_permalink_by_email($email); // Output the value of $link to the PHP error log error_log('edit_link: '.$link); return $link; } add_filter('gform_field_value_edit_pw', 'my_custom_population_function2'); function my_custom_population_function2($value, $entry) { // Form has been submitted, so retrieve the values from the database // get mail address from gf field $email = rgar($entry, '4'); // Password $password = get_pw_by_email($email); // Output the value of $password to the PHP error log error_log('edit_pw: '.$password); return $password; }
And I'm pretty sure, it was this line that caused the "error":
$email = rgar($entry, '4');
If I manually enter the email address as $email, it works as expected. So there must be some issue with Gravity Forms that is not passing the value of the email input text field (ID 4).
Any tips?
P粉8418709422024-03-30 16:19:09
I finally started working! My problem is that the value of my email field (ID4) cannot be accessed in the $enrtry variable of the "gform_field_value_" filter.
This is my working code:
function get_product_permalink_by_email($email) { $products = get_posts(array('post_type' =>'product', 'tax_query' =>array(array('taxonomy' =>'pa_e-mail', 'field' =>'name', 'terms' =>$email)))); if (empty($products)) { return ''; } return get_permalink($products[0] ->ID); } function get_pw_by_email($email) { $products = get_posts(array('post_type' =>'product', 'tax_query' =>array(array('taxonomy' =>'pa_e-mail', 'field' =>'name', 'terms' =>$email)))); $terms = wp_get_post_terms($products[0] ->ID, 'pa_pw', true); if (empty($terms)) { return ''; } return $terms[0]->name; } add_action( 'gform_pre_submission_7', 'pre_submission' ); function pre_submission( $entry, $form ) { $entry_id = $entry['id']; global $email; $email = $_POST['input_2']; $edit_link = get_product_permalink_by_email( $email ); if ( $edit_link ) { $edit_link .= 'edit'; } $edit_pw = get_pw_by_email( $email ); $_POST['input_3'] = $edit_link; $_POST['input_4'] = $edit_pw; }