2017-08-12 107 views
0

我試圖改變API調用後聯繫表單7的結果的狀態,所以如果需要我可以在前端返回一個錯誤(即默認情況下,它應該在表單下顯示一個紅色錯誤從Ajax響應)動態更改CF7狀態

我使用Forms3rdPartyIntegration插件,但這只是給了我一個回調掛鉤,我可以再嘗試改變CF7輸出(https://github.com/zaus/forms-3rdparty-integration

至於我能看到CF7狀態是隻讀的?我看不到的方式來阻止CF7只是給一個mail_sent_ok狀態

add_action('Forms3rdPartyIntegration_service', array(&$this, 'service_callback'), 10, 2); 

public function service_callback($response, $results) {   

    $submission = WPCF7_Submission::get_instance(); 
    $cf7 = WPCF7_ContactForm::get_current(); 

    // check for errors (code omitted) 
    // this is what I am essentially trying to do 
    // but doesn't work 
    $submission->status = 'mail_failed' 
    $cf7->skip_mail = true; 

    ... 
} 

我會很感激,如果任何人對觸發CF7失敗響應任何指針。

這似乎是一個類似的問題 wordpress invalidate cf7 after api call

回答

1

我知道這是一個老問題,但任何人誰遇到這個問題,我認爲你正在尋找這樣的:

if(your_condition) { 
    add_filter("wpcf7_ajax_json_echo", function ($response, $result) { 
     $response["status"] = "mail_sent_ng"; 
     $response["message"] = "Validation errors occurred. Please confirm the fields and submit it again."; 
     return $response; 
    }); 
} 

這使狀態mail_sent_ng而不是mail_ok_sent

另外$response["message"]設置顯示給用戶的錯誤/ ajax消息。

由於您的代碼中已經有$cf7->skip_mail = true,因此您已停止發送郵件,並且使用上面的代碼,您向用戶顯示有錯誤。

您也可以使用狀態validation_error