Le formulaire Gravity devrait extraire les données de MySQL
P粉447495069
P粉447495069 2024-03-30 00:05:24
0
2
363

J'essaie de créer une sorte de fonctionnalité de récupération de mot de passe (ne demandez pas pourquoi).

J'ai un site Web WordPress et j'utilise un formulaire par gravité pour demander aux clients leur adresse e-mail. Je souhaite ensuite utiliser cette adresse pour trouver le produit woo-commerce correspondant auquel la même adresse e-mail est ajoutée en tant qu'attribut personnalisé (pa_e-mail). Je souhaite ensuite utiliser un autre attribut, qui consiste à enregistrer le mot de passe (pas besoin de sécurité ultra renforcée, md5, hachage, etc.) en tant qu'attribut personnalisé (pa_pw) et à renvoyer ce mot de passe à Gravity Forms pour l'envoyer par e-mail à l'utilisateur. De plus, je souhaite envoyer un lien vers le produit à l'aide d'un lien permanent.

Mon code jusqu'à présent est dans function.php et dans le formulaire gravitationnel, j'ai deux champs de texte qui peuvent recevoir un remplissage dynamique. (edit_pw et 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 '';
  }
}

Mais les e-mails que je reçois n'ont que des valeurs nulles là où je souhaite que {Link:6} et {Passwort:7} soient affichés.

P粉447495069
P粉447495069

répondre à tous(2)
P粉262113569

J'ai réécrit le 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;

}

Et j'en suis presque sûr, c'est cette ligne qui provoque "l'erreur" :

$email = rgar($entry, '4');

Si je saisis manuellement l'adresse e-mail sous la forme $email, cela fonctionne comme prévu. Il doit donc y avoir un problème avec Gravity Forms qui ne transmet pas la valeur du champ de texte de saisie de l'e-mail (ID 4).

Des conseils ?

P粉841870942

Je me mets enfin au travail ! Mon problème est que la valeur de mon champ email (ID4) n'est pas accessible dans la variable $enrtry du filtre "gform_field_value_".

Voici mon code de travail :

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;

}
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal
À propos de nous Clause de non-responsabilité Sitemap
Site Web PHP chinois:Formation PHP en ligne sur le bien-être public,Aidez les apprenants PHP à grandir rapidement!