127 lines
5.5 KiB
PHP
127 lines
5.5 KiB
PHP
<?php
|
||
/**
|
||
* 后台:产品编辑页订阅计划配置面板。
|
||
*/
|
||
defined('ABSPATH') || exit;
|
||
|
||
class Yoone_Subscriptions_Admin {
|
||
protected static $instance = null;
|
||
|
||
public static function instance() {
|
||
if (null === self::$instance) self::$instance = new self();
|
||
return self::$instance;
|
||
}
|
||
|
||
private function __construct() {
|
||
add_filter('woocommerce_product_data_tabs', array($this, 'add_tab'));
|
||
add_action('woocommerce_product_data_panels', array($this, 'render_panel'));
|
||
add_action('woocommerce_admin_process_product_meta', array($this, 'save_meta'));
|
||
}
|
||
|
||
/**
|
||
* 在产品数据区域添加“订阅计划”标签
|
||
*/
|
||
public function add_tab($tabs) {
|
||
$tabs['yoone_subscriptions'] = array(
|
||
'label' => __('订阅计划', 'yoone-subscriptions'),
|
||
'target' => 'yoone_subscriptions_data',
|
||
'class' => array('show_if_simple', 'show_if_variable'),
|
||
'priority' => 80,
|
||
);
|
||
return $tabs;
|
||
}
|
||
|
||
/**
|
||
* 渲染面板内容
|
||
*/
|
||
public function render_panel() {
|
||
global $post;
|
||
$product = wc_get_product($post->ID);
|
||
$cfg = Yoone_Subscriptions::get_config($product);
|
||
wp_nonce_field('yoone_subscriptions_save', 'yoone_subscriptions_nonce');
|
||
|
||
echo '<div id="yoone_subscriptions_data" class="panel woocommerce_options_panel hidden">';
|
||
echo '<div class="options_group">';
|
||
|
||
// 启用订阅
|
||
woocommerce_wp_checkbox(array(
|
||
'id' => 'yoone_sub_enabled',
|
||
'label' => __('启用订阅', 'yoone-subscriptions'),
|
||
'description' => __('开启后,前端产品页将显示订阅选项。', 'yoone-subscriptions'),
|
||
'desc_tip' => true,
|
||
'value' => $cfg['enabled'] ? 'yes' : 'no',
|
||
));
|
||
|
||
// 周期(月/年)
|
||
woocommerce_wp_select(array(
|
||
'id' => 'yoone_sub_period',
|
||
'label' => __('订阅周期', 'yoone-subscriptions'),
|
||
'options' => array('month' => __('月', 'yoone-subscriptions'), 'year' => __('年', 'yoone-subscriptions')),
|
||
'value' => $cfg['period'],
|
||
'desc_tip' => true,
|
||
'description' => __('订阅价格以该周期计费。年=12×月。', 'yoone-subscriptions'),
|
||
));
|
||
|
||
// 默认订阅数量
|
||
woocommerce_wp_text_input(array(
|
||
'id' => 'yoone_sub_qty_default',
|
||
'label' => __('默认订阅数量', 'yoone-subscriptions'),
|
||
'type' => 'number',
|
||
'value' => $cfg['qty_default'],
|
||
'custom_attributes' => array('min' => '1', 'step' => '1'),
|
||
'desc_tip' => true,
|
||
'description' => __('用于前端默认数量,可在产品页调整。', 'yoone-subscriptions'),
|
||
));
|
||
|
||
// 订阅价格(可选,留空则使用产品常规价)
|
||
woocommerce_wp_text_input(array(
|
||
'id' => 'yoone_sub_price',
|
||
'label' => __('订阅价格(每周期)', 'yoone-subscriptions'),
|
||
'type' => 'text',
|
||
'value' => $cfg['price'] > 0 ? wc_format_decimal($cfg['price'], 2) : '',
|
||
'desc_tip' => true,
|
||
'description' => __('留空表示使用产品常规价。', 'yoone-subscriptions'),
|
||
));
|
||
|
||
// 是否允许一次性购买
|
||
woocommerce_wp_checkbox(array(
|
||
'id' => 'yoone_sub_allow_onetime',
|
||
'label' => __('允许一次性购买', 'yoone-subscriptions'),
|
||
'description' => __('开启后,前端产品页可选择一次性购买或订阅购买。', 'yoone-subscriptions'),
|
||
'desc_tip' => true,
|
||
'value' => $cfg['allow_onetime'] ? 'yes' : 'no',
|
||
));
|
||
|
||
echo '</div></div>';
|
||
}
|
||
|
||
/**
|
||
* 保存订阅配置
|
||
*/
|
||
public function save_meta($post_id) {
|
||
if (! isset($_POST['yoone_subscriptions_nonce']) || ! wp_verify_nonce($_POST['yoone_subscriptions_nonce'], 'yoone_subscriptions_save')) {
|
||
return; // 安全验证失败
|
||
}
|
||
if (! current_user_can('edit_post', $post_id)) return;
|
||
|
||
$enabled = isset($_POST['yoone_sub_enabled']) && 'yes' === $_POST['yoone_sub_enabled'];
|
||
$period = isset($_POST['yoone_sub_period']) ? sanitize_text_field($_POST['yoone_sub_period']) : 'month';
|
||
$qty = isset($_POST['yoone_sub_qty_default']) ? absint($_POST['yoone_sub_qty_default']) : 1;
|
||
$price = isset($_POST['yoone_sub_price']) ? wc_clean($_POST['yoone_sub_price']) : '';
|
||
$onetime = isset($_POST['yoone_sub_allow_onetime']) && 'yes' === $_POST['yoone_sub_allow_onetime'];
|
||
|
||
$period = in_array($period, array('month','year'), true) ? $period : 'month';
|
||
$qty = max(1, $qty);
|
||
$price_v = ($price === '' ? '' : wc_format_decimal($price, 2));
|
||
|
||
update_post_meta($post_id, Yoone_Subscriptions::META_ENABLED, $enabled ? '1' : '');
|
||
update_post_meta($post_id, Yoone_Subscriptions::META_PERIOD, $period);
|
||
update_post_meta($post_id, Yoone_Subscriptions::META_QTY_DEFAULT, $qty);
|
||
if ($price_v === '') {
|
||
delete_post_meta($post_id, Yoone_Subscriptions::META_PRICE);
|
||
} else {
|
||
update_post_meta($post_id, Yoone_Subscriptions::META_PRICE, $price_v);
|
||
}
|
||
update_post_meta($post_id, Yoone_Subscriptions::META_ALLOW_ONETIME, $onetime ? '1' : '');
|
||
}
|
||
} |