简体   繁体   English

Woocommerce - 使用 ajax 表格显示通知

[英]Woocommerce - Display notice with ajax form

I'm editing the woocommerce form-edit-account.php template.我正在编辑 woocommerce form-edit-account.php 模板。 The template contains a form that allows users to change the settings (name, surname, etc.).该模板包含一个允许用户更改设置(姓名、姓氏等)的表单。 Originally the form does not perform ajax requests, so to save the settings without refreshing the page I added ajax requests.原来表单不执行 ajax 请求,所以为了保存设置而不刷新页面我添加了 ajax 请求。 Everything works fine except handling error.一切正常,除了处理错误。 When a field is left blank or not respected in its conditions, woocommerce error messages do not appear.当字段留空或在其条件中不受尊重时,不会出现 woocommerce 错误消息。

I found a similar post here, but couldn't figure out how to fit it according to my form.: Display woocommerce notice through Ajax callback on product page我在这里找到了一个类似的帖子,但无法根据我的表格弄清楚如何适应它。: 通过产品页面上的 Ajax 回调显示 woocommerce 通知

My problem is that the form continues to work normally, when the fields are not respected I do not see any error message after submitting.我的问题是表单继续正常工作,当不尊重字段时,提交后我没有看到任何错误消息。 However when the page is reloaded the messages are displayed correctly.但是,当页面重新加载时,消息会正确显示。 So I'm getting the messages inserted in functions.php but only after page refresh and not at submit time.因此,我收到了插入到 functions.php 中的消息,但仅在页面刷新后而不是在提交时。 Anyone help me figure out what am I wrong?任何人都帮我弄清楚我错了什么? I appreciate any help and thank you for any replies.感谢您的任何帮助,并感谢您的任何回复。

here's what i did这就是我所做的

Example form (form-edit-account.php)示例表单(form-edit-account.php)

form name="Form" class="mts-edit-account" action="<?php echo admin_url('admin-ajax.php'); ?>" method="post" enctype="multipart/form-data" <?php add_action( 'woocommerce_edit_account_form_tag', 'action_woocommerce_edit_account_form_tag' );?> > 
  <!-- Fist & Last Name Field -->
  <div class="row name_surname">
    <div class="form-row">
      <label class="t3" for="account_first_name">Nome *</label>
      <input type="text" placeholder="Inserisci il tuo nome" class="field-settings" name="account_first_name" id="account_first_name" value="<?php echo esc_attr( $user->first_name ); ?>" />
    </div>

    <div class="form-row">
      <label class="t3" for="account_last_name">Cognome *</label>
      <input type="text" placeholder="Inserisci il tuo cognome" class="field-settings" name="account_last_name" id="account_last_name" value="<?php echo esc_attr( $user->last_name ); ?>" />
    </div> 

    <!-- Save Settings -->
    <p style="margin-bottom: 0px!important;">
      <?php wp_nonce_field( 'save_account_details', 'save-account-details-nonce' ); ?>
      <button type="submit" class="edit-account-button" name="save_account_details" value="<?php esc_attr_e( 'Save changes', 'woocommerce' ); ?>"><?php esc_html_e( 'Salva modifiche', 'woocommerce' ); ?></button>
      <input type="hidden" name="action" value="save_account_details" />
    </p>
  </div>
</form>

Js File JS文件

jQuery(document).ready(function($) {
    
    $('.mts-edit-account').on('submit', function(e) { 
        e.preventDefault(); 

    //Ajax function
    jQuery.ajax({
      
      type: "post",
      data: { action: 'save_account_details' },
      url: "wp-admin/admin-ajax.php",
      success : function( response ) {
        $('woocommerce-notices-wrapper').append(response);
      },

      error: function(error) {
        console.log(error);
      }  

    });
    });
});

functions.php功能。php

add_action( 'wp_ajax_save_account_details', 'save_account_details' );
add_action( 'wp_ajax_nopriv_save_account_details', 'save_account_details' );
add_action( 'woocommerce_save_account_details_errors','save_account_details', 10, 1 );
function save_account_details($args) {

  if (trim($_POST['account_first_name']) == '') {
    $response = wc_add_notice("Field Name Required", "notice");
  } else {
    $response = 'Settings Saved!';
  }

  // Don't forget to exit at the end of processing
  echo json_encode($response);
  exit();

}

wc_add_notice() adds the notice in session in order to be printed after the reload. wc_add_notice()在 session 中添加通知,以便在重新加载后打印。

in your case you need to use the printing function wc_print_notices()在您的情况下,您需要使用打印 function wc_print_notices()

It should be like that.应该是这样的。

wc_add_notice("Field Name Required", "notice");
$response = wc_print_notices( true );

also, You are using 'wp_ajax_nopriv' that means any non logged-in user can send that request and change the settings.此外,您正在使用'wp_ajax_nopriv' ,这意味着任何未登录的用户都可以发送该请求并更改设置。 you need to remove that.你需要删除它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM