You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

WordPress插件开发遇AJAX 500/503错误:批量创建WooCommerce商品中断求助

解决WordPress批量创建WooCommerce商品时AJAX请求500/503错误问题

问题根源分析

你当前的核心问题在于:

  • 用for循环一次性发起大量并发AJAX请求,直接触发服务器连接数限制、资源耗尽,导致后续请求返回500(内部错误)或503(服务不可用)
  • AJAX是异步执行的,循环内的所有请求几乎同时发送,进度条的width变量会因多个回调同时修改出现混乱,无法准确反映真实进度

解决方案

1. 改用串行AJAX请求(避免并发轰炸)

把循环改成递归调用,每次完成一个请求后再发起下一个,大幅降低服务器压力:

add_action( 'admin_footer', 'my_action_javascript' ); // Write our JS below here
function my_action_javascript() { ?>
    <script type="text/javascript">
    var element = document.getElementById("theBar");
    var width = 0;
    var move = 2;
    jQuery(document).ready(function($) {
        $("#totally").click(function() {
            var data = {
                'action': 'my_action',
                'whatever': 1234
            };

            jQuery.post(ajaxurl, data, function(response) {
                console.log(response);
                width += move;
                updateProgress(width, "Product infos fetched");
                var info = {
                    'action': 'start_addprod',
                    'thing': 1658
                };
                var repeat = parseInt(response);

                // 递归串行执行请求
                function addProduct(index) {
                    if (index >= repeat) {
                        updateProgress(100, "All products added successfully!");
                        return;
                    }
                    jQuery.post(ajaxurl, info, function(response) {
                        console.log(response);
                        width += move;
                        var currentWidth = Math.min(width, 100);
                        updateProgress(currentWidth, "Woocommerce products are being added");
                        // 发起下一个请求
                        addProduct(index + 1);
                    })
                    // 处理请求失败,避免流程中断
                    .fail(function(xhr) {
                        console.error("Request failed:", xhr.statusText);
                        addProduct(index + 1);
                    });
                }
                // 启动批量创建流程
                addProduct(0);
            });
        });
    });

    // 封装进度条更新逻辑,减少重复代码
    function updateProgress(progressWidth, statusText) {
        element.style.width = progressWidth + "%";
        element.innerHTML = progressWidth + "%";
        document.getElementById("currprogstatus").textContent = statusText;
    }
    </script> <?php
}

2. 后端优化:增加批量处理能力

单个请求创建单个商品效率过低,建议修改后端接口支持批量创建,进一步减少请求总数:

add_action('wp_ajax_start_addprod', 'start_addprod_callback');
function start_addprod_callback() {
    // 每次批量创建10个商品,可根据服务器性能调整
    $batch_size = isset($_POST['batch_size']) ? intval($_POST['batch_size']) : 10;
    $thing = intval($_POST['thing']);
    
    // 批量创建商品逻辑
    for ($i=0; $i<$batch_size; $i++) {
        $product = new WC_Product_Simple();
        // ...设置商品属性(从第三方API获取的数据)...
        $product->save();
    }
    
    echo "Batch created successfully";
    wp_die();
}

前端对应调整,每次请求创建10个商品,同步更新进度条幅度:

// 修改请求参数,加入批量数量
var info = {
    'action': 'start_addprod',
    'thing': 1658,
    'batch_size': 10
};
// 调整进度条每次增加的幅度
var batchMove = move * 10;
// 递归函数内更新宽度时替换为batchMove
width += batchMove;

3. 服务器层面补充检查

  • 调整服务器并发连接数限制(Apache的MaxClients、Nginx的worker_connections)
  • 检查PHP进程数限制(pm.max_children),避免进程耗尽
  • 开启WordPress调试日志(wp-config.php中设置WP_DEBUG_LOG = true),查看500错误的具体触发原因(如数据库连接耗尽、内存不足等)

内容的提问来源于stack exchange,提问作者Artificial Inheritance

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.16 03:55:36