subscription/templates/frontend/subscription-details.php

355 lines
15 KiB
PHP

<?php
/**
* 订阅详情前端页面模板
*
* @package Yoone_Subscriptions
* @version 1.0.0
*/
if (!defined('ABSPATH')) {
exit; // 防止直接访问
}
$user_id = get_current_user_id();
if (!$user_id) {
wc_print_notice(__('请先登录查看订阅详情', 'yoone-subscriptions'), 'error');
return;
}
// 获取订阅ID
$subscription_id = get_query_var('yoone-subscription-id');
if (!$subscription_id) {
$subscription_id = isset($_GET['subscription_id']) ? intval($_GET['subscription_id']) : 0;
}
if (!$subscription_id) {
wc_print_notice(__('无效的订阅ID', 'yoone-subscriptions'), 'error');
return;
}
global $wpdb;
// 获取订阅详情
$subscription = $wpdb->get_row($wpdb->prepare(
"SELECT s.*, p.post_title as product_name
FROM {$wpdb->prefix}yoone_subscriptions s
LEFT JOIN {$wpdb->posts} p ON s.product_id = p.ID
WHERE s.id = %d AND s.user_id = %d",
$subscription_id,
$user_id
));
if (!$subscription) {
wc_print_notice(__('订阅不存在或您没有权限查看', 'yoone-subscriptions'), 'error');
return;
}
// 获取相关订单
$orders = $wpdb->get_results($wpdb->prepare(
"SELECT p.ID, p.post_date, pm.meta_value as order_total, pm2.meta_value as order_status
FROM {$wpdb->posts} p
LEFT JOIN {$wpdb->postmeta} pm ON p.ID = pm.post_id AND pm.meta_key = '_order_total'
LEFT JOIN {$wpdb->postmeta} pm2 ON p.ID = pm2.post_id AND pm2.meta_key = '_order_status'
LEFT JOIN {$wpdb->postmeta} pm3 ON p.ID = pm3.post_id AND pm3.meta_key = '_yoone_subscription_id'
WHERE p.post_type = 'shop_order' AND pm3.meta_value = %d
ORDER BY p.post_date DESC",
$subscription->id
));
?>
<div class="yoone-subscription-details">
<h2><?php printf(__('订阅 #%d', 'yoone-subscriptions'), $subscription->id); ?></h2>
<p>
<a href="<?php echo esc_url(wc_get_account_endpoint_url('yoone-subscriptions')); ?>" class="woocommerce-button button">
&larr; <?php _e('返回我的订阅', 'yoone-subscriptions'); ?>
</a>
</p>
<!-- 订阅基本信息 -->
<div class="subscription-overview">
<h3><?php _e('订阅概览', 'yoone-subscriptions'); ?></h3>
<table class="woocommerce-table woocommerce-table--order-details shop_table order_details">
<tbody>
<tr>
<th><?php _e('订阅状态', 'yoone-subscriptions'); ?></th>
<td>
<span class="subscription-status status-<?php echo esc_attr($subscription->status); ?>">
<?php echo Yoone_Frontend::get_subscription_status_label($subscription->status); ?>
</span>
</td>
</tr>
<tr>
<th><?php _e('产品', 'yoone-subscriptions'); ?></th>
<td>
<?php if ($subscription->product_id): ?>
<a href="<?php echo get_permalink($subscription->product_id); ?>">
<?php echo esc_html($subscription->product_name ?: __('未知产品', 'yoone-subscriptions')); ?>
</a>
<?php else: ?>
<em><?php _e('产品已删除', 'yoone-subscriptions'); ?></em>
<?php endif; ?>
</td>
</tr>
<tr>
<th><?php _e('订阅价格', 'yoone-subscriptions'); ?></th>
<td>
<span class="woocommerce-Price-amount amount">
<?php echo Yoone_Helper::format_price($subscription->subscription_price); ?>
</span>
<small>
/ <?php echo Yoone_Helper::format_billing_period($subscription->billing_period, $subscription->billing_interval); ?>
</small>
</td>
</tr>
<tr>
<th><?php _e('开始日期', 'yoone-subscriptions'); ?></th>
<td>
<time datetime="<?php echo esc_attr($subscription->start_date); ?>">
<?php echo date_i18n(wc_date_format() . ' ' . wc_time_format(), strtotime($subscription->start_date)); ?>
</time>
</td>
</tr>
<?php if ($subscription->trial_end_date): ?>
<tr>
<th><?php _e('试用期结束', 'yoone-subscriptions'); ?></th>
<td>
<time datetime="<?php echo esc_attr($subscription->trial_end_date); ?>">
<?php echo date_i18n(wc_date_format(), strtotime($subscription->trial_end_date)); ?>
</time>
</td>
</tr>
<?php endif; ?>
<?php if ($subscription->next_payment_date && $subscription->status === 'active'): ?>
<tr>
<th><?php _e('下次付款日期', 'yoone-subscriptions'); ?></th>
<td>
<time datetime="<?php echo esc_attr($subscription->next_payment_date); ?>">
<?php echo date_i18n(wc_date_format(), strtotime($subscription->next_payment_date)); ?>
</time>
</td>
</tr>
<?php endif; ?>
<?php if ($subscription->end_date): ?>
<tr>
<th><?php _e('结束日期', 'yoone-subscriptions'); ?></th>
<td>
<time datetime="<?php echo esc_attr($subscription->end_date); ?>">
<?php echo date_i18n(wc_date_format() . ' ' . wc_time_format(), strtotime($subscription->end_date)); ?>
</time>
</td>
</tr>
<?php endif; ?>
</tbody>
</table>
</div>
<!-- 订阅操作 -->
<div class="subscription-actions">
<h3><?php _e('订阅操作', 'yoone-subscriptions'); ?></h3>
<div class="subscription-action-buttons">
<?php if ($subscription->status === 'active'): ?>
<a href="<?php echo esc_url(wp_nonce_url(add_query_arg(array('action' => 'pause', 'subscription_id' => $subscription->id)), 'pause_subscription_' . $subscription->id)); ?>"
class="woocommerce-button button pause"
onclick="return confirm('<?php _e('确定要暂停这个订阅吗?', 'yoone-subscriptions'); ?>')">
<?php _e('暂停订阅', 'yoone-subscriptions'); ?>
</a>
<?php elseif ($subscription->status === 'paused'): ?>
<a href="<?php echo esc_url(wp_nonce_url(add_query_arg(array('action' => 'resume', 'subscription_id' => $subscription->id)), 'resume_subscription_' . $subscription->id)); ?>"
class="woocommerce-button button resume">
<?php _e('恢复订阅', 'yoone-subscriptions'); ?>
</a>
<?php endif; ?>
<?php if (in_array($subscription->status, array('active', 'paused'))): ?>
<a href="<?php echo esc_url(wp_nonce_url(add_query_arg(array('action' => 'cancel', 'subscription_id' => $subscription->id)), 'cancel_subscription_' . $subscription->id)); ?>"
class="woocommerce-button button cancel"
onclick="return confirm('<?php _e('确定要取消这个订阅吗?此操作不可撤销。', 'yoone-subscriptions'); ?>')">
<?php _e('取消订阅', 'yoone-subscriptions'); ?>
</a>
<?php endif; ?>
</div>
</div>
<!-- 相关订单 -->
<div class="subscription-orders">
<h3><?php _e('相关订单', 'yoone-subscriptions'); ?></h3>
<?php if (empty($orders)): ?>
<p><?php _e('暂无相关订单', 'yoone-subscriptions'); ?></p>
<?php else: ?>
<table class="woocommerce-orders-table woocommerce-MyAccount-orders shop_table shop_table_responsive my_account_orders account-orders-table">
<thead>
<tr>
<th class="woocommerce-orders-table__header woocommerce-orders-table__header-order-number">
<span class="nobr"><?php _e('订单', 'yoone-subscriptions'); ?></span>
</th>
<th class="woocommerce-orders-table__header woocommerce-orders-table__header-order-date">
<span class="nobr"><?php _e('日期', 'yoone-subscriptions'); ?></span>
</th>
<th class="woocommerce-orders-table__header woocommerce-orders-table__header-order-status">
<span class="nobr"><?php _e('状态', 'yoone-subscriptions'); ?></span>
</th>
<th class="woocommerce-orders-table__header woocommerce-orders-table__header-order-total">
<span class="nobr"><?php _e('总计', 'yoone-subscriptions'); ?></span>
</th>
<th class="woocommerce-orders-table__header woocommerce-orders-table__header-order-actions">
<span class="nobr"><?php _e('操作', 'yoone-subscriptions'); ?></span>
</th>
</tr>
</thead>
<tbody>
<?php foreach ($orders as $order): ?>
<tr class="woocommerce-orders-table__row order">
<td class="woocommerce-orders-table__cell woocommerce-orders-table__cell-order-number" data-title="<?php _e('订单', 'yoone-subscriptions'); ?>">
<a href="<?php echo esc_url(wc_get_account_endpoint_url('view-order', $order->ID)); ?>">
#<?php echo $order->ID; ?>
</a>
</td>
<td class="woocommerce-orders-table__cell woocommerce-orders-table__cell-order-date" data-title="<?php _e('日期', 'yoone-subscriptions'); ?>">
<time datetime="<?php echo esc_attr($order->post_date); ?>">
<?php echo date_i18n(wc_date_format(), strtotime($order->post_date)); ?>
</time>
</td>
<td class="woocommerce-orders-table__cell woocommerce-orders-table__cell-order-status" data-title="<?php _e('状态', 'yoone-subscriptions'); ?>">
<?php echo esc_html(wc_get_order_status_name($order->order_status)); ?>
</td>
<td class="woocommerce-orders-table__cell woocommerce-orders-table__cell-order-total" data-title="<?php _e('总计', 'yoone-subscriptions'); ?>">
<?php echo Yoone_Helper::format_price($order->order_total); ?>
</td>
<td class="woocommerce-orders-table__cell woocommerce-orders-table__cell-order-actions" data-title="<?php _e('操作', 'yoone-subscriptions'); ?>">
<a href="<?php echo esc_url(wc_get_account_endpoint_url('view-order', $order->ID)); ?>" class="woocommerce-button button view">
<?php _e('查看', 'yoone-subscriptions'); ?>
</a>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<?php endif; ?>
</div>
</div>
<style>
.yoone-subscription-details .subscription-status {
padding: 3px 8px;
border-radius: 3px;
font-size: 11px;
font-weight: bold;
text-transform: uppercase;
color: white;
}
.yoone-subscription-details .status-active { background: #46b450; }
.yoone-subscription-details .status-paused { background: #ffb900; }
.yoone-subscription-details .status-cancelled { background: #dc3232; }
.yoone-subscription-details .status-expired { background: #666; }
.yoone-subscription-details .subscription-overview,
.yoone-subscription-details .subscription-actions,
.yoone-subscription-details .subscription-orders {
margin-bottom: 30px;
}
.yoone-subscription-details .subscription-action-buttons {
margin-top: 15px;
}
.yoone-subscription-details .subscription-action-buttons .woocommerce-button {
margin-right: 10px;
margin-bottom: 10px;
}
.yoone-subscription-details .woocommerce-button.pause {
background-color: #ffb900;
border-color: #ffb900;
}
.yoone-subscription-details .woocommerce-button.resume {
background-color: #46b450;
border-color: #46b450;
}
.yoone-subscription-details .woocommerce-button.cancel {
background-color: #dc3232;
border-color: #dc3232;
}
@media (max-width: 768px) {
.yoone-subscription-details .woocommerce-orders-table__cell {
display: block;
text-align: right;
border: none;
border-bottom: 1px solid #e1e1e1;
padding: 0.5em 0;
}
.yoone-subscription-details .woocommerce-orders-table__cell:before {
content: attr(data-title) ": ";
font-weight: bold;
float: left;
}
.yoone-subscription-details .woocommerce-orders-table__cell-order-actions {
text-align: left;
}
.yoone-subscription-details .woocommerce-orders-table__cell-order-actions:before {
display: none;
}
}
</style>
<?php
// 处理订阅操作
if (isset($_GET['action']) && isset($_GET['subscription_id'])) {
$action = sanitize_text_field($_GET['action']);
$action_subscription_id = intval($_GET['subscription_id']);
// 验证订阅ID匹配
if ($action_subscription_id === $subscription->id) {
switch ($action) {
case 'pause':
if (wp_verify_nonce($_GET['_wpnonce'], 'pause_subscription_' . $subscription->id)) {
$wpdb->update(
$wpdb->prefix . 'yoone_subscriptions',
array('status' => 'paused', 'updated_at' => current_time('mysql')),
array('id' => $subscription->id)
);
wc_add_notice(__('订阅已暂停', 'yoone-subscriptions'), 'success');
}
break;
case 'resume':
if (wp_verify_nonce($_GET['_wpnonce'], 'resume_subscription_' . $subscription->id)) {
$wpdb->update(
$wpdb->prefix . 'yoone_subscriptions',
array('status' => 'active', 'updated_at' => current_time('mysql')),
array('id' => $subscription->id)
);
wc_add_notice(__('订阅已恢复', 'yoone-subscriptions'), 'success');
}
break;
case 'cancel':
if (wp_verify_nonce($_GET['_wpnonce'], 'cancel_subscription_' . $subscription->id)) {
$wpdb->update(
$wpdb->prefix . 'yoone_subscriptions',
array('status' => 'cancelled', 'end_date' => current_time('mysql'), 'updated_at' => current_time('mysql')),
array('id' => $subscription->id)
);
wc_add_notice(__('订阅已取消', 'yoone-subscriptions'), 'success');
}
break;
}
// 重定向以避免重复提交
wp_redirect(wc_get_account_endpoint_url('yoone-subscriptions') . '/' . $subscription->id);
exit;
}
}
?>