### **فایل جاوااسکریپت (assets/js/wpt-scripts.js)** ```javascript jQuery(document).ready(function ($) { // Loader function showLoader() { $('#wpt-loader').show(); } function hideLoader() { $('#wpt-loader').hide(); } // Search with debounce for performance $('#wpt-search').on('input', _.debounce(function () { var term = $(this).val(); var paged = 1; showLoader(); $.ajax({ url: wpt_ajax.ajax_url, type: 'POST', data: { action: 'wpt_search', nonce: wpt_ajax.nonce, term: term, paged: paged, }, success: function (response) { hideLoader(); if (response.success) { updateTable(response.data, response.current_page, response.total_pages, term); } else { $('#wpt-product-table tbody').html('محصولی یافت نشد.'); $('.wpt-pagination').empty(); } }, error: function () { hideLoader(); alert('خطایی در ارتباط رخ داد. لطفاً دوباره تلاش کنید.'); }, }); }, 200)); // Enhanced Autocomplete with suggestions $('#wpt-search').autocomplete({ source: function (request, response) { $.ajax({ url: wpt_ajax.ajax_url, type: 'POST', data: { action: 'wpt_search_autocomplete', nonce: wpt_ajax.nonce, term: request.term, }, success: function (data) { response(data.data); }, }); }, minLength: 2, select: function (event, ui) { $('#wpt-search').val(ui.item.value).trigger('input'); }, }); // Voice Search if ('SpeechRecognition' in window || 'webkitSpeechRecognition' in window) { const SpeechRecognition = window.SpeechRecognition || window.webkitSpeechRecognition; const recognition = new SpeechRecognition(); recognition.lang = 'fa-IR'; // Persian language recognition.interimResults = false; recognition.maxAlternatives = 1; $('#wpt-voice-search').on('click touchstart', function (e) { e.preventDefault(); recognition.start(); showLoader(); // Show loader during voice recognition }); recognition.onresult = function (event) { const transcript = event.results[0][0].transcript; $('#wpt-search').val(transcript).trigger('input'); hideLoader(); // Hide loader after result }; recognition.onend = function () { hideLoader(); // Ensure loader is hidden when recognition ends }; recognition.onerror = function (event) { hideLoader(); // Hide loader on error alert('خطا در تشخیص صدا: ' + event.error); }; } else { $('#wpt-voice-search').hide(); // Hide if not supported } // Pagination $(document).on('click', '.wpt-pagination a', function (e) { e.preventDefault(); var currentUrl = new URL(window.location.href); var paged = this.dataset.page; // Assume we add data-page to a tags in updateTable currentUrl.searchParams.set('wpt_page', paged); var term = $('#wpt-search').val(); if (term) currentUrl.searchParams.set('wpt_search', term); history.pushState({}, '', currentUrl.toString()); showLoader(); $.ajax({ url: wpt_ajax.ajax_url, type: 'POST', data: { action: 'wpt_search', nonce: wpt_ajax.nonce, term: term, paged: paged, }, success: function (response) { hideLoader(); if (response.success) { updateTable(response.data, response.current_page, response.total_pages, term); } else { $('#wpt-product-table tbody').html('محصولی یافت نشد.'); $('.wpt-pagination').empty(); } }, error: function () { hideLoader(); alert('خطایی در ارتباط رخ داد. لطفاً دوباره تلاش کنید.'); }, }); }); // Update Table function updateTable(data, current_page, total_pages, term = '') { var tbody = $('#wpt-product-table tbody'); tbody.empty(); $.each(data, function (index, product) { var row = $(''); row.append('' + product.title + ''); var priceTd = $('').html(product.price_html ? product.price_html : '-'); row.append(priceTd); var quantityTd = $(''); if (product.stock_status === 'instock') { quantityTd.html('
' + '' + '' + '' + '
'); } else { quantityTd.html('ناموجود'); } row.append(quantityTd); var actionsTd = $(''); if (product.stock_status === 'instock') { actionsTd.html(''); } else { actionsTd.html(''); } row.append(actionsTd); tbody.append(row); }); var pagination = $('.wpt-pagination'); pagination.empty(); if (total_pages > 1) { if (window.innerWidth <= 768) { // Responsive check var pagination_html = ''; pagination.html(pagination_html); } else { var pagination_html = 'صفحه ' + current_page + ' از ' + total_pages + ''; pagination_html += ''; pagination.html(pagination_html); } } } // Quantity controls $(document).on('click', '.wpt-minus', function () { var input = $(this).siblings('.wpt-quantity'); var val = parseInt(input.val()); if (val > 0) { input.val(val - 1); } }); $(document).on('click', '.wpt-plus', function () { var input = $(this).siblings('.wpt-quantity'); var max = parseInt(input.attr('max')); var val = parseInt(input.val()); if (val < max) { input.val(val + 1); } }); // Add to cart $(document).on('click', '.wpt-add-to-cart', function () { var button = $(this); var product_id = button.data('product-id'); var quantity = button.closest('tr').find('.wpt-quantity').val(); if (quantity > 0) { showLoader(); $.ajax({ url: wpt_ajax.ajax_url, type: 'POST', data: { action: 'wpt_add_to_cart', product_id: product_id, quantity: quantity, nonce: wpt_ajax.nonce, }, success: function (response) { hideLoader(); if (response.success) { alert('محصول به سبد خرید اضافه شد.'); button.text('افزوده شد'); setTimeout(function() { button.text('افزودن به سبد خرید'); }, 3000); } else { alert(response.data.message || 'خطایی در افزودن به سبد خرید رخ داد.'); } }, error: function () { hideLoader(); alert('خطایی در ارتباط رخ داد.'); }, }); } else { alert('لطفاً تعداد را انتخاب کنید.'); } }); // Notify me (supports guest) $(document).on('click', '.wpt-notify-me', function () { var button = $(this); var product_id = button.data('product-id'); var email = ''; if (!wpt_ajax.is_logged_in) { email = prompt('لطفاً ایمیل خود را برای اطلاع‌رسانی وارد کنید:'); if (email && !/^[\\w-]+@[\\w-]+\\.[\\w-]+$/.test(email)) { alert('ایمیل معتبر وارد کنید.'); return; } } showLoader(); $.ajax({ url: wpt_ajax.ajax_url, type: 'POST', data: { action: 'wpt_notify_me', product_id: product_id, email: email, nonce: wpt_ajax.nonce, }, success: function (response) { hideLoader(); if (response.success) { alert(response.data.message); } else { alert(response.data.message || 'خطایی در ثبت درخواست رخ داد.'); } }, error: function () { hideLoader(); alert('خطایی در ارتباط رخ داد.'); }, }); }); }); جلوبندی Archives - نیک پلاس
ورود | ثبت‌نام
نمایش فیلترها
سبد خرید

سبد خرید شما خالی است.

ورود به سایت