Files
wordpress-theme-aksrt/inc/helpers/aksrt-transactions.php
T

119 lines
3.4 KiB
PHP

<?php
/**
* AKSRT Transaction Helpers
*
* Frontend transaction view-model helpers.
*
* @package AKSRT
*/
defined('ABSPATH') || exit;
function aksrt_get_order_status_labels(): array {
if (class_exists('AKSRT_Domain_Model')) {
return AKSRT_Domain_Model::order_status_labels();
}
return [];
}
function aksrt_get_product_page_view(int $product_id): array {
if ($product_id <= 0 || !class_exists('AKSRT_Shop')) {
return [];
}
return AKSRT_Shop::get_instance()->get_product_detail_view($product_id);
}
function aksrt_get_product_edit_page_view(int $user_id, int $product_id = 0): array {
if ($user_id <= 0 || !class_exists('AKSRT_Shop')) {
return [
'allowed' => false,
'is_edit' => false,
'product' => [],
];
}
return AKSRT_Shop::get_instance()->get_product_edit_view($user_id, $product_id);
}
function aksrt_get_cart_page_view(int $user_id): array {
if ($user_id <= 0 || !class_exists('AKSRT_Shop')) {
return [
'payment_methods' => [],
'shipping_methods' => [],
'address_book' => [],
'coupon_enabled' => false,
'currency_symbol' => '¥',
'pending_orders' => [],
];
}
return AKSRT_Shop::get_instance()->get_cart_page_view($user_id);
}
function aksrt_get_orders_page_view(int $user_id, int $paged = 1, int $per_page = 20, string $status = ''): array {
if ($user_id <= 0 || !class_exists('AKSRT_Shop')) {
return [
'items' => [],
'total' => 0,
'total_pages' => 1,
'page' => 1,
];
}
return AKSRT_Shop::get_instance()->get_order_center_view($user_id, [
'page' => $paged,
'per_page' => $per_page,
'status' => $status,
]);
}
function aksrt_get_revenue_page_view(int $user_id): array {
$balance = 0.0;
$total_earned = 0.0;
$total_withdrawn = 0.0;
$records = [];
if (class_exists('AKSRT_Credits')) {
$balance = (float) AKSRT_Credits::balance($user_id);
}
if (class_exists('AKSRT_Revenue')) {
$revenues = AKSRT_Revenue::get_instance()->get_user_revenues($user_id, ['limit' => 200]);
foreach ($revenues as $rev) {
$total_earned += (float) ($rev->amount ?? 0);
$records[] = [
'type' => 'earning',
'title' => (string) ($rev->description ?? __('创作分成', 'aksrt')),
'amount' => (float) ($rev->amount ?? 0),
'created_at' => (string) ($rev->created_at ?? ''),
];
}
}
if (class_exists('AKSRT_Withdrawal')) {
$withdrawals = AKSRT_Withdrawal::get_instance()->get_user_withdrawals($user_id, ['limit' => 200]);
foreach ($withdrawals as $w) {
$total_withdrawn += (float) ($w->amount ?? 0);
$records[] = [
'type' => 'withdrawal',
'title' => __('提现', 'aksrt'),
'amount' => (float) ($w->amount ?? 0),
'created_at' => (string) ($w->created_at ?? ''),
];
}
}
usort($records, static function ($a, $b) {
return strcmp($b['created_at'] ?? '', $a['created_at'] ?? '');
});
return [
'balance' => $balance,
'total_earned' => $total_earned,
'total_withdrawn' => $total_withdrawn,
'records' => $records,
];
}