subscription/templates/admin/bundle-edit.php

327 lines
14 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; // 防止直接访问
}
// $bundle 和 $items 变量由调用文件传入
// 获取所有产品用于选择
$products = wc_get_products(array(
'status' => 'publish',
'limit' => -1,
'orderby' => 'title',
'order' => 'ASC'
));
?>
<div class="wrap">
<h1><?php printf(__('编辑混装产品 #%d', 'yoone-subscriptions'), $bundle->id); ?></h1>
<form method="post" action="" id="bundle-form">
<?php wp_nonce_field('edit_bundle_' . $bundle->id); ?>
<table class="form-table">
<tbody>
<tr>
<th scope="row">
<label for="bundle_id"><?php _e('混装产品ID', 'yoone-subscriptions'); ?></label>
</th>
<td>
<input type="text" id="bundle_id" value="<?php echo esc_attr($bundle->id); ?>" readonly class="regular-text" />
<p class="description"><?php _e('混装产品的唯一标识符', 'yoone-subscriptions'); ?></p>
</td>
</tr>
<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="<?php echo esc_attr($bundle->name); ?>" 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"><?php echo esc_textarea($bundle->description); ?></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 selected($bundle->discount_type, 'none'); ?>><?php _e('无折扣', 'yoone-subscriptions'); ?></option>
<option value="percentage" <?php selected($bundle->discount_type, 'percentage'); ?>><?php _e('百分比折扣', 'yoone-subscriptions'); ?></option>
<option value="fixed" <?php selected($bundle->discount_type, '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="<?php echo esc_attr($bundle->discount_value); ?>" 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 selected($bundle->status, 'active'); ?>><?php _e('启用', 'yoone-subscriptions'); ?></option>
<option value="inactive" <?php selected($bundle->status, 'inactive'); ?>><?php _e('禁用', 'yoone-subscriptions'); ?></option>
</select>
<p class="description"><?php _e('混装产品的当前状态', 'yoone-subscriptions'); ?></p>
</td>
</tr>
<tr>
<th scope="row">
<label for="created_at"><?php _e('创建时间', 'yoone-subscriptions'); ?></label>
</th>
<td>
<input type="text" value="<?php echo date_i18n(get_option('date_format') . ' ' . get_option('time_format'), strtotime($bundle->created_at)); ?>" readonly class="regular-text" />
<p class="description"><?php _e('混装产品创建的时间(只读)', 'yoone-subscriptions'); ?></p>
</td>
</tr>
<tr>
<th scope="row">
<label for="updated_at"><?php _e('更新时间', 'yoone-subscriptions'); ?></label>
</th>
<td>
<input type="text" value="<?php echo date_i18n(get_option('date_format') . ' ' . get_option('time_format'), strtotime($bundle->updated_at)); ?>" readonly class="regular-text" />
<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">
<?php if (!empty($items)): ?>
<?php foreach ($items as $index => $item): ?>
<?php $product = wc_get_product($item->product_id); ?>
<div class="bundle-item" data-index="<?php echo $index; ?>">
<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[<?php echo $index; ?>][product_id]" class="product-select" style="width: 100%;">
<option value=""><?php _e('选择产品', 'yoone-subscriptions'); ?></option>
<?php foreach ($products as $product_option): ?>
<option value="<?php echo $product_option->get_id(); ?>" <?php selected($item->product_id, $product_option->get_id()); ?>>
<?php echo esc_html($product_option->get_name() . ' (#' . $product_option->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[<?php echo $index; ?>][quantity]" value="<?php echo esc_attr($item->quantity); ?>" min="1" class="small-text" />
</td>
<td>
<button type="button" class="button remove-item"><?php _e('移除', 'yoone-subscriptions'); ?></button>
</td>
</tr>
</tbody>
</table>
</div>
<?php endforeach; ?>
<?php endif; ?>
</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 = <?php echo count($items); ?>;
// 添加混装项目
$('#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();
});
// 折扣类型变化时更新描述
function updateDiscountDescription() {
var type = $('#discount_type').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'); ?>');
break;
}
}
$('#discount_type').on('change', updateDiscountDescription);
// 初始化折扣描述
updateDiscountDescription();
// 表单验证
$('#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;
}
});
});
</script>