文章目录[隐藏]
WordPress文创商品柔性组合与拆销售后管理插件教程
一、插件概述与安装配置
在文创电商领域,商品往往具有组合销售的特性。一款陶瓷茶具可能包含茶杯、茶壶、茶盘等多个组件,客户可能希望单独购买某个组件,或者组合购买整套产品。本教程将详细介绍如何开发一个WordPress插件,实现文创商品的柔性组合与拆销售后管理功能。
插件安装与激活
首先,我们需要创建一个基础的插件结构。在WordPress的wp-content/plugins目录下创建新文件夹flexible-cultural-products,并在其中创建主文件:
<?php
/**
* Plugin Name: 文创商品柔性组合与拆销售后管理
* Plugin URI: https://yourwebsite.com/
* Description: 实现文创商品的组合销售、拆分销售及售后管理功能
* Version: 1.0.0
* Author: 您的名称
* License: GPL v2 or later
*/
// 防止直接访问
if (!defined('ABSPATH')) {
exit;
}
// 定义插件常量
define('FCP_PLUGIN_PATH', plugin_dir_path(__FILE__));
define('FCP_PLUGIN_URL', plugin_dir_url(__FILE__));
// 初始化插件
class FlexibleCulturalProducts {
public function __construct() {
$this->init_hooks();
}
private function init_hooks() {
// 激活和停用钩子
register_activation_hook(__FILE__, array($this, 'activate'));
register_deactivation_hook(__FILE__, array($this, 'deactivate'));
// 管理界面钩子
add_action('admin_menu', array($this, 'add_admin_menu'));
// 产品类型钩子
add_filter('product_type_selector', array($this, 'add_product_type'));
add_action('woocommerce_product_options_general_product_data', array($this, 'add_product_settings'));
// 保存产品设置
add_action('woocommerce_process_product_meta', array($this, 'save_product_settings'));
}
public function activate() {
// 创建必要的数据库表
$this->create_database_tables();
// 设置默认选项
update_option('fcp_version', '1.0.0');
// 添加管理员通知
set_transient('fcp_activation_notice', true, 5);
}
public function deactivate() {
// 清理临时数据
delete_transient('fcp_activation_notice');
}
private function create_database_tables() {
global $wpdb;
$charset_collate = $wpdb->get_charset_collate();
$table_name = $wpdb->prefix . 'fcp_product_components';
$sql = "CREATE TABLE IF NOT EXISTS $table_name (
id mediumint(9) NOT NULL AUTO_INCREMENT,
parent_product_id bigint(20) NOT NULL,
component_product_id bigint(20) NOT NULL,
component_quantity int(11) DEFAULT 1,
is_optional tinyint(1) DEFAULT 0,
sort_order int(11) DEFAULT 0,
created_at datetime DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (id),
KEY parent_product_id (parent_product_id),
KEY component_product_id (component_product_id)
) $charset_collate;";
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
dbDelta($sql);
}
public function add_admin_menu() {
add_menu_page(
'文创商品管理',
'文创商品',
'manage_options',
'fcp-dashboard',
array($this, 'render_dashboard'),
'dashicons-products',
56
);
}
public function render_dashboard() {
echo '<div class="wrap">';
echo '<h1>文创商品柔性组合管理系统</h1>';
echo '<p>欢迎使用文创商品管理插件。请使用左侧菜单管理商品组合。</p>';
echo '</div>';
}
public function add_product_type($types) {
$types['flexible_cultural'] = __('文创组合商品', 'fcp');
return $types;
}
public function add_product_settings() {
global $product_object;
if ($product_object->get_type() == 'flexible_cultural') {
echo '<div class="options_group">';
// 组合商品设置
woocommerce_wp_checkbox(array(
'id' => '_fcp_is_flexible',
'label' => __('启用柔性组合', 'fcp'),
'description' => __('允许客户自定义组合商品组件', 'fcp'),
'desc_tip' => true,
));
// 最大可选组件数量
woocommerce_wp_text_input(array(
'id' => '_fcp_max_components',
'label' => __('最大可选组件数', 'fcp'),
'type' => 'number',
'desc_tip' => true,
'description' => __('客户最多可以选择多少个组件(0表示无限制)', 'fcp'),
));
echo '</div>';
}
}
public function save_product_settings($product_id) {
$product = wc_get_product($product_id);
if ($product->get_type() == 'flexible_cultural') {
// 保存柔性组合设置
$is_flexible = isset($_POST['_fcp_is_flexible']) ? 'yes' : 'no';
update_post_meta($product_id, '_fcp_is_flexible', $is_flexible);
// 保存最大组件数
if (isset($_POST['_fcp_max_components'])) {
update_post_meta($product_id, '_fcp_max_components', absint($_POST['_fcp_max_components']));
}
}
}
}
// 初始化插件
$flexible_cultural_products = new FlexibleCulturalProducts();
二、商品组件管理功能实现
组件管理界面
// 在FlexibleCulturalProducts类中添加以下方法
public function add_component_management() {
add_submenu_page(
'fcp-dashboard',
'商品组件管理',
'组件管理',
'manage_options',
'fcp-components',
array($this, 'render_component_management')
);
}
public function render_component_management() {
global $wpdb;
// 处理表单提交
if (isset($_POST['action']) && $_POST['action'] == 'add_component') {
$this->handle_add_component();
}
// 获取所有组合商品
$products = wc_get_products(array(
'type' => 'flexible_cultural',
'limit' => -1,
));
echo '<div class="wrap">';
echo '<h1>商品组件管理</h1>';
// 添加组件表单
echo '<div class="card" style="max-width: 600px; margin-bottom: 20px;">';
echo '<h2>添加新组件</h2>';
echo '<form method="post">';
echo '<input type="hidden" name="action" value="add_component">';
// 选择父商品
echo '<p>';
echo '<label for="parent_product"><strong>组合商品:</strong></label><br>';
echo '<select name="parent_product_id" id="parent_product" required>';
echo '<option value="">选择组合商品...</option>';
foreach ($products as $product) {
echo '<option value="' . $product->get_id() . '">' . $product->get_name() . '</option>';
}
echo '</select>';
echo '</p>';
// 选择组件商品
echo '<p>';
echo '<label for="component_product"><strong>组件商品:</strong></label><br>';
echo '<select name="component_product_id" id="component_product" required>';
echo '<option value="">选择组件商品...</option>';
// 这里需要获取所有简单商品
$simple_products = wc_get_products(array(
'type' => 'simple',
'limit' => -1,
));
foreach ($simple_products as $product) {
echo '<option value="' . $product->get_id() . '">' . $product->get_name() . '</option>';
}
echo '</select>';
echo '</p>';
// 组件数量
echo '<p>';
echo '<label for="component_quantity"><strong>默认数量:</strong></label><br>';
echo '<input type="number" name="component_quantity" id="component_quantity" value="1" min="1">';
echo '</p>';
// 是否可选
echo '<p>';
echo '<label for="is_optional">';
echo '<input type="checkbox" name="is_optional" id="is_optional" value="1">';
echo ' 可选组件(客户可以选择是否包含此组件)';
echo '</label>';
echo '</p>';
submit_button('添加组件');
echo '</form>';
echo '</div>';
// 显示现有组件
echo '<h2>现有组件列表</h2>';
$table_name = $wpdb->prefix . 'fcp_product_components';
$components = $wpdb->get_results("
SELECT c.*, p1.post_title as parent_name, p2.post_title as component_name
FROM $table_name c
LEFT JOIN {$wpdb->posts} p1 ON c.parent_product_id = p1.ID
LEFT JOIN {$wpdb->posts} p2 ON c.component_product_id = p2.ID
ORDER BY c.parent_product_id, c.sort_order
");
if ($components) {
echo '<table class="wp-list-table widefat fixed striped">';
echo '<thead>';
echo '<tr>';
echo '<th>组合商品</th>';
echo '<th>组件商品</th>';
echo '<th>数量</th>';
echo '<th>是否可选</th>';
echo '<th>排序</th>';
echo '<th>操作</th>';
echo '</tr>';
echo '</thead>';
echo '<tbody>';
foreach ($components as $component) {
echo '<tr>';
echo '<td>' . esc_html($component->parent_name) . '</td>';
echo '<td>' . esc_html($component->component_name) . '</td>';
echo '<td>' . esc_html($component->component_quantity) . '</td>';
echo '<td>' . ($component->is_optional ? '是' : '否') . '</td>';
echo '<td>' . esc_html($component->sort_order) . '</td>';
echo '<td>';
echo '<a href="#" class="button button-small">编辑</a> ';
echo '<a href="#" class="button button-small button-link-delete">删除</a>';
echo '</td>';
echo '</tr>';
}
echo '</tbody>';
echo '</table>';
} else {
echo '<p>暂无组件数据。</p>';
}
echo '</div>';
}
private function handle_add_component() {
global $wpdb;
if (!wp_verify_nonce($_POST['_wpnonce'], 'add_component')) {
wp_die('安全验证失败');
}
$table_name = $wpdb->prefix . 'fcp_product_components';
$data = array(
'parent_product_id' => intval($_POST['parent_product_id']),
'component_product_id' => intval($_POST['component_product_id']),
'component_quantity' => intval($_POST['component_quantity']),
'is_optional' => isset($_POST['is_optional']) ? 1 : 0,
'sort_order' => 0,
);
$result = $wpdb->insert($table_name, $data);
if ($result) {
echo '<div class="notice notice-success is-dismissible"><p>组件添加成功!</p></div>';
} else {
echo '<div class="notice notice-error is-dismissible"><p>组件添加失败:' . $wpdb->last_error . '</p></div>';
}
}
三、前端商品展示与选择
前端组件选择界面
// 添加前端展示功能
class FCP_Frontend {
public function __construct() {
// 在产品页面添加组件选择
add_action('woocommerce_before_add_to_cart_button', array($this, 'display_component_selection'));
// 处理添加到购物车
add_filter('woocommerce_add_to_cart_validation', array($this, 'validate_component_selection'), 10, 3);
add_action('woocommerce_add_to_cart', array($this, 'save_component_selection'), 10, 6);
// 在购物车中显示组件信息
add_filter('woocommerce_get_item_data', array($this, 'display_cart_item_components'), 10, 2);
}
public function display_component_selection() {
global $product;
if ($product->get_type() !== 'flexible_cultural') {
return;
}
$product_id = $product->get_id();
$components = $this->get_product_components($product_id);
if (empty($components)) {
return;
}
echo '<div class="fcp-component-selection" style="margin: 20px 0; padding: 20px; border: 1px solid #ddd; border-radius: 5px;">';
echo '<h3>自定义您的文创组合</h3>';
echo '<p>请选择您需要的组件:</p>';
foreach ($components as $component) {
$component_product = wc_get_product($component->component_product_id);
if (!$component_product) {
continue;
}
echo '<div class="fcp-component-item" style="margin: 10px 0; padding: 10px; background: #f9f9f9;">';
// 组件名称和价格
echo '<div style="display: flex; justify-content: space-between; align-items: center;">';
echo '<div>';
echo '<strong>' . esc_html($component_product->get_name()) . '</strong>';
echo '<span style="margin-left: 10px; color: #666;">' . wc_price($component_product->get_price()) . '</span>';
echo '</div>';
// 选择控件
if ($component->is_optional) {
echo '<div>';
echo '<label>';
echo '<input type="checkbox"
name="fcp_components[' . $component->component_product_id . '][selected]"
value="1"
checked
data-component-id="' . $component->component_product_id . '">';
echo ' 包含此组件';
echo '</label>';
// 数量选择(如果可选)
echo '<span style="margin-left: 10px;">';
echo '数量: ';
echo '<input type="number"
name="fcp_components[' . $component->component_product_id . '][quantity]"
value="' . $component->component_quantity . '"
min="1"
max="10"
style="width: 60px;">';
echo '</span>';
echo '</div>';
} else {
// 必选组件
echo '<div>';
echo '<span style="color: #999;">必选组件</span>';
echo '<input type="hidden"
name="fcp_components[' . $component->component_product_id . '][selected]"
value="1">';
echo '<input type="hidden"
name="fcp_components[' . $component->component_product_id . '][quantity]"
value="' . $component->component_quantity . '">';
echo '</div>';
}
echo '</div>';
echo '</div>';
}
echo '</div>';
// 添加JavaScript处理
$this->add_frontend_scripts();
}
private function get_product_components($product_id) {
global $wpdb;
$table_name = $wpdb->prefix . 'fcp_product_components';
return $wpdb->get_results($wpdb->prepare(
"SELECT * FROM $table_name WHERE parent_product_id = %d ORDER BY sort_order",
$product_id
));
}
private function add_frontend_scripts() {
?>
<script type="text/javascript">
jQuery(document).ready(function($) {
// 动态计算总价
function calculateTotalPrice() {
var total = parseFloat('<?php echo $product->get_price(); ?>');
$('.fcp-component-item').each(function() {
var $item = $(this);
var componentId = $item.find('input[type="checkbox"]').data('component-id');
var isSelected = $item.find('input[type="checkbox"]').is(':checked');
var quantity = parseInt($item.find('input[type="number"]').val()) || 1;
if (isSelected) {
// 这里需要获取组件的价格,实际应用中可能需要通过AJAX获取
var componentPrice = parseFloat($item.find('span').data('price')) || 0;
total += componentPrice * quantity;
}
});
// 更新显示的总价
$('.price .amount').text('¥' + total.toFixed(2));
}
// 绑定事件
$('.fcp-component-selection input').on('change', calculateTotalPrice);
// 初始计算
calculateTotalPrice();
});
</script>
<?php
}
public function validate_component_selection($passed, $product_id, $quantity) {
$product = wc_get_product($product_id);
if ($product->get_type() === 'flexible_cultural') {
// 检查必选组件是否都选择了
$components = $this->get_product_components($product_id);
$selected_components = isset($_POST['fcp_components']) ? $_POST['fcp_components'] : array();
foreach ($components as $component) {
if (!$component->is_optional) {
四、购物车与订单处理
验证组件选择并保存到购物车
// 继续FCP_Frontend类中的validate_component_selection方法
foreach ($components as $component) {
if (!$component->is_optional) {
// 检查必选组件是否被选择
if (!isset($selected_components[$component->component_product_id]['selected']) ||
$selected_components[$component->component_product_id]['selected'] != '1') {
wc_add_notice(
sprintf(__('必须选择组件:%s', 'fcp'),
get_the_title($component->component_product_id)),
'error'
);
$passed = false;
}
}
}
// 检查最大组件数量限制
$max_components = get_post_meta($product_id, '_fcp_max_components', true);
if ($max_components > 0) {
$selected_count = 0;
foreach ($selected_components as $component) {
if (isset($component['selected']) && $component['selected'] == '1') {
$selected_count++;
}
}
if ($selected_count > $max_components) {
wc_add_notice(
sprintf(__('最多只能选择 %d 个组件', 'fcp'), $max_components),
'error'
);
$passed = false;
}
}
}
return $passed;
}
public function save_component_selection($cart_item_key, $product_id, $quantity, $variation_id, $variation, $cart_item_data) {
$product = wc_get_product($product_id);
if ($product->get_type() === 'flexible_cultural' && isset($_POST['fcp_components'])) {
// 保存组件选择到购物车项目数据
WC()->cart->cart_contents[$cart_item_key]['fcp_components'] = $_POST['fcp_components'];
// 计算并保存自定义价格
$base_price = $product->get_price();
$components_total = 0;
foreach ($_POST['fcp_components'] as $component_id => $component_data) {
if (isset($component_data['selected']) && $component_data['selected'] == '1') {
$component_product = wc_get_product($component_id);
if ($component_product) {
$component_qty = isset($component_data['quantity']) ? intval($component_data['quantity']) : 1;
$components_total += $component_product->get_price() * $component_qty;
}
}
}
// 设置自定义价格
WC()->cart->cart_contents[$cart_item_key]['custom_price'] = $base_price + $components_total;
}
}
public function display_cart_item_components($item_data, $cart_item) {
if (isset($cart_item['fcp_components'])) {
$item_data[] = array(
'name' => __('包含组件', 'fcp'),
'value' => $this->format_components_for_display($cart_item['fcp_components'])
);
}
return $item_data;
}
private function format_components_for_display($components) {
$formatted = array();
foreach ($components as $component_id => $component_data) {
if (isset($component_data['selected']) && $component_data['selected'] == '1') {
$product = wc_get_product($component_id);
if ($product) {
$qty = isset($component_data['quantity']) ? intval($component_data['quantity']) : 1;
$formatted[] = sprintf('%s × %d', $product->get_name(), $qty);
}
}
}
return implode('<br>', $formatted);
}
}
// 初始化前端功能
new FCP_Frontend();
价格计算过滤器
// 添加价格计算功能
add_action('woocommerce_before_calculate_totals', 'fcp_custom_price_calculation', 20, 1);
function fcp_custom_price_calculation($cart) {
if (is_admin() && !defined('DOING_AJAX')) {
return;
}
if (did_action('woocommerce_before_calculate_totals') >= 2) {
return;
}
foreach ($cart->get_cart() as $cart_item_key => $cart_item) {
if (isset($cart_item['custom_price'])) {
$cart_item['data']->set_price($cart_item['custom_price']);
}
}
}
五、订单管理与售后处理
订单组件信息保存
// 创建订单管理类
class FCP_Order_Management {
public function __construct() {
// 保存订单组件信息
add_action('woocommerce_checkout_create_order_line_item', array($this, 'save_order_item_components'), 10, 4);
// 在订单详情页显示组件信息
add_action('woocommerce_order_item_meta_start', array($this, 'display_order_item_components'), 10, 3);
// 添加售后管理功能
add_action('add_meta_boxes', array($this, 'add_after_sales_meta_box'));
// 处理售后请求
add_action('wp_ajax_fcp_process_after_sales', array($this, 'process_after_sales_request'));
add_action('wp_ajax_nopriv_fcp_process_after_sales', array($this, 'process_after_sales_request'));
}
public function save_order_item_components($item, $cart_item_key, $values, $order) {
if (isset($values['fcp_components'])) {
$item->add_meta_data('_fcp_components', $values['fcp_components']);
// 保存组件详细信息用于售后管理
$components_detail = array();
foreach ($values['fcp_components'] as $component_id => $component_data) {
if (isset($component_data['selected']) && $component_data['selected'] == '1') {
$product = wc_get_product($component_id);
if ($product) {
$qty = isset($component_data['quantity']) ? intval($component_data['quantity']) : 1;
$components_detail[] = array(
'product_id' => $component_id,
'name' => $product->get_name(),
'quantity' => $qty,
'price' => $product->get_price()
);
}
}
}
$item->add_meta_data('_fcp_components_detail', $components_detail);
}
}
public function display_order_item_components($item_id, $item, $order) {
$components = $item->get_meta('_fcp_components_detail');
if (!empty($components)) {
echo '<div class="fcp-order-components" style="margin-top: 10px; padding: 10px; background: #f5f5f5; border-radius: 4px;">';
echo '<strong>' . __('包含组件:', 'fcp') . '</strong><br>';
foreach ($components as $component) {
echo sprintf(
'• %s × %d - %s<br>',
esc_html($component['name']),
$component['quantity'],
wc_price($component['price'] * $component['quantity'])
);
}
echo '</div>';
}
}
public function add_after_sales_meta_box() {
add_meta_box(
'fcp-after-sales',
__('文创商品售后管理', 'fcp'),
array($this, 'render_after_sales_meta_box'),
'shop_order',
'side',
'high'
);
}
public function render_after_sales_meta_box($post) {
$order = wc_get_order($post->ID);
$items = $order->get_items();
echo '<div class="fcp-after-sales-management">';
foreach ($items as $item_id => $item) {
$product = $item->get_product();
if ($product && $product->get_type() === 'flexible_cultural') {
$components = $item->get_meta('_fcp_components_detail');
if (!empty($components)) {
echo '<h4>' . esc_html($item->get_name()) . '</h4>';
echo '<div class="components-list">';
foreach ($components as $index => $component) {
echo '<div class="component-item" style="margin: 5px 0; padding: 5px; border: 1px solid #ddd;">';
echo '<label>';
echo '<input type="checkbox"
name="fcp_return_components[]"
value="' . $item_id . '_' . $index . '"
data-component-id="' . $component['product_id'] . '">';
echo ' ' . esc_html($component['name']) . ' × ' . $component['quantity'];
echo '</label>';
echo '</div>';
}
echo '</div>';
}
}
}
echo '<div style="margin-top: 15px;">';
echo '<label for="fcp_return_reason"><strong>' . __('退货原因:', 'fcp') . '</strong></label>';
echo '<select name="fcp_return_reason" id="fcp_return_reason" style="width: 100%; margin: 5px 0;">';
echo '<option value="">' . __('选择退货原因', 'fcp') . '</option>';
echo '<option value="damaged">' . __('商品损坏', 'fcp') . '</option>';
echo '<option value="wrong_item">' . __('发错商品', 'fcp') . '</option>';
echo '<option value="not_as_described">' . __('与描述不符', 'fcp') . '</option>';
echo '<option value="other">' . __('其他原因', 'fcp') . '</option>';
echo '</select>';
echo '<label for="fcp_return_notes"><strong>' . __('备注:', 'fcp') . '</strong></label>';
echo '<textarea name="fcp_return_notes" id="fcp_return_notes" style="width: 100%; height: 60px; margin: 5px 0;"></textarea>';
echo '<button type="button"
class="button button-primary"
onclick="fcpSubmitReturnRequest(' . $post->ID . ')">'
. __('提交退货申请', 'fcp') .
'</button>';
echo '</div>';
echo '</div>';
// 添加JavaScript
$this->add_admin_scripts();
}
private function add_admin_scripts() {
?>
<script type="text/javascript">
function fcpSubmitReturnRequest(orderId) {
var selectedComponents = [];
var reason = jQuery('#fcp_return_reason').val();
var notes = jQuery('#fcp_return_notes').val();
jQuery('input[name="fcp_return_components[]"]:checked').each(function() {
selectedComponents.push(jQuery(this).val());
});
if (selectedComponents.length === 0) {
alert('请选择要退货的组件');
return;
}
if (!reason) {
alert('请选择退货原因');
return;
}
var data = {
action: 'fcp_process_after_sales',
order_id: orderId,
components: selectedComponents,
reason: reason,
notes: notes,
security: '<?php echo wp_create_nonce('fcp_after_sales'); ?>'
};
jQuery.post(ajaxurl, data, function(response) {
if (response.success) {
alert('退货申请已提交');
location.reload();
} else {
alert('提交失败:' + response.data);
}
});
}
</script>
<?php
}
public function process_after_sales_request() {
check_ajax_referer('fcp_after_sales', 'security');
$order_id = intval($_POST['order_id']);
$components = isset($_POST['components']) ? $_POST['components'] : array();
$reason = sanitize_text_field($_POST['reason']);
$notes = sanitize_textarea_field($_POST['notes']);
// 创建售后记录
$this->create_after_sales_record($order_id, $components, $reason, $notes);
// 发送通知邮件
$this->send_after_sales_notification($order_id, $components, $reason);
wp_send_json_success('售后申请已处理');
}
private function create_after_sales_record($order_id, $components, $reason, $notes) {
global $wpdb;
$table_name = $wpdb->prefix . 'fcp_after_sales';
$data = array(
'order_id' => $order_id,
'components' => json_encode($components),
'reason' => $reason,
'notes' => $notes,
'status' => 'pending',
'created_at' => current_time('mysql'),
'updated_at' => current_time('mysql')
);
$wpdb->insert($table_name, $data);
// 在订单备注中添加记录
$order = wc_get_order($order_id);
$order->add_order_note(
sprintf(__('文创商品售后申请已提交。原因:%s', 'fcp'), $reason)
);
}
private function send_after_sales_notification($order_id, $components, $reason) {
$order = wc_get_order($order_id);
$admin_email = get_option('admin_email');
$subject = sprintf(__('文创商品售后申请 - 订单 #%s', 'fcp'), $order_id);
$message = sprintf(__('订单 #%s 提交了文创商品售后申请。', 'fcp'), $order_id) . "nn";
$message .= __('退货原因:', 'fcp') . $reason . "nn";
$message .= __('涉及组件:', 'fcp') . "n";
foreach ($components as $component_key) {
list($item_id, $index) = explode('_', $component_key);
$item = $order->get_item($item_id);
$components_detail = $item->get_meta('_fcp_components_detail');
if (isset($components_detail[$index])) {
$component = $components_detail[$index];
$message .= sprintf('- %s × %d', $component['name'], $component['quantity']) . "n";
}
}
$message .= "n" . __('请登录后台处理此售后申请。', 'fcp');
wp_mail($admin_email, $subject, $message);
}
}
// 初始化订单管理
new FCP_Order_Management();
六、售后管理数据库与界面
创建售后管理数据库表
// 在插件激活时创建售后管理表
private function create_after_sales_table() {
global $wpdb;
$charset_collate = $wpdb->get_charset_collate();
$table_name = $wpdb->prefix . 'fcp_after_sales';
$sql = "CREATE TABLE IF NOT EXISTS $table_name (
id mediumint(9) NOT NULL AUTO_INCREMENT,
order_id bigint(20) NOT NULL,
components text NOT NULL,
reason varchar(255) NOT NULL,
notes text,
status varchar(50) DEFAULT 'pending',
admin_notes text,
processed_by bigint(20),
processed_at datetime,
created_at datetime DEFAULT CURRENT_TIMESTAMP,
updated_at datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (id),
KEY order_id (order_id),
KEY status (status)
) $charset_collate;";
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
dbDelta($sql);
}
// 更新activate方法
public function activate() {
$this->create_database_tables();
$this->create_after_sales_table(); // 添加这行
update_option('fcp_version', '1.0.0');
set_transient('fcp_activation_notice', true, 5);
}
售后管理后台界面
// 添加售后管理菜单
public function add_after_sales_menu() {
add_submenu_page(
'fcp-dashboard',
'售后管理',
'售后管理',
'manage_options',
'fcp-after-sales',
array($this, 'render_after_sales_page')
);
}
public function render_after_sales_page() {
global $wpdb;
$table_name = $wpdb->prefix . 'fcp_after_sales';
// 处理状态更新
if (isset($_POST['action']) && $_POST['action'] == 'update_status') {
$this->update_after_sales_status();
}
// 获取售后记录
$records = $wpdb->get_results("
SELECT a.*, o.post_title as order_title
FROM $table_name a
LEFT JOIN {$wpdb->posts} o ON a.order_id = o.ID
ORDER BY a.created_at DESC
");
echo '<div class="wrap">';
echo '<h1>文创商品售后管理</h1>';
if ($records) {
echo '<table class="wp-list-table widefat fixed striped">';
echo '<thead>';
echo '<tr>';
echo '<th>订单号</th>';
echo '<th>退货原因</th>';
echo '<th>涉及组件</th>';
echo '<th>状态</th>';
echo '<th>提交时间</th>';
echo '<th>操作</th>';
echo '</tr>';
echo '</thead>';
echo '<tbody>';
foreach ($records as $record) {
echo '<tr>';
echo '<td><a href="' . get_edit_post_link($record->order_id) . '">#' . $record->order_id . '</a></td>';
echo '<td>' . esc_html($record->reason) . '</td>';
echo '<td>';
$components = json_decode($record->components, true);
