文章目录[隐藏]
WordPress文创IP柔性衍生品开发管理插件教程
引言:文创IP与柔性衍生品的数字化管理需求
在数字文创产业蓬勃发展的今天,文创IP的衍生品开发已成为重要的变现渠道。然而,传统衍生品开发管理面临诸多挑战:产品信息分散、库存管理混乱、授权流程复杂、销售渠道不统一等。针对这些问题,我们开发了一款专为WordPress设计的文创IP柔性衍生品开发管理插件,帮助文创团队实现全流程数字化管理。
本教程将详细介绍如何安装、配置和使用这款插件,包含完整的代码示例和最佳实践。
插件安装与基础配置
1.1 插件安装方法
首先,您需要将插件文件上传到WordPress的插件目录。以下是手动安装的完整代码示例:
<?php
/**
* Plugin Name: 文创IP衍生品管理器
* Plugin URI: https://yourwebsite.com/cultural-ip-manager
* Description: 专为文创IP设计的柔性衍生品开发管理插件
* Version: 1.0.0
* Author: 文创科技团队
* License: GPL v2 or later
* Text Domain: cultural-ip-manager
*/
// 防止直接访问
if (!defined('ABSPATH')) {
exit;
}
// 定义插件常量
define('CIP_VERSION', '1.0.0');
define('CIP_PLUGIN_DIR', plugin_dir_path(__FILE__));
define('CIP_PLUGIN_URL', plugin_dir_url(__FILE__));
// 初始化插件
add_action('plugins_loaded', 'cip_initialize_plugin');
function cip_initialize_plugin() {
// 检查必要依赖
if (!class_exists('WooCommerce')) {
add_action('admin_notices', 'cip_woocommerce_missing_notice');
return;
}
// 加载核心类
require_once CIP_PLUGIN_DIR . 'includes/class-core.php';
require_once CIP_PLUGIN_DIR . 'includes/class-database.php';
require_once CIP_PLUGIN_DIR . 'includes/class-products.php';
// 初始化核心功能
$cip_core = new CIP_Core();
$cip_core->init();
}
function cip_woocommerce_missing_notice() {
?>
<div class="notice notice-error">
<p><?php _e('文创IP衍生品管理器需要WooCommerce插件才能正常工作。', 'cultural-ip-manager'); ?></p>
</div>
<?php
}
// 激活插件时创建数据库表
register_activation_hook(__FILE__, 'cip_create_database_tables');
function cip_create_database_tables() {
global $wpdb;
$charset_collate = $wpdb->get_charset_collate();
// 衍生品产品表
$table_products = $wpdb->prefix . 'cip_products';
$sql_products = "CREATE TABLE IF NOT EXISTS $table_products (
id mediumint(9) NOT NULL AUTO_INCREMENT,
ip_id mediumint(9) NOT NULL,
product_name varchar(200) NOT NULL,
product_type varchar(100) NOT NULL,
design_status varchar(50) DEFAULT 'draft',
production_status varchar(50) DEFAULT 'pending',
stock_quantity int(11) DEFAULT 0,
woocommerce_id bigint(20) DEFAULT NULL,
created_at datetime DEFAULT CURRENT_TIMESTAMP,
updated_at datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (id),
KEY ip_id (ip_id),
KEY woocommerce_id (woocommerce_id)
) $charset_collate;";
// IP授权管理表
$table_licenses = $wpdb->prefix . 'cip_licenses';
$sql_licenses = "CREATE TABLE IF NOT EXISTS $table_licenses (
id mediumint(9) NOT NULL AUTO_INCREMENT,
ip_id mediumint(9) NOT NULL,
licensee_name varchar(200) NOT NULL,
license_type varchar(100) NOT NULL,
start_date date NOT NULL,
end_date date NOT NULL,
royalty_rate decimal(5,2) DEFAULT 0.00,
status varchar(50) DEFAULT 'active',
contract_file varchar(500) DEFAULT NULL,
created_at datetime DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (id),
KEY ip_id (ip_id),
KEY status (status)
) $charset_collate;";
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
dbDelta($sql_products);
dbDelta($sql_licenses);
}
?>
1.2 基础设置与配置
安装完成后,进入WordPress后台,您会看到新增的"文创IP管理"菜单。以下是基础配置页面的实现代码:
<?php
/**
* 插件设置页面
*/
class CIP_Settings {
public function __construct() {
add_action('admin_menu', array($this, 'add_settings_page'));
add_action('admin_init', array($this, 'register_settings'));
}
public function add_settings_page() {
add_menu_page(
'文创IP管理设置',
'文创IP管理',
'manage_options',
'cip-settings',
array($this, 'render_settings_page'),
'dashicons-palmtree',
30
);
// 添加子菜单
add_submenu_page(
'cip-settings',
'IP库管理',
'IP库管理',
'manage_options',
'edit.php?post_type=cip_ip'
);
add_submenu_page(
'cip-settings',
'衍生品管理',
'衍生品管理',
'manage_options',
'cip-products',
array($this, 'render_products_page')
);
}
public function register_settings() {
// 注册设置选项
register_setting('cip_settings_group', 'cip_default_royalty_rate');
register_setting('cip_settings_group', 'cip_auto_sync_woocommerce');
register_setting('cip_settings_group', 'cip_low_stock_threshold');
// 添加设置字段
add_settings_section(
'cip_general_section',
'常规设置',
array($this, 'render_general_section'),
'cip-settings'
);
add_settings_field(
'cip_default_royalty_rate',
'默认版权费率 (%)',
array($this, 'render_royalty_rate_field'),
'cip-settings',
'cip_general_section'
);
add_settings_field(
'cip_auto_sync_woocommerce',
'自动同步到WooCommerce',
array($this, 'render_sync_field'),
'cip-settings',
'cip_general_section'
);
}
public function render_settings_page() {
?>
<div class="wrap">
<h1>文创IP管理设置</h1>
<form method="post" action="options.php">
<?php
settings_fields('cip_settings_group');
do_settings_sections('cip-settings');
submit_button();
?>
</form>
</div>
<?php
}
public function render_general_section() {
echo '<p>配置文创IP管理插件的基本参数</p>';
}
public function render_royalty_rate_field() {
$rate = get_option('cip_default_royalty_rate', '10');
echo '<input type="number" name="cip_default_royalty_rate" value="' . esc_attr($rate) . '" min="0" max="100" step="0.1" /> %';
}
public function render_sync_field() {
$sync = get_option('cip_auto_sync_woocommerce', '1');
echo '<input type="checkbox" name="cip_auto_sync_woocommerce" value="1" ' . checked(1, $sync, false) . ' /> 启用自动同步';
}
public function render_products_page() {
// 衍生品管理页面逻辑
echo '<div class="wrap"><h1>衍生品管理</h1>';
$this->render_products_table();
echo '</div>';
}
private function render_products_table() {
global $wpdb;
$table_name = $wpdb->prefix . 'cip_products';
$products = $wpdb->get_results("SELECT * FROM $table_name ORDER BY created_at DESC");
if (empty($products)) {
echo '<p>暂无衍生品记录</p>';
return;
}
echo '<table class="wp-list-table widefat fixed striped">';
echo '<thead><tr>
<th>ID</th>
<th>产品名称</th>
<th>IP关联</th>
<th>设计状态</th>
<th>生产状态</th>
<th>库存</th>
<th>操作</th>
</tr></thead>';
echo '<tbody>';
foreach ($products as $product) {
echo '<tr>';
echo '<td>' . $product->id . '</td>';
echo '<td>' . esc_html($product->product_name) . '</td>';
echo '<td>' . $this->get_ip_name($product->ip_id) . '</td>';
echo '<td><span class="cip-status cip-status-' . $product->design_status . '">' . $this->get_status_label($product->design_status) . '</span></td>';
echo '<td><span class="cip-status cip-status-' . $product->production_status . '">' . $this->get_status_label($product->production_status) . '</span></td>';
echo '<td>' . $product->stock_quantity . '</td>';
echo '<td>
<a href="' . admin_url('admin.php?page=cip-edit-product&id=' . $product->id) . '">编辑</a> |
<a href="#" class="cip-delete-product" data-id="' . $product->id . '">删除</a>
</td>';
echo '</tr>';
}
echo '</tbody></table>';
}
private function get_ip_name($ip_id) {
// 获取IP名称的逻辑
return '示例IP';
}
private function get_status_label($status) {
$labels = array(
'draft' => '草稿',
'designing' => '设计中',
'reviewing' => '审核中',
'approved' => '已批准',
'producing' => '生产中',
'completed' => '已完成'
);
return isset($labels[$status]) ? $labels[$status] : $status;
}
}
// 初始化设置类
new CIP_Settings();
?>
IP库管理与衍生品开发流程
2.1 创建和管理文创IP
文创IP是衍生品开发的核心。我们的插件提供了完整的IP管理功能:
<?php
/**
* IP自定义文章类型
*/
class CIP_IP_Post_Type {
public function __construct() {
add_action('init', array($this, 'register_ip_post_type'));
add_action('add_meta_boxes', array($this, 'add_ip_meta_boxes'));
add_action('save_post_cip_ip', array($this, 'save_ip_meta'));
}
public function register_ip_post_type() {
$labels = array(
'name' => '文创IP',
'singular_name' => '文创IP',
'menu_name' => '文创IP库',
'add_new' => '添加新IP',
'add_new_item' => '添加新文创IP',
'edit_item' => '编辑文创IP',
'new_item' => '新文创IP',
'view_item' => '查看文创IP',
'search_items' => '搜索文创IP',
'not_found' => '未找到文创IP',
'not_found_in_trash' => '回收站中无文创IP'
);
$args = array(
'labels' => $labels,
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'show_in_menu' => false, // 已在设置中添加
'query_var' => true,
'rewrite' => array('slug' => 'cultural-ip'),
'capability_type' => 'post',
'has_archive' => true,
'hierarchical' => false,
'menu_position' => null,
'supports' => array('title', 'editor', 'thumbnail', 'excerpt'),
'taxonomies' => array('cip_category', 'cip_tag')
);
register_post_type('cip_ip', $args);
// 注册IP分类
register_taxonomy(
'cip_category',
'cip_ip',
array(
'label' => 'IP分类',
'hierarchical' => true,
'show_admin_column' => true
)
);
// 注册IP标签
register_taxonomy(
'cip_tag',
'cip_ip',
array(
'label' => 'IP标签',
'hierarchical' => false,
'show_admin_column' => true
)
);
}
public function add_ip_meta_boxes() {
add_meta_box(
'cip_ip_details',
'IP详细信息',
array($this, 'render_ip_meta_box'),
'cip_ip',
'normal',
'high'
);
add_meta_box(
'cip_ip_derivatives',
'关联衍生品',
array($this, 'render_derivatives_meta_box'),
'cip_ip',
'side',
'default'
);
}
public function render_ip_meta_box($post) {
// 添加安全验证
wp_nonce_field('cip_ip_meta_box', 'cip_ip_meta_box_nonce');
// 获取现有值
$ip_author = get_post_meta($post->ID, '_cip_ip_author', true);
$creation_date = get_post_meta($post->ID, '_cip_creation_date', true);
$copyright_holder = get_post_meta($post->ID, '_cip_copyright_holder', true);
$ip_status = get_post_meta($post->ID, '_cip_ip_status', true);
// 输出表单字段
echo '<table class="form-table">';
echo '<tr>';
echo '<th><label for="cip_ip_author">IP作者/创作团队</label></th>';
echo '<td><input type="text" id="cip_ip_author" name="cip_ip_author" value="' . esc_attr($ip_author) . '" class="regular-text" /></td>';
echo '</tr>';
echo '<tr>';
echo '<th><label for="cip_creation_date">创作日期</label></th>';
echo '<td><input type="date" id="cip_creation_date" name="cip_creation_date" value="' . esc_attr($creation_date) . '" /></td>';
echo '</tr>';
echo '<tr>';
echo '<th><label for="cip_copyright_holder">版权持有方</label></th>';
echo '<td><input type="text" id="cip_copyright_holder" name="cip_copyright_holder" value="' . esc_attr($copyright_holder) . '" class="regular-text" /></td>';
echo '</tr>';
echo '<tr>';
echo '<th><label for="cip_ip_status">IP状态</label></th>';
echo '<td>';
echo '<select id="cip_ip_status" name="cip_ip_status">';
$status_options = array(
'active' => '活跃',
'developing' => '开发中',
'archived' => '已归档',
'licensed' => '已授权'
);
foreach ($status_options as $value => $label) {
echo '<option value="' . $value . '" ' . selected($ip_status, $value, false) . '>' . $label . '</option>';
}
echo '</select>';
echo '</td>';
echo '</tr>';
echo '</table>';
}
public function render_derivatives_meta_box($post) {
global $wpdb;
$table_name = $wpdb->prefix . 'cip_products';
$products = $wpdb->get_results($wpdb->prepare(
"SELECT * FROM $table_name WHERE ip_id = %d ORDER BY created_at DESC",
$post->ID
));
if (empty($products)) {
echo '<p>暂无关联衍生品</p>';
} else {
echo '<ul>';
foreach ($products as $product) {
echo '<li><a href="' . admin_url('admin.php?page=cip-edit-product&id=' . $product->id) . '">' . esc_html($product->product_name) . '</a> (' . $product->product_type . ')</li>';
}
echo '</ul>';
}
echo '<p><a href="' . admin_url('admin.php?page=cip-add-product&ip_id=' . $post->ID) . '" class="button button-primary">添加新衍生品</a></p>';
}
public function save_ip_meta($post_id) {
// 安全检查
if (!isset($_POST['cip_ip_meta_box_nonce']) ||
!wp_verify_nonce($_POST['cip_ip_meta_box_nonce'], 'cip_ip_meta_box')) {
return;
}
// 检查自动保存
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
return;
}
// 检查用户权限
if (!current_user_can('edit_post', $post_id)) {
return;
}
// 保存字段数据
$fields = array(
'cip_ip_author',
'cip_creation_date',
'cip_copyright_holder',
'cip_ip_status'
);
foreach ($fields as $field) {
if (isset($_POST[$field])) {
update_post_meta($post_id, '_' . $field, sanitize_text_field($_POST[$field]));
}
}
}
}
// 初始化IP文章类型
new CIP_IP_Post_Type();
?>
2.2 衍生品开发工作流管理
衍生品开发通常包含多个阶段,我们的插件提供了完整的工作流管理:
<?php
/**
* 衍生品开发工作流管理
*/
class CIP_Product_Workflow {
private $workflow_stages = array(
'concept' => array(
'name' => '概念设计',
'next' => 'design',
'color' => '#f39c12'
),
'design' => array(
'name' => '设计开发',
'next' => 'review',
'color' => '#3498db'
),
'review' => array(
'name' => '设计审核',
'next' => 'sample',
'color' => '#9b59b6'
),
'sample' => array(
'name' => '样品制作',
'next' => 'production',
'color' => '#e74c3c'
),
'production' => array(
'name' => '批量生产',
'next' => 'completed',
'color' => '#2ecc71'
),
'completed' => array(
'name' => '已完成',
'next' => null,
'color' => '#95a5a6'
)
);
public function __construct() {
add_action('admin_enqueue_scripts', array($this, 'enqueue_workflow_scripts'));
add_action('wp_ajax_cip_update_workflow', array($this, 'ajax_update_workflow'));
}
public function enqueue_workflow_scripts($hook) {
if (strpos($hook, 'cip-edit-product') !== false || strpos($hook, 'cip-add-product') !== false) {
wp_enqueue_script('cip-workflow', CIP_PLUGIN_URL . 'assets/js/workflow.js', array('jquery'), CIP_VERSION, true);
wp_enqueue_style('cip-workflow', CIP_PLUGIN_URL . 'assets/css/workflow.css', array(), CIP_VERSION);
wp_localize_script('cip-workflow', 'cip_workflow_data', array(
'ajax_url' => admin_url('admin-ajax.php'),
'nonce' => wp_create_nonce('cip_workflow_nonce'),
'stages' => $this->workflow_stages
));
}
}
/**
* 渲染工作流进度条
*/
public function render_workflow_progress($product_id) {
global $wpdb;
$table_name = $wpdb->prefix . 'cip_products';
$product = $wpdb->get_row($wpdb->prepare(
"SELECT design_status, production_status FROM $table_name WHERE id = %d",
$product_id
));
if (!$product) return '';
$current_stage = $product->design_status;
$output = '<div class="cip-workflow-progress">';
$output .= '<div class="cip-workflow-stages">';
$i = 0;
foreach ($this->workflow_stages as $stage_key => $stage) {
$is_active = $stage_key === $current_stage;
$is_passed = array_search($stage_key, array_keys($this->workflow_stages)) <
array_search($current_stage, array_keys($this->workflow_stages));
$class = 'cip-workflow-stage';
if ($is_active) $class .= ' active';
if ($is_passed) $class .= ' passed';
$output .= '<div class="' . $class . '" data-stage="' . $stage_key . '">';
$output .= '<div class="stage-circle" style="background-color: ' . $stage['color'] . '">' . ($i + 1) . '</div>';
$output .= '<div class="stage-label">' . $stage['name'] . '</div>';
$output .= '</div>';
// 添加连接线(除了最后一个)
if ($stage_key !== 'completed') {
$output .= '<div class="stage-connector' . ($is_passed ? ' passed' : '') . '"></div>';
}
$i++;
}
$output .= '</div>';
// 添加控制按钮
if ($current_stage !== 'completed') {
$next_stage = $this->workflow_stages[$current_stage]['next'];
$output .= '<div class="workflow-controls">';
$output .= '<button class="button button-primary cip-advance-stage"
data-product-id="' . $product_id . '"
data-next-stage="' . $next_stage . '">
进入' . $this->workflow_stages[$next_stage]['name'] . '阶段
</button>';
$output .= '</div>';
}
$output .= '</div>';
return $output;
}
/**
* AJAX更新工作流状态
*/
public function ajax_update_workflow() {
// 验证nonce
if (!wp_verify_nonce($_POST['nonce'], 'cip_workflow_nonce')) {
wp_die('安全验证失败');
}
// 验证权限
if (!current_user_can('manage_options')) {
wp_die('权限不足');
}
$product_id = intval($_POST['product_id']);
$new_stage = sanitize_text_field($_POST['new_stage']);
global $wpdb;
$table_name = $wpdb->prefix . 'cip_products';
// 更新数据库
$result = $wpdb->update(
$table_name,
array('design_status' => $new_stage),
array('id' => $product_id),
array('%s'),
array('%d')
);
if ($result !== false) {
// 记录状态变更日志
$this->log_status_change($product_id, $new_stage);
// 如果是生产阶段,同步创建WooCommerce产品
if ($new_stage === 'production') {
$this->sync_to_woocommerce($product_id);
}
wp_send_json_success(array(
'message' => '工作流状态已更新',
'new_stage' => $new_stage,
'stage_name' => $this->workflow_stages[$new_stage]['name']
));
} else {
wp_send_json_error(array(
'message' => '更新失败'
));
}
}
/**
* 记录状态变更日志
*/
private function log_status_change($product_id, $new_status) {
global $wpdb;
$log_table = $wpdb->prefix . 'cip_workflow_logs';
$wpdb->insert(
$log_table,
array(
'product_id' => $product_id,
'old_status' => $this->get_current_status($product_id),
'new_status' => $new_status,
'changed_by' => get_current_user_id(),
'changed_at' => current_time('mysql')
),
array('%d', '%s', '%s', '%d', '%s')
);
}
/**
* 同步到WooCommerce
*/
private function sync_to_woocommerce($product_id) {
global $wpdb;
$table_name = $wpdb->prefix . 'cip_products';
$product = $wpdb->get_row($wpdb->prepare(
"SELECT * FROM $table_name WHERE id = %d",
$product_id
));
if (!$product) return;
// 检查是否已同步
if ($product->woocommerce_id) {
return; // 已存在,不重复创建
}
// 创建WooCommerce产品
$wc_product = new WC_Product_Simple();
$wc_product->set_name($product->product_name);
$wc_product->set_status('publish');
$wc_product->set_catalog_visibility('visible');
$wc_product->set_description($this->get_product_description($product_id));
$wc_product->set_short_description('文创IP衍生品:' . $product->product_name);
$wc_product->set_regular_price($this->calculate_price($product_id));
$wc_product->set_manage_stock(true);
$wc_product->set_stock_quantity($product->stock_quantity);
// 设置产品分类
$category_id = $this->get_or_create_wc_category($product->product_type);
if ($category_id) {
$wc_product->set_category_ids(array($category_id));
}
// 保存产品
$wc_product_id = $wc_product->save();
// 更新关联ID
$wpdb->update(
$table_name,
array('woocommerce_id' => $wc_product_id),
array('id' => $product_id),
array('%d'),
array('%d')
);
return $wc_product_id;
}
/**
* 获取产品描述
*/
private function get_product_description($product_id) {
global $wpdb;
$table_name = $wpdb->prefix . 'cip_products';
$product = $wpdb->get_row($wpdb->prepare(
"SELECT p.*, ip.post_title as ip_name
FROM $table_name p
LEFT JOIN {$wpdb->posts} ip ON p.ip_id = ip.ID
WHERE p.id = %d",
$product_id
));
$description = '<h3>' . $product->product_name . '</h3>';
$description .= '<p>所属IP:' . $product->ip_name . '</p>';
$description .= '<p>产品类型:' . $product->product_type . '</p>';
$description .= '<p>设计理念:此衍生品基于' . $product->ip_name . '的核心元素设计...</p>';
return $description;
}
/**
* 计算产品价格
*/
private function calculate_price($product_id) {
// 基础价格计算逻辑
$base_price = 99.00; // 基础价格
// 根据产品类型调整
global $wpdb;
$table_name = $wpdb->prefix . 'cip_products';
$product = $wpdb->get_row($wpdb->prepare(
"SELECT product_type FROM $table_name WHERE id = %d",
$product_id
));
$type_multipliers = array(
'figure' => 1.5, // 手办
'clothing' => 1.2, // 服饰
'stationery' => 1.0,// 文具
'digital' => 0.8, // 数字产品
'other' => 1.0 // 其他
);
$multiplier = isset($type_multipliers[$product->product_type])
? $type_multipliers[$product->product_type]
: 1.0;
return $base_price * $multiplier;
}
/**
* 获取或创建WooCommerce分类
*/
private function get_or_create_wc_category($category_name) {
$term = term_exists($category_name, 'product_cat');
if (!$term) {
$term = wp_insert_term(
$category_name,
'product_cat',
array(
'description' => '文创衍生品:' . $category_name,
'slug' => 'cultural-' . sanitize_title($category_name)
)
);
}
return is_array($term) ? $term['term_id'] : $term;
}
/**
* 获取当前状态
*/
private function get_current_status($product_id) {
global $wpdb;
$table_name = $wpdb->prefix . 'cip_products';
$product = $wpdb->get_row($wpdb->prepare(
"SELECT design_status FROM $table_name WHERE id = %d",
$product_id
));
return $product ? $product->design_status : '';
}
}
// 初始化工作流管理
new CIP_Product_Workflow();
?>
库存管理与销售集成
3.1 智能库存管理系统
<?php
/**
* 智能库存管理
*/
class CIP_Inventory_Manager {
public function __construct() {
add_action('admin_menu', array($this, 'add_inventory_page'));
add_action('wp_ajax_cip_update_inventory', array($this, 'ajax_update_inventory'));
add_action('woocommerce_reduce_order_stock', array($this, 'sync_order_stock'));
}
public function add_inventory_page() {
add_submenu_page(
'cip-settings',
'库存管理',
'库存管理',
'manage_options',
'cip-inventory',
array($this, 'render_inventory_page')
);
}
public function render_inventory_page() {
global $wpdb;
$products_table = $wpdb->prefix . 'cip_products';
$ip_table = $wpdb->posts;
$query = "
SELECT p.*, ip.post_title as ip_name
FROM $products_table p
LEFT JOIN $ip_table ip ON p.ip_id = ip.ID
WHERE p.design_status = 'completed'
ORDER BY p.stock_quantity ASC, p.updated_at DESC
";
$products = $wpdb->get_results($query);
echo '<div class="wrap">';
echo '<h1>库存管理</h1>';
// 库存概览
$this->render_inventory_overview($products);
// 库存列表
$this->render_inventory_list($products);
echo '</div>';
}
private function render_inventory_overview($products) {
$total_products = count($products);
$low_stock = 0;
$out_of_stock = 0;
$healthy_stock = 0;
$low_stock_threshold = get_option('cip_low_stock_threshold', 10);
foreach ($products as $product) {
if ($product->stock_quantity <= 0) {
$out_of_stock++;
} elseif ($product->stock_quantity <= $low_stock_threshold) {
$low_stock++;
} else {
$healthy_stock++;
}
}
echo '<div class="cip-inventory-overview">';
echo '<div class="overview-item total">';
echo '<h3>' . $total_products . '</h3>';
echo '<p>总产品数</p>';
echo '</div>';
echo '<div class="overview-item healthy">';
echo '<h3>' . $healthy_stock . '</h3>';
echo '<p>库存充足</p>';
echo '</div>';
echo '<div class="overview-item low">';
echo '<h3>' . $low_stock . '</h3>';
echo '<p>库存预警</p>';
echo '</div>';
echo '<div class="overview-item out">';
echo '<h3>' . $out_of_stock . '</h3>';
echo '<p>缺货产品</p>';
echo '</div>';
echo '</div>';
}
private function render_inventory_list($products) {
echo '<table class="wp-list-table widefat fixed striped">';
echo '<thead>
<tr>
<th>产品ID</th>
<th>产品名称</th>
<th>所属IP</th>
<th>当前库存</th>
<th>预警状态</th>
<th>最近更新</th>
<th>操作</th>
</tr>
</thead>';
echo '<tbody>';
foreach ($products as $product) {
$stock_status = $this->get_stock_status($product->stock_quantity);
$status_class = 'stock-' . $stock_status['level'];
echo '<tr>';
echo '<td>' . $product->id . '</td>';
echo '<td>' . esc_html($product->product_name) . '</td>';
echo '<td>' . esc_html($product->ip_name) . '</td>';
echo '<td class="' . $status_class . '">' . $product->stock_quantity . '</td>';
echo '<td><span class="stock-indicator ' . $status_class . '">' . $stock_status['label'] . '</span></td>';
echo '<td>' . human_time_diff(strtotime($product->updated_at), current_time('timestamp')) . '前</td>';
echo '<td>
<button class="button button-small cip-adjust-stock"
data-product-id="' . $product->id . '"
data-current-stock="' . $product->stock_quantity . '">
调整库存
</button>
</td>';
echo '</tr>';
}
echo '</tbody></table>';
}
private function get_stock_status($quantity) {
$low_threshold = get_option('cip_low_stock_threshold', 10);
if ($quantity <= 0) {
return array('level' => 'out', 'label' => '缺货', 'color' => '#e74c3c');
} elseif ($quantity <= $low_threshold) {
return array('level' => 'low', 'label' => '库存低', 'color' => '#f39c12');
} else {
return array('level' => 'healthy', 'label' => '充足', 'color' => '#2ecc71');
}
}
public function ajax_update_inventory() {
// 验证nonce和权限
check_ajax_referer('cip_inventory_nonce', 'nonce');
if (!current_user_can('manage_options')) {
