WP WooCommerce如何实现单个订单导出至第三方平台
WooCommerce单个订单添加导出推送按钮实现方案
核心实现逻辑
不需要安装批量导出插件,直接通过WooCommerce官方提供的钩子即可实现,新手也能快速上手:
- 按钮位置优先选订单详情页顶部操作栏,用
woocommerce_order_actions钩子实现,代码量最少,和原生后台操作逻辑完全一致 - 如果需要放在右侧边栏,用WordPress原生的
add_meta_boxes注册自定义模块即可,推送逻辑和顶部按钮完全通用 - 所有代码可以直接放在当前子主题的
functions.php文件,或者自定义功能插件中,不需要修改WooCommerce核心文件
可直接复用的代码
版本1:顶部操作栏加按钮(最简实现)
这个版本不需要额外写表单渲染、权限校验逻辑,WooCommerce原生钩子会自动处理按钮展示、请求跳转逻辑,新手优先用这个方案:
// 在订单操作栏添加Export按钮 add_filter( 'woocommerce_order_actions', 'add_custom_export_order_action', 20, 2 ); function add_custom_export_order_action( $actions, $order ) { if ( ! current_user_can( 'edit_shop_orders' ) ) return $actions; $actions['push_to_third_party'] = 'Export 导出'; return $actions; } // 按钮点击后的推送逻辑 add_action( 'woocommerce_order_action_push_to_third_party', 'execute_order_push' ); function execute_order_push( $order ) { // 组装需要推送的订单数据,可根据第三方接口要求增删字段 $payload = [ 'order_id' => $order->get_id(), 'order_no' => $order->get_order_number(), 'total_amount' => $order->get_total(), 'currency' => $order->get_currency(), 'customer_info' => [ 'name' => $order->get_billing_first_name() . ' ' . $order->get_billing_last_name(), 'contact' => $order->get_billing_phone(), 'email' => $order->get_billing_email(), 'address' => $order->get_formatted_billing_address() ], 'products' => [] ]; // 遍历组装订单商品数据 foreach ( $order->get_items() as $item ) { $payload['products'][] = [ 'sku' => $item->get_product()->get_sku(), 'name' => $item->get_name(), 'quantity' => $item->get_quantity(), 'price' => $item->get_subtotal() ]; } // 替换为实际第三方接口推送逻辑,以下为POST请求示例 $api_endpoint = '替换为你的第三方平台接口地址'; $api_token = '替换为你的接口鉴权密钥'; $response = wp_remote_post( $api_endpoint, [ 'headers' => [ 'Content-Type' => 'application/json', 'Authorization' => 'Bearer ' . $api_token ], 'body' => json_encode( $payload ), 'timeout' => 15 ]); // 推送结果记录与提示 if ( ! is_wp_error( $response ) && wp_remote_retrieve_response_code( $response ) === 200 ) { $order->add_order_note( '订单已成功推送至第三方平台' ); wc_add_notice( '订单推送成功', 'success' ); } else { wc_add_notice( '订单推送失败,请检查接口配置', 'error' ); } wp_safe_redirect( wp_get_referer() ); exit; }
版本2:右侧边栏加按钮
如果需要把按钮放在侧边栏,只需要替换按钮注册部分的代码,上面写的execute_order_push推送逻辑可以直接复用:
// 注册侧边栏导出模块 add_action( 'add_meta_boxes', 'register_export_sidebar_box' ); function register_export_sidebar_box() { add_meta_box( 'order_export_side_box', '第三方平台推送', 'render_export_sidebar_button', 'shop_order', 'side', 'high' ); } // 渲染侧边栏按钮 function render_export_sidebar_button( $post ) { $order = wc_get_order( $post->ID ); wp_nonce_field( 'export_order_' . $order->get_id(), 'export_nonce' ); echo '<p>点击按钮将当前订单数据推送至第三方平台</p>'; echo '<button type="submit" class="button button-primary" style="width:100%;text-align:center;" name="trigger_order_export" value="1">Export 导出</button>'; } // 处理侧边栏按钮提交 add_action( 'save_post_shop_order', 'handle_sidebar_export_request' ); function handle_sidebar_export_request( $post_id ) { if ( ! isset( $_POST['trigger_order_export'] ) || ! current_user_can( 'edit_shop_orders' ) ) return; if ( ! wp_verify_nonce( $_POST['export_nonce'], 'export_order_' . $post_id ) ) return; $order = wc_get_order( $post_id ); execute_order_push( $order ); }
新手注意事项
- 代码优先放到子主题的
functions.php中,避免父主题更新时自定义代码被覆盖 - 测试阶段可以先把推送请求部分替换为
$order->add_order_note( print_r( $payload, true ) ),先确认组装的订单数据符合接口要求,再接正式推送逻辑 - 如果第三方平台提供了官方PHP SDK,只需要把示例中
wp_remote_post的部分替换为SDK对应的调用方法即可 - 按钮文案、推送字段都可以根据实际需求自由调整
内容的提问来源于stack exchange,提问作者Catalin Farcas
相关产品推荐
相关产品推荐

