首页 / 教程文章 / 开发支持小批量生产的WordPress定制插件教程

开发支持小批量生产的WordPress定制插件教程

开发支持小批量生产的WordPress定制插件教程

概述

在当今数字化时代,许多中小型企业需要进行小批量生产的内容管理和展示。WordPress作为全球最流行的内容管理系统,通过定制插件可以轻松实现小批量生产管理功能。本教程将指导您开发一个完整的WordPress插件,用于管理小批量生产订单、产品和客户信息。

插件规划与结构设计

在开始编码之前,我们需要规划插件的基本功能:

  1. 产品管理:添加、编辑、删除小批量产品
  2. 订单管理:处理客户订单
  3. 客户管理:存储客户信息
  4. 生产状态跟踪:监控生产进度
  5. 数据报表:生成简单的生产报表

插件目录结构如下:

small-batch-production/
├── small-batch-production.php
├── includes/
│   ├── class-database.php
│   ├── class-products.php
│   ├── class-orders.php
│   └── class-admin.php
├── admin/
│   └── admin-pages.php
├── public/
│   └── public-display.php
└── assets/
    ├── css/
    └── js/

主插件文件开发

首先创建主插件文件,这是插件的入口点:

<?php
/**
 * 插件名称: Small Batch Production Manager
 * 插件URI: https://example.com/small-batch-production
 * 描述: 用于管理小批量生产订单和产品的WordPress插件
 * 版本: 1.0.0
 * 作者: Your Name
 * 作者URI: https://example.com
 * 许可证: GPL v2或更高版本
 * 文本域: small-batch-production
 */

// 防止直接访问
if (!defined('ABSPATH')) {
    exit;
}

// 定义插件常量
define('SBP_VERSION', '1.0.0');
define('SBP_PLUGIN_DIR', plugin_dir_path(__FILE__));
define('SBP_PLUGIN_URL', plugin_dir_url(__FILE__));

// 插件激活时执行的操作
register_activation_hook(__FILE__, 'sbp_activate_plugin');
function sbp_activate_plugin() {
    // 检查WordPress版本
    if (version_compare(get_bloginfo('version'), '5.0', '<')) {
        wp_die(__('此插件需要WordPress 5.0或更高版本', 'small-batch-production'));
    }
    
    // 创建数据库表
    require_once SBP_PLUGIN_DIR . 'includes/class-database.php';
    SBP_Database::create_tables();
    
    // 设置默认选项
    update_option('sbp_settings', array(
        'items_per_page' => 20,
        'currency' => 'USD',
        'notify_admin' => true
    ));
}

// 插件停用时执行的操作
register_deactivation_hook(__FILE__, 'sbp_deactivate_plugin');
function sbp_deactivate_plugin() {
    // 清理临时数据
    delete_transient('sbp_daily_stats');
}

// 初始化插件
add_action('plugins_loaded', 'sbp_init_plugin');
function sbp_init_plugin() {
    // 加载文本域用于国际化
    load_plugin_textdomain('small-batch-production', false, dirname(plugin_basename(__FILE__)) . '/languages');
    
    // 根据上下文加载不同的功能
    if (is_admin()) {
        require_once SBP_PLUGIN_DIR . 'includes/class-admin.php';
        require_once SBP_PLUGIN_DIR . 'admin/admin-pages.php';
        new SBP_Admin();
    } else {
        require_once SBP_PLUGIN_DIR . 'public/public-display.php';
    }
    
    // 加载公共功能
    require_once SBP_PLUGIN_DIR . 'includes/class-database.php';
    require_once SBP_PLUGIN_DIR . 'includes/class-products.php';
    require_once SBP_PLUGIN_DIR . 'includes/class-orders.php';
}

// 添加插件设置链接
add_filter('plugin_action_links_' . plugin_basename(__FILE__), 'sbp_add_settings_link');
function sbp_add_settings_link($links) {
    $settings_link = '<a href="admin.php?page=sbp-settings">' . __('设置', 'small-batch-production') . '</a>';
    array_unshift($links, $settings_link);
    return $links;
}
?>

