Skip to content

Instantly share code, notes, and snippets.

@artikus11
Last active January 31, 2022 09:59
Show Gist options
  • Save artikus11/2c6b6250ece223e9dc55299f98cf8bf3 to your computer and use it in GitHub Desktop.
Save artikus11/2c6b6250ece223e9dc55299f98cf8bf3 to your computer and use it in GitHub Desktop.
Contact Form 7 подмена тела письма собственным шаблоном
add_action( 'wpcf7_before_send_mail', 'replace_email_body', 10, 3 );
/**
* @param WPCF7_ContactForm $contact_form
* @param bool $abort
* @param WPCF7_Submission $submission
*
* @return void
*/
function replace_email_body( $contact_form, $abort, $submission ): void {
$mail_body = $submission->get_posted_data();
$product_id = $mail_body['product_id'];
$product_qty = $mail_body['product_qty'];
$product = wc_get_product($product_id);
$text = ! empty( $mail_body['text'] ) ? sanitize_text_field( wp_unslash( $mail_body['text'] ) ) : '';
$email = ! empty( $mail_body['email'] ) ? sanitize_text_field( wp_unslash( $mail_body['email'] ) ) : '';
$tel = ! empty( $mail_body['tel'] ) ? sanitize_text_field( wp_unslash( $mail_body['tel'] ) ) : '';
$mail = $contact_form->prop( 'mail' );
ob_start();
load_template(
dirname( __FILE__ ) . '/templates/email.php',
true,
[
'mail' => [
'name' => $text,
'email'=>$email,
'phone'=>$tel,
],
'product' => $product,
]
);
$mail['body'] = ob_get_clean();
$contact_form->set_properties( [ 'mail' => $mail ] );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment