文章目录[隐藏]
在WordPress中构建敏捷供应链系统的技术教程
概述:为什么选择WordPress构建供应链系统?
传统观念认为WordPress仅适用于博客和简单网站,但现代WordPress已发展成为功能强大的内容管理系统和应用程序框架。通过其丰富的插件体系、REST API和自定义开发能力,WordPress完全可以胜任敏捷供应链系统的构建。本教程将指导您利用WordPress核心功能、自定义文章类型和现代Web技术,创建一个灵活、可扩展的供应链管理系统。
系统架构设计
技术栈选择
- 核心平台:WordPress 5.8+
- 数据库:MySQL 5.7+ 或 MariaDB 10.3+
- 前端框架:React/Vue.js(可选,用于复杂交互)
- API:WordPress REST API + 自定义端点
- 缓存:Redis或Memcached(处理高并发)
数据模型设计
供应链系统通常需要以下核心实体:供应商、产品、库存、订单、物流信息。我们将通过自定义文章类型和自定义字段来实现这些数据模型。
核心功能实现
1. 创建自定义文章类型
<?php
/**
* 供应链系统自定义文章类型注册
* 文件位置:wp-content/plugins/supply-chain-system/supply-chain.php
*/
// 防止直接访问
if (!defined('ABSPATH')) {
exit;
}
/**
* 注册供应链相关自定义文章类型
*/
function scs_register_custom_post_types() {
// 1. 供应商自定义文章类型
$supplier_labels = array(
'name' => '供应商',
'singular_name' => '供应商',
'menu_name' => '供应商管理',
'add_new' => '添加供应商',
'add_new_item' => '添加新供应商',
'edit_item' => '编辑供应商',
'new_item' => '新供应商',
'view_item' => '查看供应商',
'search_items' => '搜索供应商',
'not_found' => '未找到供应商',
'not_found_in_trash' => '回收站中无供应商'
);
$supplier_args = array(
'labels' => $supplier_labels,
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'show_in_menu' => true,
'query_var' => true,
'rewrite' => array('slug' => 'supplier'),
'capability_type' => 'post',
'has_archive' => true,
'hierarchical' => false,
'menu_position' => 5,
'menu_icon' => 'dashicons-truck',
'supports' => array('title', 'editor', 'thumbnail'),
'show_in_rest' => true, // 启用REST API支持
);
register_post_type('supplier', $supplier_args);
// 2. 产品自定义文章类型
$product_labels = array(
'name' => '产品',
'singular_name' => '产品',
'menu_name' => '产品管理',
'add_new' => '添加产品',
'add_new_item' => '添加新产品',
'edit_item' => '编辑产品',
'new_item' => '新产品',
'view_item' => '查看产品',
'search_items' => '搜索产品',
'not_found' => '未找到产品',
'not_found_in_trash' => '回收站中无产品'
);
$product_args = array(
'labels' => $product_labels,
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'show_in_menu' => true,
'query_var' => true,
'rewrite' => array('slug' => 'product'),
'capability_type' => 'post',
'has_archive' => true,
'hierarchical' => false,
'menu_position' => 6,
'menu_icon' => 'dashicons-products',
'supports' => array('title', 'editor', 'thumbnail', 'excerpt'),
'show_in_rest' => true,
);
register_post_type('product', $product_args);
// 3. 订单自定义文章类型
$order_labels = array(
'name' => '订单',
'singular_name' => '订单',
'menu_name' => '订单管理',
'add_new' => '创建订单',
'add_new_item' => '创建新订单',
'edit_item' => '编辑订单',
'new_item' => '新订单',
'view_item' => '查看订单',
'search_items' => '搜索订单',
'not_found' => '未找到订单',
'not_found_in_trash' => '回收站中无订单'
);
$order_args = array(
'labels' => $order_labels,
'public' => false, // 订单不公开访问
'publicly_queryable' => false,
'show_ui' => true,
'show_in_menu' => true,
'query_var' => true,
'rewrite' => false,
'capability_type' => 'post',
'has_archive' => false,
'hierarchical' => false,
'menu_position' => 7,
'menu_icon' => 'dashicons-cart',
'supports' => array('title'),
'show_in_rest' => true,
);
register_post_type('order', $order_args);
}
add_action('init', 'scs_register_custom_post_types');
?>
2. 添加自定义字段(元数据)
<?php
/**
* 为供应链系统添加自定义字段
* 使用WordPress元数据API存储额外信息
*/
/**
* 为产品添加库存字段
*/
function scs_add_product_inventory_meta_box() {
add_meta_box(
'scs_product_inventory', // 元框ID
'库存信息', // 标题
'scs_product_inventory_callback', // 回调函数
'product', // 文章类型
'side', // 位置
'high' // 优先级
);
}
add_action('add_meta_boxes', 'scs_add_product_inventory_meta_box');
/**
* 库存元框内容
*/
function scs_product_inventory_callback($post) {
// 添加安全验证
wp_nonce_field('scs_save_inventory_data', 'scs_inventory_meta_box_nonce');
// 获取现有库存值
$current_stock = get_post_meta($post->ID, '_scs_product_stock', true);
$min_stock = get_post_meta($post->ID, '_scs_product_min_stock', true);
$sku = get_post_meta($post->ID, '_scs_product_sku', true);
// 设置默认值
$current_stock = $current_stock ? $current_stock : 0;
$min_stock = $min_stock ? $min_stock : 10;
$sku = $sku ? $sku : 'SKU-' . $post->ID;
// 输出表单字段
echo '<p><label for="scs_product_sku">产品SKU:</label>';
echo '<input type="text" id="scs_product_sku" name="scs_product_sku" value="' . esc_attr($sku) . '" size="25" /></p>';
echo '<p><label for="scs_product_stock">当前库存:</label>';
echo '<input type="number" id="scs_product_stock" name="scs_product_stock" value="' . esc_attr($current_stock) . '" min="0" /></p>';
echo '<p><label for="scs_product_min_stock">最低库存:</label>';
echo '<input type="number" id="scs_product_min_stock" name="scs_product_min_stock" value="' . esc_attr($min_stock) . '" min="0" /></p>';
// 显示库存状态
if ($current_stock < $min_stock) {
echo '<p style="color: red;">⚠️ 库存不足,需要补货</p>';
} else {
echo '<p style="color: green;">✓ 库存充足</p>';
}
}
/**
* 保存库存数据
*/
function scs_save_inventory_data($post_id) {
// 检查安全验证
if (!isset($_POST['scs_inventory_meta_box_nonce'])) {
return;
}
if (!wp_verify_nonce($_POST['scs_inventory_meta_box_nonce'], 'scs_save_inventory_data')) {
return;
}
// 检查自动保存
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
return;
}
// 检查用户权限
if (!current_user_can('edit_post', $post_id)) {
return;
}
// 保存SKU
if (isset($_POST['scs_product_sku'])) {
update_post_meta(
$post_id,
'_scs_product_sku',
sanitize_text_field($_POST['scs_product_sku'])
);
}
// 保存当前库存
if (isset($_POST['scs_product_stock'])) {
update_post_meta(
$post_id,
'_scs_product_stock',
intval($_POST['scs_product_stock'])
);
}
// 保存最低库存
if (isset($_POST['scs_product_min_stock'])) {
update_post_meta(
$post_id,
'_scs_product_min_stock',
intval($_POST['scs_product_min_stock'])
);
}
}
add_action('save_post_product', 'scs_save_inventory_data');
?>
3. 创建REST API端点
<?php
/**
* 供应链系统自定义REST API端点
* 提供库存查询、订单创建等API功能
*/
/**
* 注册自定义REST API路由
*/
function scs_register_rest_routes() {
// 库存查询API
register_rest_route('scs/v1', '/inventory/(?P<id>d+)', array(
'methods' => 'GET',
'callback' => 'scs_get_inventory_api',
'permission_callback' => '__return_true', // 实际应用中应添加权限验证
'args' => array(
'id' => array(
'validate_callback' => function($param, $request, $key) {
return is_numeric($param);
}
),
),
));
// 创建订单API
register_rest_route('scs/v1', '/order/create', array(
'methods' => 'POST',
'callback' => 'scs_create_order_api',
'permission_callback' => function() {
// 实际应用中应检查API密钥或用户权限
return current_user_can('edit_posts');
},
));
// 库存预警API
register_rest_route('scs/v1', '/inventory/alerts', array(
'methods' => 'GET',
'callback' => 'scs_get_inventory_alerts',
'permission_callback' => function() {
return current_user_can('edit_posts');
},
));
}
add_action('rest_api_init', 'scs_register_rest_routes');
/**
* 获取产品库存API
*/
function scs_get_inventory_api($request) {
$product_id = $request['id'];
// 验证产品是否存在
$product = get_post($product_id);
if (!$product || $product->post_type !== 'product') {
return new WP_Error('not_found', '产品不存在', array('status' => 404));
}
// 获取库存信息
$stock = get_post_meta($product_id, '_scs_product_stock', true);
$min_stock = get_post_meta($product_id, '_scs_product_min_stock', true);
$sku = get_post_meta($product_id, '_scs_product_sku', true);
// 构建响应数据
$response = array(
'product_id' => $product_id,
'product_name' => $product->post_title,
'sku' => $sku ? $sku : 'N/A',
'current_stock' => $stock ? intval($stock) : 0,
'minimum_stock' => $min_stock ? intval($min_stock) : 0,
'status' => ($stock < $min_stock) ? 'low' : 'adequate',
'last_updated' => $product->post_modified
);
return rest_ensure_response($response);
}
/**
* 创建订单API
*/
function scs_create_order_api($request) {
$parameters = $request->get_json_params();
// 验证必要参数
if (empty($parameters['customer_name']) || empty($parameters['items'])) {
return new WP_Error('missing_params', '缺少必要参数', array('status' => 400));
}
// 创建订单
$order_data = array(
'post_title' => '订单 - ' . $parameters['customer_name'] . ' - ' . current_time('mysql'),
'post_type' => 'order',
'post_status' => 'publish',
'post_author' => get_current_user_id(),
);
$order_id = wp_insert_post($order_data);
if (is_wp_error($order_id)) {
return $order_id;
}
// 保存订单元数据
update_post_meta($order_id, '_scs_customer_name', sanitize_text_field($parameters['customer_name']));
update_post_meta($order_id, '_scs_customer_email', sanitize_email($parameters['customer_email']));
update_post_meta($order_id, '_scs_order_items', json_encode($parameters['items']));
update_post_meta($order_id, '_scs_order_status', 'processing');
update_post_meta($order_id, '_scs_order_date', current_time('mysql'));
// 更新产品库存
foreach ($parameters['items'] as $item) {
if (isset($item['product_id']) && isset($item['quantity'])) {
$current_stock = get_post_meta($item['product_id'], '_scs_product_stock', true);
$new_stock = max(0, intval($current_stock) - intval($item['quantity']));
update_post_meta($item['product_id'], '_scs_product_stock', $new_stock);
}
}
// 构建响应
$response = array(
'success' => true,
'order_id' => $order_id,
'message' => '订单创建成功',
'order_data' => array(
'id' => $order_id,
'customer' => $parameters['customer_name'],
'status' => 'processing',
'items' => $parameters['items']
)
);
return rest_ensure_response($response);
}
/**
* 获取库存预警列表
*/
function scs_get_inventory_alerts() {
// 查询所有产品
$args = array(
'post_type' => 'product',
'posts_per_page' => -1,
'post_status' => 'publish'
);
$products = get_posts($args);
$alerts = array();
foreach ($products as $product) {
$current_stock = get_post_meta($product->ID, '_scs_product_stock', true);
$min_stock = get_post_meta($product->ID, '_scs_product_min_stock', true);
if (intval($current_stock) < intval($min_stock)) {
$alerts[] = array(
'product_id' => $product->ID,
'product_name' => $product->post_title,
'current_stock' => intval($current_stock),
'minimum_stock' => intval($min_stock),
'difference' => intval($min_stock) - intval($current_stock)
);
}
}
return rest_ensure_response(array(
'count' => count($alerts),
'alerts' => $alerts,
'timestamp' => current_time('mysql')
));
}
?>
前端界面集成
库存仪表板短代码
<?php
/**
* 库存仪表板短代码
* 使用方式:[inventory_dashboard]
*/
function scs_inventory_dashboard_shortcode($atts) {
// 获取低库存产品
$args = array(
'post_type' => 'product',
'posts_per_page' => 10,
'meta_query' => array(
array(
'key' => '_scs_product_stock',
'value' => 10, // 假设10为低库存阈值
'compare' => '<',
'type' => 'NUMERIC'
)
)
);
$low_stock_products = get_posts($args);
// 构建HTML输出
$output = '<div class="scs-inventory-dashboard">';
$output .= '<h3>库存预警仪表板</h3>';
if (empty($low_stock_products)) {
$output .= '<p>所有产品库存充足</p>';
} else {
$output .= '<table class="scs-inventory-table">';
$output .= '<thead><tr><th>产品名称</th><th>当前库存</th><th>状态</th><th>操作</th></tr></thead>';
$output .= '<tbody>';
foreach ($low_stock_products as $product) {
$current_stock = get_post_meta($product->ID, '_scs_product_stock', true);
$min_stock = get_post_meta($product->ID, '_scs_product_min_stock', true);
$sku = get_post_meta($product->ID, '_scs_product_sku', true);
$status_class = ($current_stock == 0) ? 'out-of-stock' : 'low-stock';
$status_text = ($current_stock == 0) ? '缺货' : '库存不足';
$output .= '<tr class="' . $status_class . '">';
$output .= '<td>' . esc_html($product->post_title) . ' (' . esc_html($sku) . ')</td>';
$output .= '<td>' . intval($current_stock) . ' / ' . intval($min_stock) . '</td>';
$output .= '<td><span class="status-badge">' . $status_text . '</span></td>';
$output .= '<td><a href="' . get_edit_post_link($product->ID) . '" class="button">管理库存</a></td>';
$output .= '</tr>';
}
$output .= '</tbody></table>';
}
// 添加库存统计
$total_products = wp_count_posts('product');
$output .= '<div class="scs-stats">';
$output .= '<h4>库存统计</h4>';
$output .= '<p>总产品数: ' . $total_products->publish . '</p>';
$output .= '<p>低库存产品: ' . count($low_stock_products) . '</p>';
$output .= '</div>';
$output .= '</div>';
// 添加CSS样式
$output .= '<style>
.scs-inventory-dashboard {
background: #f8f9fa;
padding: 20px;
border-radius: 8px;
margin: 20px 0;
}
.scs-inventory-table {
width: 100%;
border-collapse: collapse;
margin: 15px 0;
}
.scs-inventory-table th,
.scs-inventory-table td {
padding: 12px;
border: 1px solid #dee2e6;
text-align: left;
}
.scs-inventory-table th {
background-color: #343a40;
color: white;
}
.scs-inventory-table tr.low-stock {
background-color: #fff3cd;
}
.scs-inventory-table tr.out-of-stock {
background-color: #f8d7da;
}
.status-badge {
padding: 4px 8px;
border-radius: 4px;
font-size: 12px;
font-weight: bold;
}
.scs-inventory-table tr.low-stock .status-badge {
background-color: #ffc107;
color: #856404;
}
.scs-inventory-table tr.out-of-stock .status-badge {
background-color: #dc3545;
color: white;
}
.scs-stats {
background: white;
padding: 15px;
border-radius: 6px;
margin-top: 20px;
}
</style>';
return $output;
}
add_shortcode('inventory_dashboard', 'scs_inventory_dashboard_shortcode');
?>
高级功能:实时库存监控
WebSocket实时更新(使用Pusher或Socket.io)
<?php
/**
* 实时库存监控系统
* 使用WebSocket技术实现库存实时更新
*/
// 注意:实际部署时需要安装WebSocket服务器
// 这里使用Pusher作为示例
class SCS_RealTime_Inventory {
private $pusher;
public function __construct() {
// 初始化Pusher(需要安装Pusher PHP SDK)
// $this->pusher = new PusherPusher(
// 'your_app_key',
// 'your_app_secret',
// 'your_app_id',
// ['cluster' => 'your_app_cluster']
// );
add_action('save_post_product', array($this, 'notify_inventory_change'), 10, 3);
add_action('scs_inventory_updated', array($this, 'broadcast_inventory_update'), 10, 2);
}
/**
* 库存变化时发送通知
*/
public function notify_inventory_change($post_id, $post, $update) {
if ($post->post_type !== 'product' || wp_is_post_revision($post_id)) {
return;
}
// 获取库存变化
$old_stock = get_post_meta($post_id, '_scs_product_stock', true);
$new_stock = isset($_POST['scs_product_stock']) ? intval($_POST['scs_product_stock']) : $old_stock;
if ($old_stock != $new_stock) {
// 触发库存更新事件
do_action('scs_inventory_updated', $post_id, array(
'old_stock' => $old_stock,
'new_stock' => $new_stock,
'product_name' => $post->post_title,
'timestamp' => current_time('mysql')
));
}
}
/**
* 广播库存更新
*/
public function broadcast_inventory_update($product_id, $data) {
// 实际使用中,这里会调用WebSocket服务
// $this->pusher->trigger('inventory-channel', 'stock-updated', array(
// 'product_id' => $product_id,
// 'data' => $data
// ));
// 临时方案:使用WordPress Transients记录最近更新
$recent_updates = get_transient('scs_recent_inventory_updates');
if (!$recent_updates) {
$recent_updates = array();
}
$recent_updates[] = array(
'product_id' => $product_id,
'data' => $data,
'time' => time()
);
// 只保留最近10条记录
if (count($recent_updates) > 10) {
array_shift($recent_updates);
}
set_transient('scs_recent_inventory_updates', $recent_updates, 3600);
// 发送邮件通知(如果库存低于阈值)
$min_stock = get_post_meta($product_id, '_scs_product_min_stock', true);
if ($data['new_stock'] < $min_stock) {
$this->send_low_stock_alert($product_id, $data);
}
}
/**
* 发送低库存警报邮件
*/
private function send_low_stock_alert($product_id, $data) {
$admin_email = get_option('admin_email');
$product_name = $data['product_name'];
$current_stock = $data['new_stock'];
$min_stock = get_post_meta($product_id, '_scs_product_min_stock', true);
$subject = '库存警报: ' . $product_name . ' 库存不足';
$message = "
<html>
<body>
<h2>库存警报</h2>
<p>产品 <strong>{$product_name}</strong> 库存低于最低阈值:</p>
<ul>
<li>当前库存: {$current_stock}</li>
<li>最低库存要求: {$min_stock}</li>
<li>差额: " . ($min_stock - $current_stock) . "</li>
</ul>
<p>请及时补货。</p>
<p><a href='" . admin_url('post.php?post=' . $product_id . '&action=edit') . "'>点击这里管理库存</a></p>
</body>
</html>
";
$headers = array('Content-Type: text/html; charset=UTF-8');
wp_mail($admin_email, $subject, $message, $headers);
}
/**
* 获取最近库存更新记录
*/
public static function get_recent_updates() {
return get_transient('scs_recent_inventory_updates') ?: array();
}
}
// 初始化实时监控系统
new SCS_RealTime_Inventory();
?>
数据报表与分析
<?php
/**
* 供应链数据报表系统
* 生成库存、销售等数据分析报表
*/
class SCS_Reports {
/**
* 生成库存周转率报表
*/
public static function generate_inventory_turnover_report($start_date, $end_date) {
global $wpdb;
$query = $wpdb->prepare("
SELECT
p.ID as product_id,
p.post_title as product_name,
pm_sku.meta_value as sku,
pm_stock.meta_value as current_stock,
COUNT(o.ID) as sales_count,
SUM(om_qty.meta_value) as total_sold
FROM {$wpdb->posts} p
LEFT JOIN {$wpdb->postmeta} pm_sku ON p.ID = pm_sku.post_id AND pm_sku.meta_key = '_scs_product_sku'
LEFT JOIN {$wpdb->postmeta} pm_stock ON p.ID = pm_stock.post_id AND pm_stock.meta_key = '_scs_product_stock'
LEFT JOIN {$wpdb->posts} o ON o.post_type = 'order'
LEFT JOIN {$wpdb->postmeta} om_qty ON o.ID = om_qty.post_id AND om_qty.meta_key = '_scs_order_quantity'
WHERE p.post_type = 'product'
AND p.post_status = 'publish'
AND o.post_date BETWEEN %s AND %s
GROUP BY p.ID
ORDER BY total_sold DESC
", $start_date, $end_date);
$results = $wpdb->get_results($query);
// 计算库存周转率
$report_data = array();
foreach ($results as $row) {
$average_inventory = $row->current_stock;
$cost_of_goods_sold = $row->total_sold * 100; // 假设每个产品成本100元
if ($average_inventory > 0) {
$turnover_ratio = $cost_of_goods_sold / $average_inventory;
} else {
$turnover_ratio = 0;
}
$report_data[] = array(
'product_id' => $row->product_id,
'product_name' => $row->product_name,
'sku' => $row->sku,
'current_stock' => $row->current_stock,
'total_sold' => $row->total_sold,
'turnover_ratio' => round($turnover_ratio, 2),
'status' => self::get_turnover_status($turnover_ratio)
);
}
return $report_data;
}
/**
* 获取周转率状态
*/
private static function get_turnover_status($ratio) {
if ($ratio >= 5) {
return array('label' => '优秀', 'color' => 'success');
} elseif ($ratio >= 2) {
return array('label' => '良好', 'color' => 'info');
} elseif ($ratio >= 1) {
return array('label' => '一般', 'color' => 'warning');
} else {
return array('label' => '较差', 'color' => 'danger');
}
}
/**
* 生成供应商绩效报表
*/
public static function generate_supplier_performance_report() {
global $wpdb;
$query = "
SELECT
s.ID as supplier_id,
s.post_title as supplier_name,
COUNT(DISTINCT p.ID) as products_supplied,
AVG(pm_rating.meta_value) as avg_rating,
SUM(CASE WHEN pm_stock.meta_value < pm_min.meta_value THEN 1 ELSE 0 END) as low_stock_items
FROM {$wpdb->posts} s
LEFT JOIN {$wpdb->posts} p ON p.post_type = 'product'
LEFT JOIN {$wpdb->postmeta} pm_supplier ON p.ID = pm_supplier.post_id AND pm_supplier.meta_key = '_scs_product_supplier'
LEFT JOIN {$wpdb->postmeta} pm_rating ON s.ID = pm_rating.post_id AND pm_rating.meta_key = '_scs_supplier_rating'
LEFT JOIN {$wpdb->postmeta} pm_stock ON p.ID = pm_stock.post_id AND pm_stock.meta_key = '_scs_product_stock'
LEFT JOIN {$wpdb->postmeta} pm_min ON p.ID = pm_min.post_id AND pm_min.meta_key = '_scs_product_min_stock'
WHERE s.post_type = 'supplier'
AND s.post_status = 'publish'
AND pm_supplier.meta_value = s.ID
GROUP BY s.ID
ORDER BY avg_rating DESC
";
return $wpdb->get_results($query);
}
/**
* 生成报表HTML
*/
public static function render_report_dashboard() {
$inventory_report = self::generate_inventory_turnover_report(
date('Y-m-01'), // 本月第一天
date('Y-m-t') // 本月最后一天
);
$supplier_report = self::generate_supplier_performance_report();
ob_start();
?>
<div class="scs-reports-dashboard">
<h2>供应链分析报表</h2>
<div class="report-section">
<h3>库存周转率分析</h3>
<table class="wp-list-table widefat fixed striped">
<thead>
<tr>
<th>产品名称</th>
<th>SKU</th>
<th>当前库存</th>
<th>本月销量</th>
<th>周转率</th>
<th>状态</th>
</tr>
</thead>
<tbody>
<?php foreach ($inventory_report as $item): ?>
<tr>
<td><?php echo esc_html($item['product_name']); ?></td>
<td><?php echo esc_html($item['sku']); ?></td>
<td><?php echo intval($item['current_stock']); ?></td>
<td><?php echo intval($item['total_sold']); ?></td>
<td><?php echo $item['turnover_ratio']; ?></td>
<td>
<span class="status-badge status-<?php echo $item['status']['color']; ?>">
<?php echo $item['status']['label']; ?>
</span>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
<div class="report-section">
<h3>供应商绩效评估</h3>
<table class="wp-list-table widefat fixed striped">
<thead>
<tr>
<th>供应商</th>
<th>供应产品数</th>
<th>平均评分</th>
<th>低库存产品数</th>
<th>绩效等级</th>
</tr>
</thead>
<tbody>
<?php foreach ($supplier_report as $supplier):
$performance_score = ($supplier->avg_rating * 20) - ($supplier->low_stock_items * 5);
$grade = self::calculate_supplier_grade($performance_score);
?>
<tr>
<td><?php echo esc_html($supplier->supplier_name); ?></td>
<td><?php echo intval($supplier->products_supplied); ?></td>
<td><?php echo round($supplier->avg_rating, 1); ?>/5</td>
<td><?php echo intval($supplier->low_stock_items); ?></td>
<td>
<span class="grade-badge grade-<?php echo strtolower($grade); ?>">
<?php echo $grade; ?>
</span>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
</div>
<style>
.scs-reports-dashboard {
padding: 20px;
}
.report-section {
margin-bottom: 40px;
background: white;
padding: 20px;
border-radius: 8px;
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
}
.status-badge, .grade-badge {
padding: 4px 8px;
border-radius: 4px;
font-size: 12px;
font-weight: bold;
}
.status-success, .grade-a { background: #d4edda; color: #155724; }
.status-info, .grade-b { background: #d1ecf1; color: #0c5460; }
.status-warning, .grade-c { background: #fff3cd; color: #856404; }
.status-danger, .grade-d { background: #f8d7da; color: #721c24; }
</style>
<?php
return ob_get_clean();
}
/**
* 计算供应商等级
*/
private static function calculate_supplier_grade($score) {
if ($score >= 80) return 'A';
if ($score >= 60) return 'B';
if ($score >= 40) return 'C';
return 'D';
}
}
// 添加报表页面到WordPress后台
add_action('admin_menu', function() {
add_menu_page(
'供应链报表',
'供应链报表',