数据库管理类

接下来创建数据库管理类,用于处理所有数据库操作:

<?php
/**
 * 数据库管理类
 * 处理插件的所有数据库操作
 */

class SBP_Database {
    
    /**
     * 创建插件所需的数据库表
     */
    public static function create_tables() {
        global $wpdb;
        
        $charset_collate = $wpdb->get_charset_collate();
        
        // 产品表
        $products_table = $wpdb->prefix . 'sbp_products';
        $products_sql = "CREATE TABLE IF NOT EXISTS $products_table (
            id mediumint(9) NOT NULL AUTO_INCREMENT,
            name varchar(200) NOT NULL,
            description text,
            sku varchar(100) UNIQUE,
            price decimal(10,2) NOT NULL,
            production_time int DEFAULT 7,
            min_batch_size int DEFAULT 1,
            max_batch_size int DEFAULT 100,
            status varchar(20) DEFAULT 'active',
            created_at datetime DEFAULT CURRENT_TIMESTAMP,
            updated_at datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
            PRIMARY KEY (id)
        ) $charset_collate;";
        
        // 订单表
        $orders_table = $wpdb->prefix . 'sbp_orders';
        $orders_sql = "CREATE TABLE IF NOT EXISTS $orders_table (
            id mediumint(9) NOT NULL AUTO_INCREMENT,
            order_number varchar(50) UNIQUE NOT NULL,
            customer_id mediumint(9),
            customer_name varchar(200),
            customer_email varchar(200),
            product_id mediumint(9) NOT NULL,
            quantity int NOT NULL,
            unit_price decimal(10,2) NOT NULL,
            total_price decimal(10,2) NOT NULL,
            status varchar(50) DEFAULT 'pending',
            production_progress int DEFAULT 0,
            notes text,
            due_date date,
            created_at datetime DEFAULT CURRENT_TIMESTAMP,
            updated_at datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
            PRIMARY KEY (id),
            FOREIGN KEY (product_id) REFERENCES $products_table(id) ON DELETE CASCADE
        ) $charset_collate;";
        
        // 客户表(简化版)
        $customers_table = $wpdb->prefix . 'sbp_customers';
        $customers_sql = "CREATE TABLE IF NOT EXISTS $customers_table (
            id mediumint(9) NOT NULL AUTO_INCREMENT,
            name varchar(200) NOT NULL,
            email varchar(200) UNIQUE NOT NULL,
            company varchar(200),
            phone varchar(50),
            address text,
            total_orders int DEFAULT 0,
            created_at datetime DEFAULT CURRENT_TIMESTAMP,
            PRIMARY KEY (id)
        ) $charset_collate;";
        
        require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
        dbDelta($products_sql);
        dbDelta($orders_sql);
        dbDelta($customers_sql);
        
        // 添加示例数据(仅用于演示)
        self::add_sample_data();
    }
    
    /**
     * 添加示例数据
     */
    private static function add_sample_data() {
        global $wpdb;
        
        $products_table = $wpdb->prefix . 'sbp_products';
        
        // 检查是否已有数据
        $count = $wpdb->get_var("SELECT COUNT(*) FROM $products_table");
        
        if ($count == 0) {
            $sample_products = array(
                array(
                    'name' => '定制T恤',
                    'description' => '高品质棉质T恤,可定制图案',
                    'sku' => 'CT-001',
                    'price' => 25.99,
                    'production_time' => 10,
                    'min_batch_size' => 10,
                    'max_batch_size' => 500
                ),
                array(
                    'name' => '促销水杯',
                    'description' => '不锈钢保温杯,可激光雕刻Logo',
                    'sku' => 'MB-002',
                    'price' => 15.50,
                    'production_time' => 7,
                    'min_batch_size' => 20,
                    'max_batch_size' => 1000
                )
            );
            
            foreach ($sample_products as $product) {
                $wpdb->insert($products_table, $product);
            }
        }
    }
    
    /**
     * 获取所有产品
     * @param int $per_page 每页显示数量
     * @param int $page_number 页码
     * @return array 产品数组
     */
    public static function get_products($per_page = 20, $page_number = 1) {
        global $wpdb;
        
        $table_name = $wpdb->prefix . 'sbp_products';
        $offset = ($page_number - 1) * $per_page;
        
        $sql = "SELECT * FROM $table_name WHERE status != 'deleted'";
        
        // 搜索功能
        if (isset($_REQUEST['s']) && !empty($_REQUEST['s'])) {
            $search = sanitize_text_field($_REQUEST['s']);
            $sql .= $wpdb->prepare(" AND (name LIKE '%%%s%%' OR sku LIKE '%%%s%%' OR description LIKE '%%%s%%')", $search, $search, $search);
        }
        
        // 排序
        $orderby = isset($_REQUEST['orderby']) ? sanitize_sql_orderby($_REQUEST['orderby']) : 'created_at';
        $order = isset($_REQUEST['order']) && $_REQUEST['order'] == 'asc' ? 'ASC' : 'DESC';
        $sql .= " ORDER BY $orderby $order";
        
        // 分页
        $sql .= $wpdb->prepare(" LIMIT %d OFFSET %d", $per_page, $offset);
        
        return $wpdb->get_results($sql, ARRAY_A);
    }
    
    /**
     * 获取产品总数
     * @return int 产品总数
     */
    public static function get_products_count() {
        global $wpdb;
        
        $table_name = $wpdb->prefix . 'sbp_products';
        $sql = "SELECT COUNT(*) FROM $table_name WHERE status != 'deleted'";
        
        // 搜索功能
        if (isset($_REQUEST['s']) && !empty($_REQUEST['s'])) {
            $search = sanitize_text_field($_REQUEST['s']);
            $sql .= $wpdb->prepare(" AND (name LIKE '%%%s%%' OR sku LIKE '%%%s%%' OR description LIKE '%%%s%%')", $search, $search, $search);
        }
        
        return $wpdb->get_var($sql);
    }
}
?>

