I am trying to make some sort of password recovery function (don't ask why).
I have a WordPress site and use Gravity Forms to ask for the client's mail address. Then I 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). Then I want to use another attribute, that is holding a password (no need for super strong security, md5, hashes, etc) as a custom attribute (pa_pw) and send that password back to Gravity Forms to send it via mail to the user. Also, I want to send the link to that product, using the permalink.
The code I have so far is inside the functions.php and in Gravity Forms, I have two textfields that can receive dynamic fill. (edit_pw & 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 mail I receive just has empty values, where I want {Link:6} & {Passwort:7} to appear.
I finally got it to work! My problem was, that the value of my email field (ID4) was not accessible in the $enrtry variable of my "gform_field_value_" filters.
Here 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;
}