139 lines
5.3 KiB
PHP
139 lines
5.3 KiB
PHP
<?php
|
|
if (!defined('ABSPATH')) {
|
|
exit;
|
|
}
|
|
|
|
if (!class_exists('WP_List_Table')) {
|
|
require_once ABSPATH . 'wp-admin/includes/class-wp-list-table.php';
|
|
}
|
|
|
|
class Sodino_Upsell_List_Table extends WP_List_Table {
|
|
private $repository;
|
|
private $items_per_page = 20;
|
|
|
|
public function __construct($repository) {
|
|
parent::__construct([
|
|
'singular' => 'sodino_upsell',
|
|
'plural' => 'sodino_upsells',
|
|
'ajax' => false,
|
|
]);
|
|
|
|
$this->repository = $repository;
|
|
}
|
|
|
|
public function get_columns() {
|
|
return [
|
|
'cb' => '<input type="checkbox" />',
|
|
'title' => __('عنوان', 'sodino'),
|
|
'trigger' => __('شرط فعالسازی', 'sodino'),
|
|
'suggested_product'=> __('محصول پیشنهادی', 'sodino'),
|
|
'discount' => __('تخفیف', 'sodino'),
|
|
'status' => __('وضعیت', 'sodino'),
|
|
'actions' => __('عملیات', 'sodino'),
|
|
];
|
|
}
|
|
|
|
protected function get_sortable_columns() {
|
|
return [
|
|
'title' => ['title', true],
|
|
'priority' => ['priority', true],
|
|
];
|
|
}
|
|
|
|
protected function column_cb($item) {
|
|
return sprintf('<input type="checkbox" name="upsell_ids[]" value="%d" />', $item->id);
|
|
}
|
|
|
|
public function get_bulk_actions() {
|
|
return [
|
|
'delete' => __('حذف گروهی', 'sodino'),
|
|
];
|
|
}
|
|
|
|
public function column_title($item) {
|
|
$edit_url = admin_url('admin.php?page=sodino-add-upsell&action=edit&id=' . $item->id);
|
|
$title = sprintf('<strong><a href="%s">%s</a></strong>', esc_url($edit_url), esc_html($item->title));
|
|
return $title;
|
|
}
|
|
|
|
public function column_trigger($item) {
|
|
switch ($item->trigger_type) {
|
|
case 'product':
|
|
$product = wc_get_product(intval($item->trigger_value));
|
|
return $product ? esc_html($product->get_name()) : __('محصول خاص', 'sodino');
|
|
case 'category':
|
|
$term = get_term(intval($item->trigger_value));
|
|
return $term && !is_wp_error($term) ? esc_html($term->name) : __('دستهبندی', 'sodino');
|
|
case 'cart_total':
|
|
return sprintf('%s %s', esc_html(number_format_i18n(floatval($item->trigger_value))), __('تومان', 'sodino'));
|
|
default:
|
|
return __('نامشخص', 'sodino');
|
|
}
|
|
}
|
|
|
|
public function column_suggested_product($item) {
|
|
$product = wc_get_product($item->target_product_id);
|
|
return $product ? esc_html($product->get_name()) : __('نامشخص', 'sodino');
|
|
}
|
|
|
|
public function column_discount($item) {
|
|
if ($item->discount_type === 'fixed') {
|
|
return sprintf('%s %s', esc_html(number_format_i18n($item->discount_value)), __('تومان', 'sodino'));
|
|
}
|
|
if ($item->discount_type === 'percentage') {
|
|
return sprintf('%s %%', esc_html($item->discount_value));
|
|
}
|
|
return __('بدون تخفیف', 'sodino');
|
|
}
|
|
|
|
public function column_status($item) {
|
|
return $item->status ? __('فعال', 'sodino') : __('غیرفعال', 'sodino');
|
|
}
|
|
|
|
public function column_actions($item) {
|
|
$edit_url = admin_url('admin.php?page=sodino-add-upsell&action=edit&id=' . $item->id);
|
|
$toggle_url = wp_nonce_url(admin_url('admin.php?page=sodino-upsells&action=toggle_upsell_status&id=' . $item->id), 'toggle_upsell_status');
|
|
$delete_url = wp_nonce_url(admin_url('admin.php?page=sodino-upsells&action=delete_upsell&id=' . $item->id), 'delete_upsell');
|
|
|
|
$toggle_label = $item->status ? __('غیرفعال کردن', 'sodino') : __('فعال کردن', 'sodino');
|
|
|
|
return sprintf(
|
|
'<a href="%s">%s</a> | <a href="%s">%s</a> | <a href="%s" onclick="return confirm(\'%s\');">%s</a>',
|
|
esc_url($edit_url),
|
|
esc_html__('ویرایش', 'sodino'),
|
|
esc_url($toggle_url),
|
|
esc_html($toggle_label),
|
|
esc_url($delete_url),
|
|
esc_js(__('آیا از حذف این پیشنهاد آپسل مطمئن هستید؟', 'sodino')),
|
|
esc_html__('حذف', 'sodino')
|
|
);
|
|
}
|
|
|
|
public function prepare_items() {
|
|
$this->_column_headers = [$this->get_columns(), [], $this->get_sortable_columns()];
|
|
$this->process_bulk_action();
|
|
|
|
$all_items = $this->repository->getAll();
|
|
$current_page = $this->get_pagenum();
|
|
$total_items = count($all_items);
|
|
$this->items = array_slice($all_items, ($current_page - 1) * $this->items_per_page, $this->items_per_page);
|
|
|
|
$this->set_pagination_args([
|
|
'total_items' => $total_items,
|
|
'per_page' => $this->items_per_page,
|
|
'total_pages' => ceil($total_items / $this->items_per_page),
|
|
]);
|
|
}
|
|
|
|
public function process_bulk_action() {
|
|
if ('delete' === $this->current_action()) {
|
|
$upsell_ids = isset($_POST['upsell_ids']) ? array_map('intval', $_POST['upsell_ids']) : [];
|
|
if (!empty($upsell_ids) && check_admin_referer('bulk-' . $this->_args['plural'])) {
|
|
foreach ($upsell_ids as $id) {
|
|
$this->repository->delete($id);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|