subscription/templates/admin/bundle-add.php

260 lines
9.8 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
/**
* 添加混装产品页面模板
*
* @package Yoone_Subscriptions
* @version 1.0.0
*/
if (!defined('ABSPATH')) {
exit; // 防止直接访问
}
// 获取所有产品用于选择
$products = wc_get_products(array(
'status' => 'publish',
'limit' => -1,
'orderby' => 'title',
'order' => 'ASC'
));
?>
<div class="wrap">
<h1><?php _e('添加混装产品', 'yoone-subscriptions'); ?></h1>
<form method="post" action="" id="bundle-form">
<?php wp_nonce_field('add_bundle'); ?>
<table class="form-table">
<tbody>
<tr>
<th scope="row">
<label for="name"><?php _e('混装产品名称', 'yoone-subscriptions'); ?> <span class="required">*</span></label>
</th>
<td>
<input type="text" name="name" id="name" value="" class="regular-text" required />
<p class="description"><?php _e('混装产品的显示名称', 'yoone-subscriptions'); ?></p>
</td>
</tr>
<tr>
<th scope="row">
<label for="description"><?php _e('描述', 'yoone-subscriptions'); ?></label>
</th>
<td>
<textarea name="description" id="description" rows="4" class="large-text"></textarea>
<p class="description"><?php _e('混装产品的详细描述', 'yoone-subscriptions'); ?></p>
</td>
</tr>
<tr>
<th scope="row">
<label for="discount_type"><?php _e('折扣类型', 'yoone-subscriptions'); ?></label>
</th>
<td>
<select name="discount_type" id="discount_type" class="regular-text">
<option value="none"><?php _e('无折扣', 'yoone-subscriptions'); ?></option>
<option value="percentage"><?php _e('百分比折扣', 'yoone-subscriptions'); ?></option>
<option value="fixed"><?php _e('固定金额折扣', 'yoone-subscriptions'); ?></option>
</select>
<p class="description"><?php _e('选择折扣的计算方式', 'yoone-subscriptions'); ?></p>
</td>
</tr>
<tr>
<th scope="row">
<label for="discount_value"><?php _e('折扣值', 'yoone-subscriptions'); ?></label>
</th>
<td>
<input type="number" name="discount_value" id="discount_value" value="0" min="0" step="0.01" class="regular-text" />
<p class="description" id="discount-description"><?php _e('折扣的具体数值', 'yoone-subscriptions'); ?></p>
</td>
</tr>
<tr>
<th scope="row">
<label for="status"><?php _e('状态', 'yoone-subscriptions'); ?></label>
</th>
<td>
<select name="status" id="status" class="regular-text">
<option value="active"><?php _e('启用', 'yoone-subscriptions'); ?></option>
<option value="inactive"><?php _e('禁用', 'yoone-subscriptions'); ?></option>
</select>
<p class="description"><?php _e('混装产品的当前状态', 'yoone-subscriptions'); ?></p>
</td>
</tr>
</tbody>
</table>
<h2><?php _e('混装产品项目', 'yoone-subscriptions'); ?></h2>
<p><?php _e('选择要包含在此混装产品中的产品', 'yoone-subscriptions'); ?></p>
<div id="bundle-items">
<div class="bundle-item-template" style="display: none;">
<div class="bundle-item" data-index="0">
<table class="form-table">
<tbody>
<tr>
<th scope="row" style="width: 150px;">
<label><?php _e('产品', 'yoone-subscriptions'); ?></label>
</th>
<td style="width: 300px;">
<select name="bundle_items[0][product_id]" class="product-select" style="width: 100%;">
<option value=""><?php _e('选择产品', 'yoone-subscriptions'); ?></option>
<?php foreach ($products as $product): ?>
<option value="<?php echo $product->get_id(); ?>">
<?php echo esc_html($product->get_name() . ' (#' . $product->get_id() . ')'); ?>
</option>
<?php endforeach; ?>
</select>
</td>
<th scope="row" style="width: 100px;">
<label><?php _e('数量', 'yoone-subscriptions'); ?></label>
</th>
<td style="width: 100px;">
<input type="number" name="bundle_items[0][quantity]" value="1" min="1" class="small-text" />
</td>
<td>
<button type="button" class="button remove-item"><?php _e('移除', 'yoone-subscriptions'); ?></button>
</td>
</tr>
</tbody>
</table>
</div>
</div>
<div id="bundle-items-container">
<!-- 动态添加的混装项目将显示在这里 -->
</div>
<p>
<button type="button" id="add-bundle-item" class="button button-secondary">
<?php _e('添加产品', 'yoone-subscriptions'); ?>
</button>
</p>
</div>
<?php submit_button(__('保存混装产品', 'yoone-subscriptions')); ?>
</form>
<p>
<a href="<?php echo admin_url('admin.php?page=yoone-bundles'); ?>" class="button">
<?php _e('返回混装产品列表', 'yoone-subscriptions'); ?>
</a>
</p>
</div>
<style>
.bundle-item {
border: 1px solid #ddd;
margin-bottom: 15px;
padding: 15px;
background: #f9f9f9;
border-radius: 4px;
}
.bundle-item .form-table {
margin: 0;
}
.bundle-item .form-table th,
.bundle-item .form-table td {
padding: 5px 10px;
vertical-align: middle;
}
.required {
color: #d63638;
}
#bundle-items-container:empty::after {
content: "<?php _e('暂无产品项目,请点击"添加产品"按钮添加', 'yoone-subscriptions'); ?>";
color: #666;
font-style: italic;
display: block;
padding: 20px;
text-align: center;
border: 2px dashed #ddd;
border-radius: 4px;
}
</style>
<script>
jQuery(document).ready(function($) {
var itemIndex = 0;
// 添加混装项目
$('#add-bundle-item').on('click', function() {
var template = $('.bundle-item-template').html();
template = template.replace(/\[0\]/g, '[' + itemIndex + ']');
template = template.replace(/data-index="0"/g, 'data-index="' + itemIndex + '"');
$('#bundle-items-container').append(template);
itemIndex++;
});
// 移除混装项目
$(document).on('click', '.remove-item', function() {
$(this).closest('.bundle-item').remove();
});
// 折扣类型变化时更新描述
$('#discount_type').on('change', function() {
var type = $(this).val();
var description = $('#discount-description');
var valueField = $('#discount_value');
switch(type) {
case 'percentage':
description.text('<?php _e('输入百分比数值例如10 表示 10% 折扣)', 'yoone-subscriptions'); ?>');
valueField.attr('max', '100');
break;
case 'fixed':
description.text('<?php _e('输入固定折扣金额', 'yoone-subscriptions'); ?>');
valueField.removeAttr('max');
break;
default:
description.text('<?php _e('无折扣', 'yoone-subscriptions'); ?>');
valueField.val('0');
break;
}
});
// 表单验证
$('#bundle-form').on('submit', function(e) {
var name = $('#name').val().trim();
var items = $('.bundle-item').length;
if (!name) {
alert('<?php _e('请输入混装产品名称', 'yoone-subscriptions'); ?>');
$('#name').focus();
e.preventDefault();
return false;
}
if (items === 0) {
alert('<?php _e('请至少添加一个产品项目', 'yoone-subscriptions'); ?>');
e.preventDefault();
return false;
}
// 检查是否所有产品都已选择
var hasEmptyProduct = false;
$('.product-select').each(function() {
if (!$(this).val()) {
hasEmptyProduct = true;
return false;
}
});
if (hasEmptyProduct) {
alert('<?php _e('请为所有项目选择产品', 'yoone-subscriptions'); ?>');
e.preventDefault();
return false;
}
});
// 初始化时添加一个空项目
$('#add-bundle-item').trigger('click');
});
</script>