WooCommerce定时SMS报错:send_scheduled_sms参数不足求助
解决WooCommerce定时短信的参数不足错误
问题根源
AutomateWoo调度Cron任务时仅传递了1个参数,但你的send_scheduled_sms()函数定义要求接收2个参数,导致参数不匹配触发致命错误。
两种修复方案
方案一:修改函数,仅依赖订单ID(推荐)
直接从订单对象中获取用户手机号,无需额外传递参数,更符合WooCommerce的逻辑:
function send_scheduled_sms( $order_id ) { // 验证订单是否存在 $order = wc_get_order( $order_id ); if ( ! $order ) return; // 获取用户 billing 手机号 $customer_phone = $order->get_billing_phone(); // 调用SMS插件发送短信(替换为你实际使用的插件发送函数) if ( ! empty( $customer_phone ) ) { $message = "您好,您的订单【{$order->get_order_number()}】已进入处理状态,请留意后续物流通知。"; // 示例:假设插件提供的发送函数是 wp_sms_send() wp_sms_send( $customer_phone, $message ); } }
同时确保AutomateWoo调度时仅传递订单ID即可。
方案二:确保调度时传递完整参数
如果必须保留两个参数,需要在注册钩子和调度任务时明确参数数量:
// 注册自定义钩子,指定接收2个参数 add_action( 'custom_send_scheduled_sms', 'send_scheduled_sms', 10, 2 ); function send_scheduled_sms( $order_id, $customer_phone ) { // 短信发送逻辑 if ( ! empty( $customer_phone ) ) { $message = "您的订单【{$order_id}】已处理,请留意后续通知。"; wp_sms_send( $customer_phone, $message ); } } // 在AutomateWoo触发时调度任务,传递两个参数 add_action( 'automatewoo/workflow/triggered', 'schedule_sms_task', 10, 2 ); function schedule_sms_task( $workflow, $data ) { // 确保数据是订单对象 if ( isset( $data['order'] ) && $data['order'] instanceof WC_Order ) { $order = $data['order']; // 延迟2分钟执行 wp_schedule_single_event( time() + 120, 'custom_send_scheduled_sms', [ $order->get_id(), $order->get_billing_phone() ] ); } }
关键注意点:
add_action的第三个参数要设置为2,表明该钩子接受2个参数- 调度任务时,把需要传递的参数放在数组中作为
wp_schedule_single_event的第三个参数
验证步骤
- 保存修改后的
functions.php文件 - 修改测试订单状态为Processing
- 等待2分钟后,检查WP Crontrol是否无报错,同时确认用户是否收到短信
内容的提问来源于stack exchange,提问作者GODFREY OTIENO
相关产品推荐
相关产品推荐

