514 lines
17 KiB
PHP
514 lines
17 KiB
PHP
<?php
|
||
/**
|
||
* 订阅选项前端模板
|
||
*
|
||
* @package Yoone_Subscriptions
|
||
* @version 1.0.0
|
||
*/
|
||
|
||
if (!defined('ABSPATH')) {
|
||
exit; // 防止直接访问
|
||
}
|
||
|
||
global $product;
|
||
|
||
// 检查产品是否支持订阅
|
||
$is_subscription = $product->get_meta('_yoone_subscription_enabled');
|
||
if (!$is_subscription) {
|
||
return;
|
||
}
|
||
|
||
// 获取订阅设置
|
||
$subscription_price = $product->get_meta('_yoone_subscription_price');
|
||
$billing_period = $product->get_meta('_yoone_billing_period') ?: 'month';
|
||
$billing_interval = $product->get_meta('_yoone_billing_interval') ?: 1;
|
||
$trial_period = $product->get_meta('_yoone_trial_period');
|
||
$trial_length = $product->get_meta('_yoone_trial_length');
|
||
$subscription_length = $product->get_meta('_yoone_subscription_length');
|
||
$signup_fee = $product->get_meta('_yoone_signup_fee');
|
||
|
||
// 计算价格
|
||
$regular_price = $product->get_regular_price();
|
||
$subscription_price = $subscription_price ?: $regular_price;
|
||
$trial_price = 0; // 试用期免费
|
||
|
||
// 计算节省金额(如果订阅价格更低)
|
||
$monthly_regular = $regular_price;
|
||
$monthly_subscription = $subscription_price;
|
||
|
||
// 转换为月度价格进行比较
|
||
switch ($billing_period) {
|
||
case 'week':
|
||
$monthly_subscription = $subscription_price * 4.33; // 平均每月周数
|
||
break;
|
||
case 'year':
|
||
$monthly_subscription = $subscription_price / 12;
|
||
break;
|
||
case 'day':
|
||
$monthly_subscription = $subscription_price * 30;
|
||
break;
|
||
}
|
||
|
||
$monthly_savings = max(0, $monthly_regular - $monthly_subscription);
|
||
$savings_percentage = $monthly_regular > 0 ? ($monthly_savings / $monthly_regular) * 100 : 0;
|
||
|
||
?>
|
||
|
||
<div class="yoone-subscription-options" data-product-id="<?php echo esc_attr($product->get_id()); ?>">
|
||
|
||
<!-- 购买方式选择 -->
|
||
<div class="purchase-options">
|
||
<h4><?php _e('购买方式', 'yoone-subscriptions'); ?></h4>
|
||
|
||
<div class="purchase-option-list">
|
||
<!-- 一次性购买 -->
|
||
<div class="purchase-option">
|
||
<label class="purchase-option-label">
|
||
<input type="radio" name="yoone_purchase_type" value="one_time" checked>
|
||
<span class="option-content">
|
||
<span class="option-title"><?php _e('一次性购买', 'yoone-subscriptions'); ?></span>
|
||
<span class="option-price"><?php echo Yoone_Helper::format_price($regular_price); ?></span>
|
||
<span class="option-description"><?php _e('立即付款,拥有产品', 'yoone-subscriptions'); ?></span>
|
||
</span>
|
||
</label>
|
||
</div>
|
||
|
||
<!-- 订阅购买 -->
|
||
<div class="purchase-option subscription-option">
|
||
<label class="purchase-option-label">
|
||
<input type="radio" name="yoone_purchase_type" value="subscription">
|
||
<span class="option-content">
|
||
<span class="option-title">
|
||
<?php _e('订阅购买', 'yoone-subscriptions'); ?>
|
||
<?php if ($savings_percentage > 5): ?>
|
||
<span class="savings-badge"><?php printf(__('省 %.0f%%', 'yoone-subscriptions'), $savings_percentage); ?></span>
|
||
<?php endif; ?>
|
||
</span>
|
||
<span class="option-price">
|
||
<?php echo Yoone_Helper::format_price($subscription_price); ?>
|
||
<small>/ <?php echo Yoone_Helper::format_billing_period($billing_period, $billing_interval); ?></small>
|
||
</span>
|
||
<span class="option-description">
|
||
<?php
|
||
if ($trial_period && $trial_length > 0) {
|
||
printf(__('免费试用 %d %s,然后每%s收费', 'yoone-subscriptions'),
|
||
$trial_length,
|
||
Yoone_Helper::get_period_label($trial_period),
|
||
Yoone_Helper::format_billing_period($billing_period, $billing_interval)
|
||
);
|
||
} else {
|
||
printf(__('每%s自动续费', 'yoone-subscriptions'),
|
||
Yoone_Helper::format_billing_period($billing_period, $billing_interval)
|
||
);
|
||
}
|
||
?>
|
||
</span>
|
||
</span>
|
||
</label>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
|
||
<!-- 订阅详情(仅在选择订阅时显示) -->
|
||
<div class="subscription-details" style="display: none;">
|
||
|
||
<!-- 价格明细 -->
|
||
<div class="subscription-pricing">
|
||
<h5><?php _e('价格明细', 'yoone-subscriptions'); ?></h5>
|
||
|
||
<div class="pricing-breakdown">
|
||
<?php if ($signup_fee > 0): ?>
|
||
<div class="pricing-item">
|
||
<span class="label"><?php _e('注册费用:', 'yoone-subscriptions'); ?></span>
|
||
<span class="value"><?php echo Yoone_Helper::format_price($signup_fee); ?></span>
|
||
</div>
|
||
<?php endif; ?>
|
||
|
||
<?php if ($trial_period && $trial_length > 0): ?>
|
||
<div class="pricing-item trial">
|
||
<span class="label">
|
||
<?php printf(__('试用期 (%d %s):', 'yoone-subscriptions'),
|
||
$trial_length,
|
||
Yoone_Helper::get_period_label($trial_period)
|
||
); ?>
|
||
</span>
|
||
<span class="value free"><?php _e('免费', 'yoone-subscriptions'); ?></span>
|
||
</div>
|
||
<?php endif; ?>
|
||
|
||
<div class="pricing-item recurring">
|
||
<span class="label">
|
||
<?php printf(__('续费价格 (每%s):', 'yoone-subscriptions'),
|
||
Yoone_Helper::format_billing_period($billing_period, $billing_interval)
|
||
); ?>
|
||
</span>
|
||
<span class="value"><?php echo Yoone_Helper::format_price($subscription_price); ?></span>
|
||
</div>
|
||
|
||
<?php if ($subscription_length > 0): ?>
|
||
<div class="pricing-item">
|
||
<span class="label"><?php _e('订阅期限:', 'yoone-subscriptions'); ?></span>
|
||
<span class="value">
|
||
<?php printf(__('%d 次付款', 'yoone-subscriptions'), $subscription_length); ?>
|
||
</span>
|
||
</div>
|
||
<?php endif; ?>
|
||
</div>
|
||
</div>
|
||
|
||
<!-- 订阅优势 -->
|
||
<?php if ($monthly_savings > 0): ?>
|
||
<div class="subscription-benefits">
|
||
<h5><?php _e('订阅优势', 'yoone-subscriptions'); ?></h5>
|
||
<ul class="benefits-list">
|
||
<li>
|
||
<span class="benefit-icon">💰</span>
|
||
<?php printf(__('每月节省 %s (%.0f%%)', 'yoone-subscriptions'),
|
||
Yoone_Helper::format_price($monthly_savings),
|
||
$savings_percentage
|
||
); ?>
|
||
</li>
|
||
<li>
|
||
<span class="benefit-icon">🔄</span>
|
||
<?php _e('自动续费,无需手动购买', 'yoone-subscriptions'); ?>
|
||
</li>
|
||
<li>
|
||
<span class="benefit-icon">⏸️</span>
|
||
<?php _e('随时暂停或取消订阅', 'yoone-subscriptions'); ?>
|
||
</li>
|
||
<?php if ($trial_period && $trial_length > 0): ?>
|
||
<li>
|
||
<span class="benefit-icon">🎁</span>
|
||
<?php printf(__('免费试用 %d %s', 'yoone-subscriptions'),
|
||
$trial_length,
|
||
Yoone_Helper::get_period_label($trial_period)
|
||
); ?>
|
||
</li>
|
||
<?php endif; ?>
|
||
</ul>
|
||
</div>
|
||
<?php endif; ?>
|
||
|
||
<!-- 订阅条款 -->
|
||
<div class="subscription-terms">
|
||
<h5><?php _e('订阅条款', 'yoone-subscriptions'); ?></h5>
|
||
<div class="terms-content">
|
||
<p>
|
||
<?php
|
||
if ($trial_period && $trial_length > 0) {
|
||
printf(__('您将获得 %d %s 的免费试用期。试用期结束后,将按 %s 的价格每%s自动收费。', 'yoone-subscriptions'),
|
||
$trial_length,
|
||
Yoone_Helper::get_period_label($trial_period),
|
||
Yoone_Helper::format_price($subscription_price),
|
||
Yoone_Helper::format_billing_period($billing_period, $billing_interval)
|
||
);
|
||
} else {
|
||
printf(__('您将按 %s 的价格每%s自动收费。', 'yoone-subscriptions'),
|
||
Yoone_Helper::format_price($subscription_price),
|
||
Yoone_Helper::format_billing_period($billing_period, $billing_interval)
|
||
);
|
||
}
|
||
?>
|
||
</p>
|
||
|
||
<p><?php _e('您可以随时在账户页面中管理您的订阅,包括暂停、恢复或取消订阅。', 'yoone-subscriptions'); ?></p>
|
||
|
||
<?php if ($subscription_length > 0): ?>
|
||
<p>
|
||
<?php printf(__('此订阅将在 %d 次付款后自动结束。', 'yoone-subscriptions'), $subscription_length); ?>
|
||
</p>
|
||
<?php endif; ?>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
|
||
<!-- 隐藏字段 -->
|
||
<input type="hidden" name="yoone_subscription_data" value="">
|
||
|
||
</div>
|
||
|
||
<style>
|
||
.yoone-subscription-options {
|
||
margin: 20px 0;
|
||
padding: 20px;
|
||
border: 1px solid #e1e1e1;
|
||
border-radius: 5px;
|
||
background: #f9f9f9;
|
||
}
|
||
|
||
.yoone-subscription-options h4 {
|
||
margin: 0 0 15px 0;
|
||
color: #333;
|
||
border-bottom: 2px solid #46b450;
|
||
padding-bottom: 5px;
|
||
}
|
||
|
||
.yoone-subscription-options .purchase-option-list {
|
||
display: grid;
|
||
gap: 15px;
|
||
}
|
||
|
||
.yoone-subscription-options .purchase-option {
|
||
background: white;
|
||
border: 2px solid #e1e1e1;
|
||
border-radius: 5px;
|
||
transition: all 0.3s ease;
|
||
}
|
||
|
||
.yoone-subscription-options .purchase-option:hover {
|
||
border-color: #46b450;
|
||
}
|
||
|
||
.yoone-subscription-options .purchase-option.selected {
|
||
border-color: #46b450;
|
||
background: #f0f8f0;
|
||
}
|
||
|
||
.yoone-subscription-options .purchase-option-label {
|
||
display: block;
|
||
padding: 15px;
|
||
cursor: pointer;
|
||
margin: 0;
|
||
}
|
||
|
||
.yoone-subscription-options .purchase-option-label input[type="radio"] {
|
||
margin: 0 10px 0 0;
|
||
}
|
||
|
||
.yoone-subscription-options .option-content {
|
||
display: block;
|
||
}
|
||
|
||
.yoone-subscription-options .option-title {
|
||
display: block;
|
||
font-weight: bold;
|
||
font-size: 16px;
|
||
margin-bottom: 5px;
|
||
color: #333;
|
||
}
|
||
|
||
.yoone-subscription-options .savings-badge {
|
||
background: #dc3232;
|
||
color: white;
|
||
padding: 2px 8px;
|
||
border-radius: 10px;
|
||
font-size: 11px;
|
||
font-weight: bold;
|
||
margin-left: 10px;
|
||
}
|
||
|
||
.yoone-subscription-options .option-price {
|
||
display: block;
|
||
font-size: 18px;
|
||
font-weight: bold;
|
||
color: #46b450;
|
||
margin-bottom: 5px;
|
||
}
|
||
|
||
.yoone-subscription-options .option-price small {
|
||
font-size: 14px;
|
||
color: #666;
|
||
font-weight: normal;
|
||
}
|
||
|
||
.yoone-subscription-options .option-description {
|
||
display: block;
|
||
color: #666;
|
||
font-size: 14px;
|
||
}
|
||
|
||
.yoone-subscription-options .subscription-details {
|
||
margin-top: 20px;
|
||
padding: 20px;
|
||
background: white;
|
||
border-radius: 5px;
|
||
border: 1px solid #e1e1e1;
|
||
}
|
||
|
||
.yoone-subscription-options .subscription-details h5 {
|
||
margin: 0 0 15px 0;
|
||
color: #333;
|
||
font-size: 16px;
|
||
}
|
||
|
||
.yoone-subscription-options .pricing-breakdown {
|
||
display: grid;
|
||
gap: 10px;
|
||
margin-bottom: 20px;
|
||
}
|
||
|
||
.yoone-subscription-options .pricing-item {
|
||
display: flex;
|
||
justify-content: space-between;
|
||
align-items: center;
|
||
padding: 8px 0;
|
||
border-bottom: 1px solid #f0f0f0;
|
||
}
|
||
|
||
.yoone-subscription-options .pricing-item:last-child {
|
||
border-bottom: none;
|
||
}
|
||
|
||
.yoone-subscription-options .pricing-item.trial .value.free {
|
||
color: #46b450;
|
||
font-weight: bold;
|
||
}
|
||
|
||
.yoone-subscription-options .pricing-item.recurring {
|
||
background: #f0f8f0;
|
||
padding: 12px;
|
||
border-radius: 3px;
|
||
border: none;
|
||
margin: 10px 0;
|
||
}
|
||
|
||
.yoone-subscription-options .pricing-item.recurring .value {
|
||
font-size: 18px;
|
||
font-weight: bold;
|
||
color: #46b450;
|
||
}
|
||
|
||
.yoone-subscription-options .benefits-list {
|
||
list-style: none;
|
||
padding: 0;
|
||
margin: 0;
|
||
}
|
||
|
||
.yoone-subscription-options .benefits-list li {
|
||
display: flex;
|
||
align-items: center;
|
||
gap: 10px;
|
||
padding: 8px 0;
|
||
color: #333;
|
||
}
|
||
|
||
.yoone-subscription-options .benefit-icon {
|
||
font-size: 18px;
|
||
}
|
||
|
||
.yoone-subscription-options .subscription-terms {
|
||
margin-top: 20px;
|
||
padding: 15px;
|
||
background: #f8f8f8;
|
||
border-radius: 3px;
|
||
border-left: 4px solid #46b450;
|
||
}
|
||
|
||
.yoone-subscription-options .terms-content p {
|
||
margin: 0 0 10px 0;
|
||
color: #666;
|
||
font-size: 14px;
|
||
line-height: 1.5;
|
||
}
|
||
|
||
.yoone-subscription-options .terms-content p:last-child {
|
||
margin-bottom: 0;
|
||
}
|
||
|
||
@media (max-width: 768px) {
|
||
.yoone-subscription-options {
|
||
padding: 15px;
|
||
}
|
||
|
||
.yoone-subscription-options .purchase-option-label {
|
||
padding: 12px;
|
||
}
|
||
|
||
.yoone-subscription-options .option-title {
|
||
font-size: 14px;
|
||
}
|
||
|
||
.yoone-subscription-options .option-price {
|
||
font-size: 16px;
|
||
}
|
||
|
||
.yoone-subscription-options .pricing-item {
|
||
flex-direction: column;
|
||
align-items: flex-start;
|
||
gap: 5px;
|
||
}
|
||
|
||
.yoone-subscription-options .benefits-list li {
|
||
flex-direction: column;
|
||
align-items: flex-start;
|
||
text-align: left;
|
||
}
|
||
}
|
||
</style>
|
||
|
||
<script>
|
||
jQuery(document).ready(function($) {
|
||
var $subscriptionOptions = $('.yoone-subscription-options');
|
||
var $purchaseOptions = $subscriptionOptions.find('input[name="yoone_purchase_type"]');
|
||
var $subscriptionDetails = $subscriptionOptions.find('.subscription-details');
|
||
var $hiddenField = $subscriptionOptions.find('input[name="yoone_subscription_data"]');
|
||
|
||
// 购买方式切换
|
||
$purchaseOptions.on('change', function() {
|
||
var purchaseType = $(this).val();
|
||
var $option = $(this).closest('.purchase-option');
|
||
|
||
// 更新选中状态
|
||
$('.purchase-option').removeClass('selected');
|
||
$option.addClass('selected');
|
||
|
||
if (purchaseType === 'subscription') {
|
||
// 显示订阅详情
|
||
$subscriptionDetails.slideDown();
|
||
|
||
// 更新产品价格显示
|
||
updateProductPrice('subscription');
|
||
|
||
// 设置订阅数据
|
||
var subscriptionData = {
|
||
type: 'subscription',
|
||
price: '<?php echo $subscription_price; ?>',
|
||
billing_period: '<?php echo $billing_period; ?>',
|
||
billing_interval: '<?php echo $billing_interval; ?>',
|
||
trial_period: '<?php echo $trial_period; ?>',
|
||
trial_length: '<?php echo $trial_length; ?>',
|
||
signup_fee: '<?php echo $signup_fee; ?>'
|
||
};
|
||
$hiddenField.val(JSON.stringify(subscriptionData));
|
||
|
||
} else {
|
||
// 隐藏订阅详情
|
||
$subscriptionDetails.slideUp();
|
||
|
||
// 恢复原价格显示
|
||
updateProductPrice('one_time');
|
||
|
||
// 清除订阅数据
|
||
$hiddenField.val('');
|
||
}
|
||
});
|
||
|
||
// 更新产品价格显示
|
||
function updateProductPrice(type) {
|
||
var $priceElement = $('.price .woocommerce-Price-amount');
|
||
|
||
if (type === 'subscription') {
|
||
var subscriptionPrice = '<?php echo Yoone_Helper::format_price($subscription_price); ?>';
|
||
var billingPeriod = '<?php echo Yoone_Helper::format_billing_period($billing_period, $billing_interval); ?>';
|
||
|
||
$priceElement.html(subscriptionPrice + ' <small>/ ' + billingPeriod + '</small>');
|
||
|
||
// 添加试用期信息
|
||
<?php if ($trial_period && $trial_length > 0): ?>
|
||
var trialInfo = '<?php printf(__("免费试用 %d %s", "yoone-subscriptions"), $trial_length, Yoone_Helper::get_period_label($trial_period)); ?>';
|
||
if ($('.trial-info').length === 0) {
|
||
$('<div class="trial-info" style="color: #46b450; font-size: 14px; margin-top: 5px;">' + trialInfo + '</div>').insertAfter($priceElement);
|
||
}
|
||
<?php endif; ?>
|
||
|
||
} else {
|
||
var regularPrice = '<?php echo Yoone_Helper::format_price($regular_price); ?>';
|
||
$priceElement.html(regularPrice);
|
||
$('.trial-info').remove();
|
||
}
|
||
}
|
||
|
||
// 初始化状态
|
||
$purchaseOptions.filter(':checked').trigger('change');
|
||
});
|
||
</script>
|