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

WooCommerce订单追踪码短信发送插件问题排查求助

WooCommerce插件问题解决方案

一、解决AJAX异步发送问题

当前按钮点击触发页面刷新,是因为按钮处于订单编辑表单内,默认会触发表单提交。要实现异步发送,需通过JS拦截点击事件,用AJAX提交请求:

  1. 修改按钮输出代码
    在订单编辑页添加按钮时,给按钮设置唯一ID并指定type="button",避免触发表单提交:
add_action('woocommerce_admin_order_data_after_billing_address', 'add_tracking_sms_button');
function add_tracking_sms_button($order) {
    ?>
    <div class="tracking-sms-container">
        <input type="text" id="tracking-code" placeholder="输入物流追踪码">
        <button type="button" id="send-tracking-sms" data-order-id="<?php echo $order->get_id(); ?>">发送追踪码短信</button>
        <div id="sms-response"></div>
    </div>
    <?php
}
  1. 添加AJAX处理JS
    通过admin_enqueue_scripts钩子加载JS,拦截按钮点击事件发送异步请求:
add_action('admin_enqueue_scripts', 'enqueue_tracking_sms_scripts');
function enqueue_tracking_sms_scripts($hook) {
    if ($hook !== 'post.php' || get_post_type() !== 'shop_order') return;
    
    wp_enqueue_script('tracking-sms-js', plugin_dir_url(__FILE__) . 'tracking-sms.js', array('jquery'), '1.0', true);
    wp_localize_script('tracking-sms-js', 'trackingSmsAjax', array(
        'ajax_url' => admin_url('admin-ajax.php'),
        'nonce' => wp_create_nonce('tracking_sms_nonce')
    ));
}

对应的tracking-sms.js内容:

jQuery(document).ready(function($) {
    $('#send-tracking-sms').on('click', function() {
        const orderId = $(this).data('order-id');
        const trackingCode = $('#tracking-code').val().trim();
        const responseDiv = $('#sms-response');
        
        if (!trackingCode) {
            responseDiv.html('<span style="color:red;">请输入物流追踪码</span>');
            return;
        }
        
        $.ajax({
            url: trackingSmsAjax.ajax_url,
            type: 'POST',
            data: {
                action: 'send_tracking_sms',
                order_id: orderId,
                tracking_code: trackingCode,
                nonce: trackingSmsAjax.nonce
            },
            beforeSend: function() {
                $('#send-tracking-sms').prop('disabled', true).text('发送中...');
                responseDiv.html('');
            },
            success: function(response) {
                $('#send-tracking-sms').prop('disabled', false).text('发送追踪码短信');
                if (response.success) {
                    responseDiv.html('<span style="color:green;">短信发送成功</span>');
                } else {
                    responseDiv.html(`<span style="color:red;">发送失败:${response.data}</span>`);
                }
            },
            error: function() {
                $('#send-tracking-sms').prop('disabled', false).text('发送追踪码短信');
                responseDiv.html('<span style="color:red;">请求出错,请重试</span>');
            }
        });
    });
});
  1. 注册AJAX处理函数
    在PHP中处理AJAX请求,调用短信服务并返回结果:
add_action('wp_ajax_send_tracking_sms', 'handle_send_tracking_sms');
function handle_send_tracking_sms() {
    // 验证权限和nonce
    if (!wp_verify_nonce($_POST['nonce'], 'tracking_sms_nonce') || !current_user_can('edit_shop_orders')) {
        wp_send_json_error('权限验证失败');
    }
    
    $orderId = intval($_POST['order_id']);
    $trackingCode = sanitize_text_field($_POST['tracking_code']);
    $order = wc_get_order($orderId);
    
    if (!$order) {
        wp_send_json_error('订单不存在');
    }
    
    $phone = $order->get_billing_phone();
    // 根据ippanel要求格式化手机号,比如添加国际区号,示例为伊朗号码,需按实际调整
    $formattedPhone = '+98' . ltrim($phone, '0');
    
    // 调用ippanel发送短信(见下文)
    $sendResult = send_ippanel_sms($formattedPhone, $trackingCode);
    
    if ($sendResult) {
        wp_send_json_success('短信发送成功');
    } else {
        wp_send_json_error('短信发送失败,请检查配置');
    }
}

二、解决短信无法发送问题(验证ippanel调用)

以下是标准的ippanel调用方式及排查要点,可对照检查你的代码:

1. 正确的ippanel调用示例

可选择官方SDK或curl调用:

function send_ippanel_sms($phone, $trackingCode) {
    $apiKey = '你的ippanel API密钥';
    $senderNumber = '你的ippanel认证发送号码';
    $message = "您好,您的订单物流追踪码是:{$trackingCode},请留意物流状态。";
    
    // 方式1:使用官方SDK(需先通过Composer安装ippanel/rest-sdk)
    try {
        $client = new \Ippanel\Client($apiKey);
        $client->send(
            $senderNumber,
            [$phone],
            $message
        );
        return true;
    } catch (\Ippanel\Errors\Error $e) {
        // 记录错误日志便于排查
        error_log('Ippanel发送错误:' . $e->getMessage());
        return false;
    }
    
    // 方式2:使用curl(未安装SDK时用)
    /*
    $url = "https://api2.ippanel.com/api/v1/sms/send/";
    $data = [
        'api_key' => $apiKey,
        'sender' => $senderNumber,
        'recipients' => [$phone],
        'message' => $message
    ];
    
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
    curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    
    $response = curl_exec($ch);
    $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);
    
    if ($httpCode === 200) {
        $responseData = json_decode($response, true);
        return isset($responseData['message_id']);
    } else {
        error_log('Ippanel请求错误,状态码:' . $httpCode . ',响应:' . $response);
        return false;
    }
    */
}

2. 排查要点

  • API密钥:确认密钥正确且未在ippanel后台被禁用
  • 手机号格式:必须带国际区号(如中国+86、伊朗+98),且无空格、短横线等特殊字符
  • 发送号码:使用的发送号码需在ippanel后台完成认证
  • 短信内容:需符合ippanel审核规则,避免敏感词
  • 错误日志:开启PHP错误日志,或通过error_log记录请求响应,查看具体错误原因(如余额不足、号码无效、权限问题等)

内容的提问来源于stack exchange,提问作者Paul Vek

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.26 22:27:13