Gravity Forms无法动态填充WooCommerce产品密码及链接问题求助
WordPress密码找回功能问题:Gravity Forms邮件字段为空
我在WordPress站点上实现密码找回功能,流程是:通过Gravity Forms收集用户邮箱,匹配WooCommerce产品中pa_e-mail属性对应的条目,提取该产品的pa_pw属性值和永久链接,填充到Gravity Forms的edit_pw和edit_link字段并发送邮件。但现在收到的邮件里,{Link:6}和{Passwort:7}位置显示为空值,现有代码如下:
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 ''; } }
问题原因
- 未传入
$entry对象:填充函数中直接调用rgar($entry, '4'),但$entry变量并未被传入函数,导致无法正确获取用户提交的邮箱。 - 提交判断逻辑不可靠:依赖
rgpost('is_submit_6')判断表单提交的方式不准确,Gravity Forms有专门的提交钩子可用。 - 遍历全部产品效率低下:
wc_get_products(limit => -1)会拉取所有产品,当产品数量较多时严重影响性能,且没必要。
修复后的代码
// 根据邮箱直接查询匹配的产品(优化查询逻辑) function get_product_by_email( $email ) { $products = wc_get_products( array( 'limit' => 1, 'status' => 'publish', 'meta_query' => array( array( 'key' => 'pa_e-mail', 'value' => $email, 'compare' => '=' ) ) ) ); return !empty($products) ? $products[0] : false; } // 在表单提交前填充字段(钩子后缀的6是你的表单ID,需对应实际ID) add_action( 'gform_pre_submission_6', 'populate_pw_and_link_fields' ); function populate_pw_and_link_fields( $form ) { // 获取用户提交的邮箱(input_4对应表单中邮箱字段的ID,需调整为实际值) $email = rgpost( 'input_4' ); if ( empty($email) ) return; // 获取匹配的产品 $product = get_product_by_email( $email ); if ( $product ) { // 填充edit_pw字段(input_7对应表单中密码字段的ID,需调整) $_POST['input_7'] = $product->get_attribute('pa_pw'); // 填充edit_link字段(input_6对应表单中链接字段的ID,需调整) $_POST['input_6'] = $product->get_permalink(); } }
注意事项
- 字段ID对应:代码中的
input_4、input_6、input_7必须和你Gravity Forms中对应字段的ID完全一致,可在表单编辑页面查看字段ID。 - 移除旧代码:删除原来的
get_pw_by_email、get_product_permalink_by_email函数以及两个gform_field_value_*过滤器,避免代码冲突。 - 性能优化:通过
meta_query直接筛选匹配产品,只返回第一个结果,相比遍历全部产品性能提升显著。
内容的提问来源于stack exchange,提问作者user20947428
相关产品推荐
相关产品推荐

