@ -1,90 +1,90 @@
jQuery ( document ) . ready ( function ( $ ) {
jQuery ( document ) . ready ( function ( $ ) {
// Static translations for both English and Russian
// =====================
var translations = {
// Перевод строк
en : {
// =====================
error _coupon _empty : 'Please enter the coupon code.' ,
// currentTranslations должен быть определен глобально из PHP или другого JS
error _coupon _invalid : 'Error! The coupon code is invalid or does not exist.' ,
// Пример:
error _generic : 'An error occurred. Please try again.' ,
// var currentTranslations = translations[language];
shipping _method _error : 'No pickup point selected for delivery.' ,
shipping _address _error : 'Delivery address not provided' ,
email _taken _error : 'This email is already registered. Please log in or provide a different email.' ,
phone _number _error : 'Incorrect phone number entered.' ,
shipping _method _missing : 'No delivery method selected. Please check your address or contact the site administration for help.' ,
change _address : 'Change address' ,
shipping _updated : 'Shipping methods will be updated during checkout.' ,
coupon _applied : 'Coupon applied: '
} ,
// =====================
ru : {
// Обновление методов доставки
error _coupon _empty : 'Пожалуйста, введите код купона.' ,
// =====================
error _coupon _invalid : 'Ошибка! Промокод введен неверно или не существует.' ,
error _generic : 'Произошла ошибка. Попробуйте ещё раз.' ,
shipping _method _error : 'Не выбран ПВЗ для доставки заказа.' ,
shipping _address _error : 'Не указан адрес доставки' ,
email _taken _error : 'Этот email уже зарегистрирован. Войдите в свой аккаунт или укажите другой E-mail.' ,
phone _number _error : 'Неправильно введен номер' ,
shipping _method _missing : 'Не выбран метод доставки. Пожалуйста перепроверьте ваш адрес или обратитесь за помощью к администрации сайта.' ,
change _address : 'Изменить адрес' ,
shipping _updated : 'Методы доставки будут обновлены в процессе оформления заказа.' ,
coupon _applied : 'Применен промокод: '
}
} ;
// Get the current language (assuming you're using a data-lang attribute on the body or html tag)
var currentTranslations = window . langData . translations
var currentLang = $ ( 'body' ) . data ( 'lang' ) || 'en' ; // Default to 'en' if no lang attribute is found
// Use the correct translations based on the current language
$ ( document . body ) . on ( 'updated_checkout' , function ( ) {
var currentTranslations = translations [ currentLang ] ;
var shippingMethodsHtml = $ ( '.woocommerce-checkout-review-order-table .woocommerce-shipping-methods' ) . html ( ) ;
jQuery ( document . body ) . on ( 'updated_checkout' , function ( ) {
// Найти новые методы доставки внутри shop_table
var shippingMethodsHtml = jQuery ( '.woocommerce-checkout-review-order-table .woocommerce-shipping-methods' ) . html ( ) ;
// Обновить кастомный блок
if ( ! shippingMethodsHtml ) {
if ( ! shippingMethodsHtml ) {
shippingMethodsHtml = '<p>' + currentTranslations . shipping _method _missing + '</p>' ;
shippingMethodsHtml = '<p>' + currentTranslations . shipping _method _missing + '</p>' ;
}
}
jQuery ( '#custom-shipping-methods' ) . html ( shippingMethodsHtml ) ;
jQuery ( '.order-contacts__delivery' ) . removeClass ( 'loading' ) ;
jQuery ( '.modal-map-control__item.active' ) . click ( ) ;
$ ( '#custom-shipping-methods' ) . html ( shippingMethodsHtml ) ;
$ ( '.order-contacts__delivery' ) . removeClass ( 'loading' ) ;
$ ( '.modal-map-control__item.active' ) . click ( ) ;
} ) ;
} ) ;
$ ( document . body ) . on ( 'update_checkout' , function ( ) {
$ ( document . body ) . on ( 'update_checkout' , function ( ) {
jQuery ( '.order-contacts__delivery' ) . addClass ( 'loading' ) ;
$ ( '.order-contacts__delivery' ) . addClass ( 'loading' ) ;
} ) ;
} ) ;
// =====================
// Обработка клика по ПВЗ
// =====================
$ ( 'body' ) . on ( 'click' , '.modal-map-control__item' , function ( ) {
$ ( 'body' ) . on ( 'click' , '.modal-map-control__item' , function ( ) {
if ( ! $ ( this ) . hasClass ( 'active' ) ) {
if ( ! $ ( this ) . hasClass ( 'active' ) ) {
$ ( '#addr_inp' ) . val ( '' ) ;
$ ( '#addr_inp, #pvz_inp, #billing_address_1, #billing_address_2' ) . val ( '' ) ;
$ ( '#pvz_inp' ) . val ( '' ) ;
$ ( '#billing_address_1' ) . val ( '' ) ;
$ ( '#billing_address_2' ) . val ( '' ) ;
}
}
$ ( '.shipping-add-info' ) . removeClass ( 'active' ) ;
$ ( '.shipping-add-info' ) . removeClass ( 'active' ) ;
var remote = $ ( this ) . data ( 'remote' ) ;
var remote = $ ( this ) . data ( 'remote' ) ;
$ ( '.shipping-add-info[data-remote="' + remote + '"]' ) . addClass ( 'active' ) ;
$ ( '.shipping-add-info[data-remote="' + remote + '"]' ) . addClass ( 'active' ) ;
} ) ;
} ) ;
jQuery ( '#addr _inp') . on ( 'click' , function ( ) {
$ ( '#pvz _inp') . on ( 'click' , function ( ) {
jQuery ( '#address_input' ) . addClass ( 'active' ) ;
$ ( '.open-pvz-btn' ) . click ( ) ;
} ) ;
} ) ;
$ ( '#addr_inp' ) . on ( 'click' , function ( ) {
$ ( '#address_input' ) . addClass ( 'active' ) ;
} ) ;
} ) ;
jQuery ( document ) . ready ( function ( $ ) {
// =====================
// Перехват отправки формы купона
// Обработка формы адреса
// =====================
$ ( '#address_input input' ) . on ( 'change input keyup' , function ( ) {
var address = $ ( '#f_address' ) . val ( ) ;
var apart = $ ( '#f_apartment' ) . val ( ) ;
var floor = $ ( '#f_floor' ) . val ( ) ;
var entrance = $ ( '#f_entrance' ) . val ( ) ;
var number _phone = $ ( '#f_number-phone' ) . val ( ) ;
var address _2 = '' ;
if ( apart ) address _2 += ' кв ' + apart ;
if ( floor ) address _2 += ' этаж ' + floor ;
if ( entrance ) address _2 += ' подъезд ' + entrance ;
if ( number _phone ) address _2 += ' домофон ' + number _phone ;
$ ( '#addr_inp' ) . val ( address + address _2 ) ;
$ ( '#billing_address_1' ) . val ( address ) ;
$ ( '#billing_address_2' ) . val ( address _2 ) ;
} ) ;
// =====================
// AJAX отправка купона
// =====================
$ ( document ) . on ( 'click' , 'button[name="apply_coupon"]' , function ( e ) {
$ ( document ) . on ( 'click' , 'button[name="apply_coupon"]' , function ( e ) {
e . preventDefault ( ) ; // Останавливаем отправку основной формы
e . preventDefault ( ) ;
// Отправка данных купона через AJAX
var couponCode = $ ( '#coupon_code' ) . val ( ) ;
var couponCode = $ ( '#coupon_code' ) . val ( ) ;
if ( ! couponCode ) {
if ( ! couponCode ) {
alert ( currentTranslations . error _coupon _empty ) ; // Using translated string
alert ( currentTranslations . error _coupon _empty ) ;
return ;
return ;
}
}
$ . ajax ( {
$ . ajax ( {
url : wc _checkout _params . ajax _url , // URL для AJAX-запроса WooCommerce
url : wc _checkout _params . ajax _url ,
type : 'POST' ,
type : 'POST' ,
data : {
data : {
action : 'apply_coupon' ,
action : 'apply_coupon' ,
@ -96,93 +96,80 @@ jQuery(document).ready(function($) {
$ ( '#promo_form' ) . hide ( ) ;
$ ( '#promo_form' ) . hide ( ) ;
$ ( '#promo_delete' ) . show ( ) ;
$ ( '#promo_delete' ) . show ( ) ;
$ ( '#promo_err' ) . hide ( ) ;
$ ( '#promo_err' ) . hide ( ) ;
$ ( '#coupon_code_app' ) . val ( coupon _applied + ' ' + couponCode ) ;
$ ( '#coupon_code_app' ) . val ( currentTranslations . c oupon _applied + ' ' + couponCode ) ;
$ ( '#delete_coupon' ) . data ( 'coupon' , couponCode ) ;
$ ( '#delete_coupon' ) . data ( 'coupon' , couponCode ) ;
} else {
} else {
$ ( '#promo_err' ) . show ( ) ;
$ ( '#promo_err' ) . show ( ) ;
}
}
} ,
} ,
error : function ( ) {
error : function ( ) {
alert ( currentTranslations . error _generic ) ; // Using translated string
alert ( currentTranslations . error _generic ) ;
}
}
} ) ;
} ) ;
} ) ;
} ) ;
jQuery ( '#pvz_inp' ) . on ( 'click' , function ( ) {
// =====================
jQuery ( '.open-pvz-btn' ) . click ( ) ;
// Обработка ошибок формы чекаута
} ) ;
// =====================
$ ( document . body ) . on ( 'checkout_error' , function ( ) {
$ ( '.form-input__error' ) . removeClass ( 'active' ) ;
$ ( '#address_input input' ) . on ( 'change input keyup' , function ( ) {
$ ( '.woocommerce-error li' ) . each ( function ( ) {
var address = $ ( '#f_address' ) . val ( ) ;
var errorMessage = $ ( this ) . text ( ) . trim ( ) ;
var apart = $ ( '#f_apartment' ) . val ( ) ;
var fieldKey = $ ( this ) . data ( 'id' ) ;
var floor = $ ( '#f_floor' ) . val ( ) ;
var entrance = $ ( '#f_entrance' ) . val ( ) ;
var number _phone = $ ( '#f_number-phone' ) . val ( ) ;
var address _2 = '' ;
if ( fieldKey ) {
var errorContainer = $ ( '#' + fieldKey + '-error' ) ;
if ( errorContainer . length ) {
errorContainer . html ( '<span class="error-message">' + errorMessage + '</span>' ) . addClass ( 'active' ) ;
}
}
// Сопоставление ошибок с переводами
if (
errorMessage === 'phone_err' ||
errorMessage === 'The string supplied did not seem to be a phone number.' ||
errorMessage . includes ( 'Некорректный номер телефона' )
) {
$ ( '#billing_phone-error' ) . html ( currentTranslations . phone _number _error ) . addClass ( 'active' ) ;
}
if ( apart ) {
if (
address _2 += ' кв ' + apart ;
errorMessage === 'Order pickup point not selected.' ||
errorMessage === 'Не выбран ПВЗ для доставки заказа.'
) {
$ ( '#pvz-error' ) . html ( currentTranslations . shipping _method _error ) . addClass ( 'active' ) ;
}
}
if ( floor ) {
address _2 += ' этаж ' + floor ;
if ( errorMessage === 'adress_error' ) {
$ ( '#adr-error' ) . html ( currentTranslations . shipping _address _error ) . addClass ( 'active' ) ;
}
}
if ( entrance ) {
address _2 += ' подъезд ' + entrance ;
if ( errorMessage === 'Неверный адрес эл. почты для выставления счета' ) {
$ ( '#billing_email-error' ) . html ( errorMessage ) . addClass ( 'active' ) ;
}
}
if ( number _phone ) {
address _2 += ' домофон ' + number _phone ;
if ( errorMessage === 'Не выбран метод доставки. Пожалуйста перепроверьте ваш адрес или обратитесь за помощью к администрации сайта.' ) {
$ ( '#billing_city-error' ) . html ( errorMessage ) . addClass ( 'active' ) ;
}
}
$ ( '#addr_inp' ) . val ( address + address _2 ) ;
if ( errorMessage === 'Этот email уже зарегистрирован.' ) {
$ ( '#billing_address_1' ) . val ( address ) ;
$ ( '#acc-error' ) . html ( currentTranslations . email _taken _error ) . addClass ( 'active' ) ;
$ ( '#billing_address_2' ) . val ( address _2 ) ;
}
} ) ;
} ) ;
} ) ;
// =====================
// Удаление купона
// =====================
$ ( '#delete_coupon' ) . on ( 'click' , function ( ) {
$ ( '.woocommerce-remove-coupon[data-coupon="' + $ ( this ) . data ( 'coupon' ) + '"]' ) . click ( ) ;
$ ( '#promo_form' ) . show ( ) ;
$ ( '#promo_delete' ) . hide ( ) ;
} ) ;
} ) ;
// jQuery(document).ready(function ($) {
$ ( '.woocommerce-remove-coupon' ) . on ( 'click' , function ( ) {
// $(document.body).on('checkout_error', function () {
$ ( '#promo_form' ) . show ( ) ;
// $('.form-input__error').removeClass('active');
$ ( '#promo_delete' ) . hide ( ) ;
} ) ;
// $('.woocommerce-error li').each(function () {
} ) ;
// var errorMessage = $(this).text().trim();
// var fieldKey = $(this).data('id');
// if (fieldKey) {
// var errorContainer = $('#' + fieldKey + '-error');
// if (errorContainer.length) {
// errorContainer.html('<span class="error-message">' + errorMessage + '</span>');
// $('#' + fieldKey + '-error').addClass('active');
// }
// }
// if (errorMessage == 'phone_err' || errorMessage == 'The string supplied did not seem to be a phone number.' || errorMessage.includes('Некорректный номер телефона')) {
// $('#billing_phone-error').html(currentTranslations.phone_number_error).addClass('active'); // Using translated string
// }
// if (errorMessage == 'Order pickup point not selected.' || errorMessage == 'Не выбран ПВЗ для доставки заказа.') {
// $('#pvz-error').html(currentTranslations.shipping_method_error).addClass('active'); // Using translated string
// }
// if (errorMessage == 'adress_error') {
// $('#adr-error').html(currentTranslations.shipping_address_error).addClass('active'); // Using translated string
// }
// if (errorMessage == 'Неверный адрес эл. почты для выставления счета') {
// $('#billing_email-error').html(errorMessage).addClass('active');
// }
// if (errorMessage == 'Не выбран метод доставки. Пожалуйста перепроверьте ваш адрес или обратитесь за помощью к администрации сайта.') {
// $('#billing_city-error').html(errorMessage).addClass('active');
// }
// if (errorMessage == 'Этот email уже зарегистрирован.') {
// $('#acc-error').html(currentTranslations.email_taken_error).addClass('active'); // Using translated string
// }
// });
// });
// $('#delete_coupon').on('click', function(){
// $('.woocommerce-remove-coupon[data-coupon=' + $(this).data('coupon') + ']').click();
// $('#promo_form').show();
// $('#promo_delete').hide();
// });
// $('.woocommerce-remove-coupon').on('click', function(){
// $('#promo_form').show();
// $('#promo_delete').hide();
// });
// });