产品管理类

创建产品管理类,处理产品相关业务逻辑:

<?php
/**
 * 产品管理类
 * 处理产品相关的业务逻辑
 */

class SBP_Products {
    
    /**
     * 添加新产品
     * @param array $product_data 产品数据数组
     * @return int|false 成功返回产品ID,失败返回false
     */
    public static function add_product($product_data) {
        global $wpdb;
        
        $table_name = $wpdb->prefix . 'sbp_products';
        
        // 数据验证
        $errors = self::validate_product_data($product_data);
        if (!empty($errors)) {
            return false;
        }
        
        // 准备插入数据
        $data = array(
            'name' => sanitize_text_field($product_data['name']),
            'description' => sanitize_textarea_field($product_data['description']),
            'sku' => sanitize_text_field($product_data['sku']),
            'price' => floatval($product_data['price']),
            'production_time' => intval($product_data['production_time']),
            'min_batch_size' => intval($product_data['min_batch_size']),
            'max_batch_size' => intval($product_data['max_batch_size']),
            'status' => 'active'
        );
        
        // 插入数据库
        $result = $wpdb->insert($table_name, $data);
        
        if ($result) {
            return $wpdb->insert_id;
        }
        
        return false;
    }
    
    /**
     * 验证产品数据
     * @param array $data 产品数据
     * @return array 错误信息数组
     */
    private static function validate_product_data($data) {
        $errors = array();
        
        // 检查必填字段
        if (empty($data['name'])) {
            $errors[] = __('产品名称不能为空', 'small-batch-production');
        }
        
        if (empty($data['sku'])) {
            $errors[] = __('产品SKU不能为空', 'small-batch-production');
        }
        
        // 检查价格
        if (!is_numeric($data['price']) || floatval($data['price']) <= 0) {
            $errors[] = __('产品价格必须为正数', 'small-batch-production');
        }
        
        // 检查批次大小
        $min_batch = intval($data['min_batch_size']);
        $max_batch = intval($data['max_batch_size']);
        
        if ($min_batch <= 0) {
            $errors[] = __('最小批次大小必须大于0', 'small-batch-production');
        }
        
        if ($max_batch < $min_batch) {
            $errors[] = __('最大批次大小不能小于最小批次大小', 'small-batch-production');
        }
        
        // 检查生产时间
        $production_time = intval($data['production_time']);
        if ($production_time <= 0) {
            $errors[] = __('生产时间必须为正数', 'small-batch-production');
        }
        
        return $errors;
    }
    
