Здравствуйте, у меня есть три формы. Две одинаковые для обратного звонка и одна для заказа. Как надо письмо приходит только из формы .delivery_form, из .contacts_form письмо приходит с заголовками, но пустое, а из формы заказа письмо приходит с заголовками из других форм, но тоже пустое. Помогите, ломаю голову долго.
Формы:
<!-- delivery_form -->
<div id="delivery_form">
<div class="form_title">ЗАДАТЬ ВОПРОС</div>
<form action="<?php echo admin_url('admin-ajax.php?action=send_mail'); ?>">
<label for="delivery_name" class="input">
<input name="delivery_name" type="text" id="delivery_name" class="contacts_name" placeholder=" ">
<span class="label">Имя</span>
<span class="border"></span>
</label>
<label for="delivery_tel" class="input">
<input name="delivery_tel" type="tel" id="delivery_tel" class="contacts_tel" placeholder=" ">
<span class="label">Телефон</span>
<span class="border"></span>
</label>
<button type="submit">Отправить</button>
</form>
</div>
<!-- contacts_form -->
<div class="form_wrap">
<div class="form_title">ЗАКАЖИТЕ ЗВОНОК</div>
<form action="<?php echo admin_url('admin-ajax.php?action=send_mail'); ?>">
<label for="contacts_name" class="input">
<input name="contacts_name" type="text" id="contacts_name" class="contacts_name" placeholder=" ">
<span class="label">Имя</span>
<span class="border"></span>
</label>
<label for="contacts_tel" class="input">
<input name="contacts_tel" type="tel" id="contacts_tel" class="contacts_tel" placeholder=" ">
<span class="label">Телефон</span>
<span class="border"></span>
</label>
<button type="submit">Отправить</button>
</form>
</div>
<div class="popup">
<a class="popup-close" href="#"><img src="<?php echo get_stylesheet_directory_uri(); ?>/assets/img/close.svg" alt="x"></a>
<div class="form_title">Заказать</div>
<form action="<?php echo admin_url('admin-ajax.php?action=send_order'); ?>">
<label for="order_name" class="input">
<input name="order_name" type="text" id="order_name" placeholder=" " required>
<span class="label">Имя<sup>*</sup></span>
<span class="border"></span>
</label>
<label for="order_tel" class="input">
<input name="order_tel" type="tel" id="order_tel" placeholder=" " required>
<span class="label">Телефон<sup>*</sup></span>
<span class="border"></span>
</label>
<label for="order_email" class="input">
<input name="order_email" type="email" id="order_email" placeholder=" " required>
<span class="label">E-mail<sup>*</sup></span>
<span class="border"></span>
</label>
<label for="order_message" class="input">
<input name="order_message" type="text" id="order_message" placeholder=" ">
<span class="label">Комментарий</span>
<span class="border"></span>
</label>
<button type="submit">Отправить</button>
</form>
</div>
js:
// форма обратного звонка
var form = $('.contacts form, .delivery form'),
action = form.attr('action'),
formData = {};
form.on('submit', function(e) {
formData = {
contacts_name: $('.contacts_name').val(),
contacts_tel: $('.contacts_tel').val()
};
$.ajax({
url: action,
type: 'POST',
data: formData,
beforeSend: function (data) {
form.find('button[type="submit"], input').attr('disabled', true);
},
error: function(request, txtstatus, errorThrown) {
console.log(request);
console.log(txtstatus);
console.log(errorThrown);
$('form button').html('Ошибка');
},
success: function () {
$('form button').html('Отправлено');
$('form input').val('');
// form.find('button[type="submit"], input').attr('disabled', false);
}
});
e.preventDefault();
});
// форма заказа товара
form = $('.popup form');
form.on('submit', function (e) {
formData = {
order_name: $('#order_name').val(),
order_tel: $('#order_tel').val(),
order_email: $('#order_email').val(),
order_message: $('#order_message').val(),
order_title: $('h1').text()
};
$.ajax({
url: action,
type: 'POST',
data: formData,
beforeSend: function (data) {
$(this).find('button[type="submit"], input').attr('disabled', true);
},
error: function (request, txtstatus, errorThrown) {
console.log(request);
console.log(txtstatus);
console.log(errorThrown);
$('form button').html('Ошибка');
},
success: function () {
$('form button').html('Отправлено');
$('form input').val('');
$(this).find('button[type="submit"], input').attr('disabled', false);
}
});
e.preventDefault();
});
functions.php
add_action( 'wp_ajax_send_mail', 'send_mail' );
add_action( 'wp_ajax_nopriv_send_mail', 'send_mail' );
add_action( 'wp_ajax_send_order', 'send_order' );
add_action( 'wp_ajax_nopriv_send_order', 'send_order' );
function send_mail() {
$contacts_name = $_POST['contacts_name'];
$contacts_tel = $_POST['contacts_tel'];
$to = get_option( 'admin_email' );
remove_all_filters( 'wp_mail_from' );
remove_all_filters( 'wp_mail_from_name' );
add_filter( 'wp_mail_from_name', function($from_name){
return 'plitkin.nn@gmail.com';
});
$headers = array(
'From: Me Myself <me@example.net>',
'content-type: text/html',
'Cc: gagasha7@gmail.com'
);
$content = 'Имя: ' . $contacts_name . ', Телефон: ' . $contacts_tel;
wp_mail( $to, 'Позвонить', $content, $headers );
wp_die();
}
function send_order() {
$order_name = $_POST['order_name'];
$order_tel = $_POST['order_tel'];
$order_email = $_POST['order_email'];
$order_message = $_POST['order_message'];
$order_title = $_POST['order_title'];
$to = get_option( 'admin_email' );
remove_all_filters( 'wp_mail_from' );
remove_all_filters( 'wp_mail_from_name' );
add_filter( 'wp_mail_from_name', function($from_name){
return 'plitkin.nn@gmail.com';
});
$headers = array(
'From: Me Myself <me@example.net>',
'content-type: text/html',
'Cc: gagasha7@gmail.com'
);
$content = 'Имя: ' . $order_name . ', Телефон: ' . $order_tel . ', E-mail: ' . $order_email . ', Комментарий: ' . $order_message;
wp_mail( $to, 'Заказ', $content, $headers );
wp_die();
}