#### فایل `wpt-ajax.js` (اصلاحشده برای شماره صفحات کامل)
```javascript
jQuery(document).ready(function ($) {
// Loader
function showLoader() {
$('#wpt-loader').show();
}
function hideLoader() {
$('#wpt-loader').hide();
}
// Search
$('#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);
} else {
$('#wpt-product-table tbody').html('
محصولی یافت نشد. |
');
$('.wpt-pagination').empty();
}
},
error: function () {
hideLoader();
alert('خطایی در ارتباط رخ داد. لطفاً دوباره تلاش کنید.');
},
});
}, 300));
// Autocomplete
$('#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');
},
});
// Pagination
$(document).on('click', '.wpt-pagination a', function (e) {
e.preventDefault();
var href = $(this).attr('href');
var paged = href.match(/wpt_page=(\d+)/) ? parseInt(href.match(/wpt_page=(\d+)/)[1]) : 1;
var term = $('#wpt-search').val();
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);
} else {
$('#wpt-product-table tbody').html('محصولی یافت نشد. |
');
$('.wpt-pagination').empty();
}
},
error: function () {
hideLoader();
alert('خطایی در ارتباط رخ داد. لطفاً دوباره تلاش کنید.');
},
});
});
// Update Table
function updateTable(data, current_page, total_pages) {
var tbody = $('#wpt-product-table tbody');
tbody.empty();
$.each(data, function (index, product) {
var row = '' +
'' + product.title + ' | ' +
'' + (product.price ? wpt_ajax.wc_price_prefix + product.price + wpt_ajax.wc_price_suffix : '-') + ' | ' +
'' +
(product.stock_status === 'instock' ?
' ' +
'' +
'' +
'' +
' ' :
'ناموجود') +
' | ' +
'' +
(product.stock_status === 'instock' ?
'' :
'') +
' | ' +
'
';
tbody.append(row);
});
var pagination = $('.wpt-pagination');
pagination.empty();
if (total_pages > 1) {
var pagination_html = 'صفحه ' + current_page + ' از ' + total_pages + '';
pagination_html += '';
pagination.html(pagination_html);
}
}
// تابع کمکی برای افزودن پارامتر به URL بدون از دست دادن پارامترهای موجود
function addQueryArg(url, key, value) {
var newurl = new URL(url);
newurl.searchParams.set(key, value);
return newurl.toString();
}
// 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) {
alert('لطفاً تعداد را انتخاب کنید.');
return;
}
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(response.data.message);
$(document.body).trigger('wc_fragment_refresh');
window.location.href = response.data.cart_url;
} else {
alert(response.data.message || 'خطایی در افزودن به سبد خرید رخ داد.');
}
},
error: function (xhr, status, error) {
hideLoader();
console.log('AJAX Error:', xhr.responseText, status, error); // Log for debugging
alert('خطایی در ارتباط رخ داد. لطفاً دوباره تلاش کنید.');
},
});
});
// Notify me
$(document).on('click', '.wpt-notify-me', function () {
var button = $(this);
var product_id = button.data('product-id');
showLoader();
$.ajax({
url: wpt_ajax.ajax_url,
type: 'POST',
data: {
action: 'wpt_notify_me',
product_id: product_id,
nonce: wpt_ajax.nonce,
},
success: function (response) {
hideLoader();
if (response.success) {
alert(response.data.message);
} else {
alert(response.data.message || 'خطایی در ثبت درخواست رخ داد.');
}
},
error: function () {
hideLoader();
alert('خطایی در ارتباط رخ داد.');
},
});
});
});
#### فایل `wpt-style.css` (برای جذابیت صفحهبندی)
```css
.wpt-container {
max-width: 1200px;
margin: 0 auto;
padding: 20px;
}
.wpt-search {
width: 100%;
padding: 10px;
margin-bottom: 20px;
font-size: 16px;
border: 1px solid #ddd;
border-radius: 4px;
}
.wpt-table-wrapper {
overflow-x: auto;
}
.wpt-table {
width: 100%;
border-collapse: collapse;
background: #fff;
}
.wpt-table th,
.wpt-table td {
padding: 12px;
text-align: right;
border: 1px solid #ddd;
}
.wpt-table th {
background: #f4f4f4;
font-weight: bold;
}
.wpt-quantity-controls {
display: flex;
align-items: center;
gap: 5px;
}
.wpt-quantity-controls button {
background: #0073aa;
color: #fff;
border: none;
padding: 5px 10px;
cursor: pointer;
border-radius: 3px;
}
.wpt-quantity-controls button:hover {
background: #005177;
}
.wpt-quantity {
width: 60px;
text-align: center;
}
.wpt-add-to-cart {
background: #28a745;
color: #fff;
border: none;
padding: 8px 12px;
cursor: pointer;
border-radius: 3px;
transition: background 0.3s;
}
.wpt-add-to-cart:hover {
background: #218838;
}
.wpt-notify-me {
background: #ffc107;
color: #000;
border: none;
padding: 8px 12px;
cursor: pointer;
border-radius: 3px;
transition: background 0.3s;
}
.wpt-notify-me:hover {
background: #e0a800;
}
.wpt-pagination {
margin-top: 20px;
text-align: center;
}
.wpt-pagination-links a {
margin: 0 5px;
padding: 8px 12px;
background: #0073aa;
color: #fff;
text-decoration: none;
border-radius: 3px;
}
.wpt-pagination-links a:hover {
background: #005177;
}
.wpt-pagination-links a.active {
background: #005177;
cursor: default;
}
#wpt-loader {
text-align: center;
font-size: 16px;
color: #0073aa;
}
@media (max-width: 768px) {
.wpt-table-wrapper.responsive .wpt-table {
display: block;
}
.wpt-table thead {
display: none;
}
.wpt-table tbody {
display: block;
}
.wpt-table tr {
display: grid;
grid-template-columns: 1fr;
margin-bottom: 15px;
border: 1px solid #ddd;
padding: 10px;
background: #fff;
box-shadow: 0 2px 5px rgba(0,0,0,0.1); /* جذابیت کارت */
border-radius: 8px;
}
.wpt-table td {
display: block;
text-align: right;
border: none;
padding: 5px 0;
font-size: 14px;
}
.wpt-table td:before {
content: attr(data-label);
font-weight: bold;
margin-right: 10px;
color: #333;
}
.wpt-quantity-controls {
justify-content: flex-end;
margin-top: 10px;
}
.wpt-add-to-cart, .wpt-notify-me {
width: 100%;
margin-top: 10px;
}
.wpt-pagination {
margin-top: 20px;
text-align: center;
}
.wpt-pagination-links a {
padding: 8px 12px;
margin: 0 5px;
}
}
فروشگاه - نیک پلاس
نمایش 1–12 از 83 نتیجهSorted by latest
نمایش فیلترها