    /**
     * 更新产品生产状态
     * @param int $product_id 产品ID
     * @param string $status 新状态
     * @return bool 是否成功
     */
    public static function update_product_status($product_id, $status) {
        global $wpdb;
        
        $table_name = $wpdb->prefix . 'sbp_products';
        $allowed_statuses = array('active', 'inactive', 'discontinued');
        
        if (!in_array($status, $allowed_statuses)) {
            return false;
        }
        
        $result = $wpdb->update(
            $table_name,
            array('status' => $status),
            array('id' => intval($product_id)),
            array('%s'),
            array('%d')
        );
        
        return $result !== false;
    }
    
    /**
     * 获取可用于生产的产品列表
     * @return array 产品列表
     */
    public static function get_available_products() {
        global $wpdb;
        
        $table_name = $wpdb->prefix . 'sbp_products';
        $sql = "SELECT id, name, sku, price, min_batch_size, max_batch_size 
                FROM $table_name 
                WHERE status = 'active' 
                ORDER BY name ASC";
        
        return $wpdb->get_results($sql, ARRAY_A);
    }
    
    /**
     * 计算小批量生产的总成本
     * @param int $product_id 产品ID
     * @param int $quantity 数量
     * @return array 成本详情
     */
    public static function calculate_production_cost($product_id, $quantity) {
        global $wpdb;
        
        $table_name = $wpdb->prefix . 'sbp_products';
        $product = $wpdb->get_row($wpdb->prepare(
            "SELECT price, min_batch_size, max_batch_size FROM $table_name WHERE id = %d",
            $product_id
        ), ARRAY_A);
        
        if (!$product) {
            return false;
        }
        
        // 检查数量是否在允许范围内
        if ($quantity < $product['min_batch_size'] || $quantity > $product['max_batch_size']) {
            return false;
        }
        
        // 计算成本(这里可以添加更复杂的成本计算逻辑)
        $unit_price = floatval($product['price']);
        $total_cost = $unit_price * $quantity;
        
        // 小批量折扣逻辑(示例)
        $discount = 0;
        if ($quantity >= 50 && $quantity < 100) {
            $discount = 0.05; // 5%折扣
        } elseif ($quantity >= 100) {
            $discount = 0.10; // 10%折扣
        }
        
        $discount_amount = $total_cost * $discount;
        $final_cost = $total_cost - $discount_amount;
        
        return array(
            'unit_price' => $unit_price,
            'quantity' => $quantity,
            'subtotal' => $total_cost,
            'discount_rate' => $discount * 100,
            'discount_amount' => $discount_amount,
            'total_cost' => $final_cost
        );
    }
}
?>

管理界面实现

创建管理页面类,用于在WordPress后台显示管理界面:

<?php
/**
 * 管理页面类
 * 处理WordPress后台管理界面
 */

class SBP_Admin_Pages {
    
