单域名下双WooCommerce站点订单来源标识添加需求
解决方案
一、网站后台订单详情页添加站点标识
方法1:使用钩子函数(推荐)
直接在主题的functions.php文件或自定义插件中添加以下代码,通过WooCommerce钩子在订单详情页插入站点标识:
add_action('woocommerce_admin_order_data_after_billing_address', 'add_site_identifier_to_admin_order', 10, 1); function add_site_identifier_to_admin_order($order) { // 通过请求路径判断所属站点 $site_path = $_SERVER['REQUEST_URI']; if (strpos($site_path, '/website1') !== false) { $site_label = '所属站点:Website 1'; } elseif (strpos($site_path, '/website2') !== false) { $site_label = '所属站点:Website 2'; } else { $site_label = '所属站点:未知'; } // 输出带样式的标识文本 echo '<div class="form-field form-field-wide"> <label>' . __('站点信息', 'woocommerce') . '</label> <p style="font-weight: bold; color: #2271b1;">' . $site_label . '</p> </div>'; }
如果是WordPress多站点模式搭建,用get_current_blog_id()判断更准确:
add_action('woocommerce_admin_order_data_after_billing_address', 'add_site_identifier_to_admin_order', 10, 1); function add_site_identifier_to_admin_order($order) { $blog_id = get_current_blog_id(); switch ($blog_id) { case 1: $site_label = '所属站点:Website 1'; break; case 2: $site_label = '所属站点:Website 2'; break; default: $site_label = '所属站点:未知'; } echo '<div class="form-field form-field-wide"> <label>' . __('站点信息', 'woocommerce') . '</label> <p style="font-weight: bold; color: #2271b1;">' . $site_label . '</p> </div>'; }
方法2:自定义模板修改
复制wp-content/plugins/woocommerce/templates/admin/order/view/order-details.php到主题的woocommerce/admin/order/view/目录下,在账单地址后合适位置添加以下代码:
<?php // 判断所属站点 $site_path = $_SERVER['REQUEST_URI']; if (strpos($site_path, '/website1') !== false) { $site_label = '所属站点:Website 1'; } elseif (strpos($site_path, '/website2') !== false) { $site_label = '所属站点:Website 2'; } ?> <div class="form-field form-field-wide"> <label><?php _e('站点信息', 'woocommerce'); ?></label> <p style="font-weight: bold; color: #2271b1;"><?php echo $site_label; ?></p> </div>
二、Woo Mobile App订单详情添加站点标识
Woo Mobile App依赖WooCommerce REST API获取订单数据,需通过钩子修改API返回内容,让App能读取到站点标识:
步骤1:给API订单数据添加站点标识字段
在functions.php或自定义插件中添加代码:
add_filter('woocommerce_rest_prepare_shop_order_object', 'add_site_identifier_to_api_order', 10, 3); function add_site_identifier_to_api_order($response, $order, $request) { // 判断站点 $site_path = $_SERVER['REQUEST_URI']; if (strpos($site_path, '/website1') !== false) { $site_label = 'Website 1'; } elseif (strpos($site_path, '/website2') !== false) { $site_label = 'Website 2'; } else { $site_label = '未知站点'; } // 将标识添加到API响应 $response->data['site_identifier'] = $site_label; return $response; }
步骤2:让App显示标识字段
默认官方App不支持自定义字段显示,可通过两种方式处理:
- 自定义开发App:在订单详情页调用API时读取
site_identifier字段,在指定位置渲染显示; - 保存为订单元数据:将站点标识存为订单自定义元数据,部分支持自定义字段的第三方App可自动识别:
// 下单时保存站点标识到订单元数据 add_action('woocommerce_checkout_create_order', 'save_site_identifier_to_order_meta', 10, 2); function save_site_identifier_to_order_meta($order, $data) { $site_path = $_SERVER['REQUEST_URI']; if (strpos($site_path, '/website1') !== false) { $site_label = 'Website 1'; } elseif (strpos($site_path, '/website2') !== false) { $site_label = 'Website 2'; } else { $site_label = '未知站点'; } $order->update_meta_data('_site_identifier', $site_label); } // 让API返回该元数据 add_filter('woocommerce_rest_prepare_shop_order_object', 'include_site_meta_in_api', 10, 3); function include_site_meta_in_api($response, $order, $request) { $site_label = $order->get_meta('_site_identifier'); $response->data['site_identifier'] = $site_label; $response->data['meta_data'][] = array( 'key' => '_site_identifier', 'value' => $site_label ); return $response; }
注意事项
- 代码添加前请备份主题
functions.php,或使用自定义插件添加代码,避免主题更新丢失; - 多站点模式下,用
get_current_blog_id()判断站点比路径判断更可靠; - 测试时请分别在两个站点下单,验证后台和App的显示效果。
内容的提问来源于stack exchange,提问作者Firat
相关产品推荐
相关产品推荐

