/*
Plugin Name: Pangol Wallet System
Description: سیستم کیف پول اختصاصی برای سایت پنگول پت با قابلیت شارژ، مدیریت، مشاهده تراکنش و رابط کاربری برای کاربران
Version: 1.1
Author: ChatGPT
*/
if ( ! defined( 'ABSPATH' ) ) exit;
// ایجاد جدول کیف پول کاربران و تراکنشها در هنگام فعالسازی افزونه
register_activation_hook(__FILE__, 'pangol_wallet_create_tables');
function pangol_wallet_create_tables() {
global $wpdb;
$charset_collate = $wpdb->get_charset_collate();
$wallet_table = $wpdb->prefix . 'pangol_wallet';
$transaction_table = $wpdb->prefix . 'pangol_wallet_transactions';
$sql1 = "CREATE TABLE $wallet_table (
id BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT,
user_id BIGINT(20) UNSIGNED NOT NULL,
balance DECIMAL(10,2) DEFAULT 0,
status VARCHAR(20) DEFAULT 'active',
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (id),
UNIQUE KEY user_id (user_id)
) $charset_collate;";
$sql2 = "CREATE TABLE $transaction_table (
id BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT,
user_id BIGINT(20) UNSIGNED NOT NULL,
amount DECIMAL(10,2) NOT NULL,
type VARCHAR(20),
note TEXT,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (id)
) $charset_collate;";
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
dbDelta($sql1);
dbDelta($sql2);
}
// ایجاد کیف پول هنگام ثبت نام
add_action('user_register', 'pangol_wallet_create_user_wallet');
function pangol_wallet_create_user_wallet($user_id) {
global $wpdb;
$table = $wpdb->prefix . 'pangol_wallet';
$wpdb->insert($table, ['user_id' => $user_id]);
}
// نمایش کیف پول در حساب کاربری ووکامرس
add_action('woocommerce_account_wallet_endpoint', 'pangol_wallet_account_page');
function pangol_wallet_account_page() {
$user_id = get_current_user_id();
if (!$user_id) return;
global $wpdb;
$wallet = $wpdb->get_row($wpdb->prepare("SELECT * FROM {$wpdb->prefix}pangol_wallet WHERE user_id = %d", $user_id));
$transactions = $wpdb->get_results($wpdb->prepare("SELECT * FROM {$wpdb->prefix}pangol_wallet_transactions WHERE user_id = %d ORDER BY created_at DESC", $user_id));
echo '
موجودی کیف پول: ' . esc_html($wallet->balance) . ' تومان
';
echo '';
if (isset($_POST['pangol_charge']) && is_numeric($_POST['pangol_amount'])) {
$amount = floatval($_POST['pangol_amount']);
$wpdb->query($wpdb->prepare("UPDATE {$wpdb->prefix}pangol_wallet SET balance = balance + %f WHERE user_id = %d", $amount, $user_id));
$wpdb->insert($wpdb->prefix . 'pangol_wallet_transactions', [
'user_id' => $user_id,
'amount' => $amount,
'type' => 'credit',
'note' => 'شارژ دستی توسط کاربر'
]);
echo 'کیف پول با موفقیت شارژ شد.
';
}
echo 'تراکنشها
مبلغ | نوع | یادداشت | تاریخ |
';
foreach ($transactions as $tr) {
echo '' . esc_html($tr->amount) . ' | ' . esc_html($tr->type) . ' | ' . esc_html($tr->note) . ' | ' . esc_html($tr->created_at) . ' |
';
}
echo '
';
}
// ثبت مسیر کیف پول در حساب کاربری ووکامرس
add_filter('woocommerce_account_menu_items', 'pangol_wallet_account_link');
function pangol_wallet_account_link($items) {
$items['wallet'] = 'کیف پول';
return $items;
}
add_action('init', function() {
add_rewrite_endpoint('wallet', EP_ROOT | EP_PAGES);
});
مقایسه - پت شاپ پنگول پت