You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
cosmopet-architecture/wp-content/themes/cosmopet/modules/profile/module-ajax-controller.php

177 lines
6.3 KiB

<?php
// include_module('profile');
// Сохранить значения полей
add_action('wp_ajax_edit_user', 'save_custom_user_profile_fields');
add_action('wp_ajax_nopriv_edit_user', 'save_custom_user_profile_fields'); //
function save_custom_user_profile_fields() {
// Проверка прав пользователя
wp_update_user( array(
'ID' => get_current_user_id(),
'first_name' => $_POST['name'],
'last_name' => $_POST['l_name'],
) );
$phone = update_user_meta( get_current_user_id(), 'billing_phone', $_POST['phone'] );
}
function ajax_add_pet() {
$current_user = wp_get_current_user();
// Подготовка данных
$pet_name = $_POST['name'];
$old_type = $_POST['old_type'];
if ($old_type == 'ex'){
$old= $_POST['old'];
}
$weight= $_POST['weight'];
$activity = $_POST['activity'];
$pet = $_POST['pet'];
$breed = $_POST['breed'];
$sex = $_POST['sex'];
$user = $current_user->ID;
$post_data = array(
'post_title' => $pet_name,
'post_type' => 'pets',
'post_status' => 'publish',
);
$post_id = wp_insert_post($post_data);
var_dump($post_id);
if ($post_id) {
// Добавление мета-полей
if ($old_type == 'ex'){
update_field( 'old', $old, $post_id );
}
else{
update_field( 'day', $_POST['day'], $post_id );
update_field( 'month', $_POST['month'], $post_id );
update_field( 'year', $_POST['year'], $post_id );
}
update_field( 'weight', $weight, $post_id );
update_field( 'breed', $breed, $post_id );
update_field( 'sex', $sex, $post_id );
update_field( 'type', $pet, $post_id );
update_field( 'user', $user, $post_id );
update_field( 'activity', $activity, $post_id );
if($_POST['sterilized']=='1' && $pet=='cat'){
update_field( 'sterilized', true );
}
else{
update_field( 'sterilized', false );
}
wp_send_json_success('Питомец успешно добавлен!');
} else {
wp_send_json_error('Ошибка при добавлении питомца.');
}
wp_die(); // Завершение работы
}
add_action('wp_ajax_add_pet', 'ajax_add_pet');
add_action('wp_ajax_nopriv_add_pet', 'ajax_add_pet'); // Если нужно разрешить для незалогиненных пользователей
function ajax_edit_pet() {
$current_user = wp_get_current_user();
// Подготовка данных
$pet_name = $_POST['name'];
$old_type = $_POST['old_type'];
if ($old_type == 'ex'){
$old= $_POST['old'];
}
else{
$old_acc = $_POST['day'] . ' ' . $_POST['month'] . ' ' . $_POST['year'];
}
$weight= $_POST['weight'];
$activity = $_POST['activity'];
$pet = $_POST['pet'];
$breed = $_POST['breed'];
$sex = $_POST['sex'];
$user = $current_user->ID;
$post_id = intval($_POST['pet_id']);
if (get_field('user', $post_id) == $user) {
// Добавление мета-полей
if ($old_type == 'ex'){
update_field( 'old', $old, $post_id );
}
else{
update_field( 'old', '', $post_id );
update_field( 'day', $_POST['day'], $post_id );
update_field( 'month', $_POST['month'], $post_id );
update_field( 'year', $_POST['year'], $post_id );
}
update_field( 'weight', $weight, $post_id );
update_field( 'breed', $breed, $post_id );
update_field( 'sex', $sex, $post_id );
update_field( 'type', $pet, $post_id );
update_field( 'activity', $activity, $post_id );
if($_POST['sterilized']=='1' && $pet=='cat'){
update_field( 'sterilized', true );
}
else{
update_field( 'sterilized', false );
}
wp_send_json_success('Питомец успешно отредактирован!');
} else {
wp_send_json_error('Ошибка при редактировании питомца.');
}
wp_die(); // Завершение работы
}
add_action('wp_ajax_edit_pet', 'ajax_edit_pet');
add_action('wp_ajax_nopriv_edit_pet', 'ajax_edit_pet');
add_action('wp_ajax_update_subscription_address', 'handle_subscription_address_update');
add_action('wp_ajax_nopriv_update_subscription_address', 'handle_subscription_address_update');
function handle_subscription_address_update() {
if (!isset($_POST['address_nonce']) || !wp_verify_nonce($_POST['address_nonce'], 'update_subscription_address')) {
error_log('[AJAX] Ошибка nonce');
wp_send_json_error(['message' => pll__('Ошибка безопасности. Обновите страницу.')]);
}
if (!is_user_logged_in()) {
error_log('[AJAX] Пользователь не авторизован');
wp_send_json_error(['message' => pll__('Вы не авторизованы.')]);
}
$subscription_id = intval($_POST['subscription_id'] ?? 0);
$address_1 = sanitize_text_field($_POST['address'] ?? '');
$city = sanitize_text_field($_POST['city'] ?? '');
$comment = sanitize_textarea_field($_POST['comment'] ?? '');
if (!$subscription_id || empty($address_1)) {
wp_send_json_error(['message' => pll__('Недостаточно данных.')]);
}
$subscription = wcs_get_subscription($subscription_id);
if (!$subscription || $subscription->get_user_id() !== get_current_user_id()) {
wp_send_json_error(['message' => pll__('Подписка не найдена или не принадлежит вам.')]);
}
update_post_meta($subscription_id, '_shipping_address_1', $address_1);
update_post_meta($subscription_id, '_shipping_city', $city);
update_post_meta($subscription_id, '_shipping_comment', $comment);
$subscription->set_customer_note($comment);
$subscription->save();
wp_send_json_success(['message' => pll__('Адрес доставки успешно обновлён.')]);
}