    /**
     * 初始化管理菜单
     */
    public static function init_menu() {
        // 主菜单
        add_menu_page(
            __('小批量生产管理', 'small-batch-production'),
            __('生产管理', 'small-batch-production'),
            'manage_options',
            'sbp-dashboard',
            array(__CLASS__, 'render_dashboard_page'),
            'dashicons-products',
            30
        );
        
        // 子菜单
        add_submenu_page(
            'sbp-dashboard',
            __('产品管理', 'small-batch-production'),
            __('产品', 'small-batch-production'),
            'manage_options',
            'sbp-products',
            array(__CLASS__, 'render_products_page')
        );
        
        add_submenu_page(
            'sbp-dashboard',

接上文,继续生成管理界面实现代码:

<?php
/**
 * 管理页面类
 * 处理WordPress后台管理界面
 */

class SBP_Admin_Pages {
    
    /**
     * 初始化管理菜单
     */
    public static function init_menu() {
        // 主菜单
        add_menu_page(
            __('小批量生产管理', 'small-batch-production'),
            __('生产管理', 'small-batch-production'),
            'manage_options',
            'sbp-dashboard',
            array(__CLASS__, 'render_dashboard_page'),
            'dashicons-products',
            30
        );
        
        // 子菜单
        add_submenu_page(
            'sbp-dashboard',
            __('产品管理', 'small-batch-production'),
            __('产品', 'small-batch-production'),
            'manage_options',
            'sbp-products',
            array(__CLASS__, 'render_products_page')
        );
        
        add_submenu_page(
            'sbp-dashboard',
            __('订单管理', 'small-batch-production'),
            __('订单', 'small-batch-production'),
            'manage_options',
            'sbp-orders',
            array(__CLASS__, 'render_orders_page')
        );
        
        add_submenu_page(
            'sbp-dashboard',
            __('客户管理', 'small-batch-production'),
            __('客户', 'small-batch-production'),
            'manage_options',
            'sbp-customers',
            array(__CLASS__, 'render_customers_page')
        );
        
        add_submenu_page(
            'sbp-dashboard',
            __('生产报表', 'small-batch-production'),
            __('报表', 'small-batch-production'),
            'manage_options',
            'sbp-reports',
            array(__CLASS__, 'render_reports_page')
        );
        
        add_submenu_page(
            'sbp-dashboard',
            __('插件设置', 'small-batch-production'),
            __('设置', 'small-batch-production'),
            'manage_options',
            'sbp-settings',
            array(__CLASS__, 'render_settings_page')
        );
    }
    
    /**
     * 渲染仪表板页面
     */
    public static function render_dashboard_page() {
        // 检查用户权限
        if (!current_user_can('manage_options')) {
            wp_die(__('您没有权限访问此页面', 'small-batch-production'));
        }
        
        // 获取统计数据
        global $wpdb;
        $products_table = $wpdb->prefix . 'sbp_products';
        $orders_table = $wpdb->prefix . 'sbp_orders';
        
        $total_products = $wpdb->get_var("SELECT COUNT(*) FROM $products_table WHERE status = 'active'");
        $total_orders = $wpdb->get_var("SELECT COUNT(*) FROM $orders_table");
        $pending_orders = $wpdb->get_var("SELECT COUNT(*) FROM $orders_table WHERE status = 'pending'");
        $in_production_orders = $wpdb->get_var("SELECT COUNT(*) FROM $orders_table WHERE status = 'in_production'");
        
        // 获取最近订单
        $recent_orders = $wpdb->get_results(
            "SELECT o.*, p.name as product_name 
             FROM $orders_table o 
             LEFT JOIN $products_table p ON o.product_id = p.id 
             ORDER BY o.created_at DESC 
             LIMIT 5",
            ARRAY_A
        );
        
        ?>
        <div class="wrap sbp-dashboard">
            <h1><?php _e('小批量生产管理仪表板', 'small-batch-production'); ?></h1>
            
            <div class="sbp-stats-container">
                <div class="sbp-stat-card">
                    <h3><?php _e('活跃产品', 'small-batch-production'); ?></h3>
                    <div class="stat-number"><?php echo esc_html($total_products); ?></div>
                </div>
                
                <div class="sbp-stat-card">
                    <h3><?php _e('总订单数', 'small-batch-production'); ?></h3>
                    <div class="stat-number"><?php echo esc_html($total_orders); ?></div>
                </div>
                
                <div class="sbp-stat-card">
                    <h3><?php _e('待处理订单', 'small-batch-production'); ?></h3>
                    <div class="stat-number"><?php echo esc_html($pending_orders); ?></div>
                </div>
                
                <div class="sbp-stat-card">
                    <h3><?php _e('生产中订单', 'small-batch-production'); ?></h3>
                    <div class="stat-number"><?php echo esc_html($in_production_orders); ?></div>
                </div>
            </div>
            
            <div class="sbp-recent-orders">
                <h2><?php _e('最近订单', 'small-batch-production'); ?></h2>
                <table class="wp-list-table widefat fixed striped">
                    <thead>
                        <tr>
                            <th><?php _e('订单号', 'small-batch-production'); ?></th>
                            <th><?php _e('客户', 'small-batch-production'); ?></th>
                            <th><?php _e('产品', 'small-batch-production'); ?></th>
                            <th><?php _e('数量', 'small-batch-production'); ?></th>
                            <th><?php _e('状态', 'small-batch-production'); ?></th>
                            <th><?php _e('下单时间', 'small-batch-production'); ?></th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php if (empty($recent_orders)): ?>
                            <tr>
                                <td colspan="6"><?php _e('暂无订单', 'small-batch-production'); ?></td>
                            </tr>
                        <?php else: ?>
                            <?php foreach ($recent_orders as $order): ?>
                                <tr>
                                    <td><?php echo esc_html($order['order_number']); ?></td>
                                    <td><?php echo esc_html($order['customer_name']); ?></td>
                                    <td><?php echo esc_html($order['product_name']); ?></td>
                                    <td><?php echo esc_html($order['quantity']); ?></td>
                                    <td>
                                        <span class="sbp-status-badge sbp-status-<?php echo esc_attr($order['status']); ?>">
                                            <?php echo self::get_status_label($order['status']); ?>
                                        </span>
                                    </td>
                                    <td><?php echo date_i18n(get_option('date_format'), strtotime($order['created_at'])); ?></td>
                                </tr>
                            <?php endforeach; ?>
                        <?php endif; ?>
                    </tbody>
                </table>
            </div>
            
            <div class="sbp-quick-actions">
                <h2><?php _e('快速操作', 'small-batch-production'); ?></h2>
                <div class="action-buttons">
                    <a href="<?php echo admin_url('admin.php?page=sbp-products&action=add'); ?>" class="button button-primary">
                        <?php _e('添加新产品', 'small-batch-production'); ?>
                    </a>
                    <a href="<?php echo admin_url('admin.php?page=sbp-orders&action=add'); ?>" class="button button-secondary">
                        <?php _e('创建新订单', 'small-batch-production'); ?>
                    </a>
                    <a href="<?php echo admin_url('admin.php?page=sbp-reports'); ?>" class="button button-secondary">
                        <?php _e('查看生产报表', 'small-batch-production'); ?>
                    </a>
                </div>
            </div>
        </div>
        
        <style>
            .sbp-stats-container {
                display: grid;
                grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
                gap: 20px;
                margin: 20px 0;
            }
            
            .sbp-stat-card {
                background: #fff;
                border: 1px solid #ccd0d4;
                border-radius: 4px;
                padding: 20px;
                text-align: center;
                box-shadow: 0 1px 3px rgba(0,0,0,0.1);
            }
            
            .sbp-stat-card h3 {
                margin-top: 0;
                color: #666;
                font-size: 14px;
                text-transform: uppercase;
            }
            
            .sbp-stat-card .stat-number {
                font-size: 32px;
                font-weight: bold;
                color: #0073aa;
            }
            
            .sbp-status-badge {
                display: inline-block;
                padding: 3px 8px;
                border-radius: 3px;
                font-size: 12px;
                font-weight: 600;
            }
            
            .sbp-status-pending {
                background: #f0ad4e;
                color: #fff;
            }
            
            .sbp-status-in_production {
                background: #5bc0de;
                color: #fff;
            }
            
            .sbp-status-completed {
                background: #5cb85c;
                color: #fff;
            }
            
            .sbp-status-cancelled {
                background: #d9534f;
                color: #fff;
            }
            
            .sbp-quick-actions {
                margin-top: 30px;
                background: #fff;
                padding: 20px;
                border: 1px solid #ccd0d4;
                border-radius: 4px;
            }
            
            .action-buttons {
                display: flex;
                gap: 10px;
                margin-top: 15px;
            }
        </style>
        <?php
    }
    
    /**
     * 渲染产品管理页面
     */
    public static function render_products_page() {
        // 检查用户权限
        if (!current_user_can('manage_options')) {
            wp_die(__('您没有权限访问此页面', 'small-batch-production'));
        }
        
        $action = isset($_GET['action']) ? sanitize_text_field($_GET['action']) : 'list';
        
        switch ($action) {
            case 'add':
            case 'edit':
                self::render_product_edit_page($action);
                break;
            case 'delete':
                self::handle_delete_product();
                break;
            default:
                self::render_products_list_page();
                break;
        }
    }
    
    /**
     * 渲染产品列表页面
     */
    private static function render_products_list_page() {
        global $wpdb;
        
        // 处理批量操作
        if (isset($_POST['action']) || isset($_POST['action2'])) {
            $action = isset($_POST['action']) && $_POST['action'] != '-1' ? $_POST['action'] : $_POST['action2'];
            $product_ids = isset($_POST['product_ids']) ? array_map('intval', $_POST['product_ids']) : array();
            
            if (!empty($product_ids) && $action == 'delete') {
                foreach ($product_ids as $product_id) {
                    self::delete_product($product_id);
                }
                echo '<div class="notice notice-success"><p>' . __('产品已删除', 'small-batch-production') . '</p></div>';
            }
        }
        
        // 获取产品数据
        $per_page = 20;
        $current_page = isset($_GET['paged']) ? max(1, intval($_GET['paged'])) : 1;
        $products = SBP_Database::get_products($per_page, $current_page);
        $total_items = SBP_Database::get_products_count();
        
        ?>
        <div class="wrap">
            <h1 class="wp-heading-inline">
                <?php _e('产品管理', 'small-batch-production'); ?>
            </h1>
            <a href="<?php echo admin_url('admin.php?page=sbp-products&action=add'); ?>" class="page-title-action">
                <?php _e('添加新产品', 'small-batch-production'); ?>
            </a>
            
            <hr class="wp-header-end">
            
            <!-- 搜索表单 -->
            <form method="get" style="margin: 20px 0;">
                <input type="hidden" name="page" value="sbp-products">
                <div class="search-box">
                    <label class="screen-reader-text" for="product-search-input">
                        <?php _e('搜索产品', 'small-batch-production'); ?>
                    </label>
                    <input type="search" id="product-search-input" name="s" 
                           value="<?php echo isset($_GET['s']) ? esc_attr($_GET['s']) : ''; ?>" 
                           placeholder="<?php _e('按名称、SKU或描述搜索', 'small-batch-production'); ?>">
                    <input type="submit" id="search-submit" class="button" 
                           value="<?php _e('搜索产品', 'small-batch-production'); ?>">
                </div>
            </form>
            
            <!-- 产品列表表格 -->
            <form method="post">
                <?php wp_nonce_field('sbp_bulk_action', 'sbp_nonce'); ?>
                <div class="tablenav top">
                    <div class="alignleft actions bulkactions">
                        <label for="bulk-action-selector-top" class="screen-reader-text">
                            <?php _e('选择批量操作', 'small-batch-production'); ?>
                        </label>
                        <select name="action" id="bulk-action-selector-top">
                            <option value="-1"><?php _e('批量操作', 'small-batch-production'); ?></option>
                            <option value="delete"><?php _e('删除', 'small-batch-production'); ?></option>
                        </select>
                        <input type="submit" id="doaction" class="button action" 
                               value="<?php _e('应用', 'small-batch-production'); ?>">
                    </div>
                    
                    <!-- 分页 -->
                    <div class="tablenav-pages">
                        <?php
                        $total_pages = ceil($total_items / $per_page);
                        if ($total_pages > 1) {
                            echo paginate_links(array(
                                'base' => add_query_arg('paged', '%#%'),
                                'format' => '',
                                'prev_text' => __('&laquo; 上一页'),
                                'next_text' => __('下一页 &raquo;'),
                                'total' => $total_pages,
                                'current' => $current_page
                            ));
                        }
                        ?>
                    </div>
                </div>
                
                <table class="wp-list-table widefat fixed striped">
                    <thead>
                        <tr>
                            <td id="cb" class="manage-column column-cb check-column">
                                <input type="checkbox" id="cb-select-all-1">
                            </td>
                            <th scope="col" class="manage-column column-name">
                                <?php _e('产品名称', 'small-batch-production'); ?>
                            </th>
                            <th scope="col" class="manage-column column-sku">
                                <?php _e('SKU', 'small-batch-production'); ?>
                            </th>
                            <th scope="col" class="manage-column column-price">
                                <?php _e('价格', 'small-batch-production'); ?>
                            </th>
                            <th scope="col" class="manage-column column-batch-size">
                                <?php _e('批次范围', 'small-batch-production'); ?>
                            </th>
                            <th scope="col" class="manage-column column-production-time">
                                <?php _e('生产时间(天)', 'small-batch-production'); ?>
                            </th>
                            <th scope="col" class="manage-column column-status">
                                <?php _e('状态', 'small-batch-production'); ?>
                            </th>
                            <th scope="col" class="manage-column column-actions">
                                <?php _e('操作', 'small-batch-production'); ?>
                            </th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php if (empty($products)): ?>
                            <tr>
                                <td colspan="8"><?php _e('暂无产品', 'small-batch-production'); ?></td>
                            </tr>
                        <?php else: ?>
                            <?php foreach ($products as $product): ?>
                                <tr>
                                    <th scope="row" class="check-column">
                                        <input type="checkbox" name="product_ids[]" value="<?php echo esc_attr($product['id']); ?>">
                                    </th>
                                    <td class="column-name">
                                        <strong>
                                            <a href="<?php echo admin_url('admin.php?page=sbp-products&action=edit&id=' . $product['id']); ?>">
                                                <?php echo esc_html($product['name']); ?>
                                            </a>
                                        </strong>
                                        <div class="row-actions">
                                            <span class="edit">
                                                <a href="<?php echo admin_url('admin.php?page=sbp-products&action=edit&id=' . $product['id']); ?>">
                                                    <?php _e('编辑', 'small-batch-production'); ?>
                                                </a> | 
                                            </span>
                                            <span class="trash">
                                                <a href="<?php echo wp_nonce_url(admin_url('admin.php?page=sbp-products&action=delete&id=' . $product['id']), 'delete_product_' . $product['id']); ?>" 
                                                   onclick="return confirm('<?php _e('确定要删除此产品吗?', 'small-batch-production'); ?>');">
                                                    <?php _e('删除', 'small-batch-production'); ?>
                                                </a>
                                            </span>
                                        </div>
                                    </td>
                                    <td class="column-sku"><?php echo esc_html($product['sku']); ?></td>
                                    <td class="column-price"><?php echo number_format($product['price'], 2); ?></td>
                                    <td class="column-batch-size">
                                        <?php printf(__('%d - %d', 'small-batch-production'), $product['min_batch_size'], $product['max_batch_size']); ?>
                                    </td>
                                    <td class="column-production-time"><?php echo esc_html($product['production_time']); ?></td>
                                    <td class="column-status">
                                        <span class="sbp-status-badge sbp-status-<?php echo esc_attr($product['status']); ?>">
                                            <?php echo self::get_status_label($product['status']); ?>
                                        </span>
                                    </td>
                                    <td class="column-actions">
                                        <a href="<?php echo admin_url('admin.php?page=sbp-orders&action=add&product_id=' . $product['id']); ?>" 
本文来自网络,不代表柔性供应链服务中心立场,转载请注明出处:https://mall.org.cn/5671.html

EXCHANGES®作者

上一篇
下一篇

为您推荐

发表回复

联系我们

联系我们

18559313275

在线咨询: QQ交谈

邮箱: vip@exchanges.center

工作时间:周一至周五,9:00-17:30,节假日休息
返回顶部