WooCommerce HPOS后台订单列表自定义列数据不填充排查求助
问题:WooCommerce HPOS适配后自定义订单列无法填充数据
WooCommerce插件升级后,原来源/媒介筛选器消失,已恢复自定义列但数据无法填充。代码已适配HPOS,以下是相关代码,请求排查错误原因:
列显示代码
public function source_medium_column($columns) { $new_columns = (is_array($columns)) ? $columns : array(); $new_columns['MyPluginOne_source'] = __('Source/Medium', 'wp-MyPluginOne-woocommerce'); return $new_columns; }
数据填充代码(无效)
public function source_medium_value($column, $order_id) { $order = wc_get_order($order_id); if ($column == 'MyPluginOne_source') { // Check for the new meta key first $source = $order->get_meta('MyPluginOne_woo_order_source', true); if ($source) { echo $source; return; } // If the new meta key is not found, try the deprecated one $source_medium_deprecated = $order->get_meta('MyPluginOne_woo_single_source', true); if ($source_medium_deprecated) { echo $source_medium_deprecated; return; } // Fetch source/medium on request. if (!$source && isset($_GET['MyPluginOne_woo_fetch_sale_source'])) { error_log('Fetching source/medium for order ' . $order_id); $post_date = get_the_date('Y-m-d', $order->get_id()); $start_date = date('Y-m-d', strtotime($post_date . ' - 5 days')); $end_date = date('Y-m-d', strtotime($post_date . ' + 5 days')); // Get sources from ga4. if (method_exists('WPMyPluginOne_Utils', 'get_ga_mode') && 'ga4' === WPMyPluginOne_Utils::get_ga_mode()) { $stats = $GLOBALS['WP_MyPluginOne']->get_reports( 'MyPluginOne_woo_order_source', array(), array( 'start' => $start_date, 'end' => $end_date ), array( 'sourceMedium', 'transactionId', ), array(), array( 'logic' => 'AND', 'filters' => array( array( 'type' => 'dimension', 'name' => 'transactionId', 'match_type' => 1, 'value' => $order->get_id() ) ) ), 0, false ); error_log('Stats: ' . print_r($stats, true)); if (!empty($stats['rows'][0]['sourceMedium'])) { $order->update_meta_data('MyPluginOne_woo_order_source', $stats['rows'][0]['sourceMedium']); $source = $stats['rows'][0]['sourceMedium']; } } else { $stats = $GLOBALS['WP_MyPluginOne']->pa_get_analytics_dashboard('ga:totalEvents', $start_date, $end_date, 'ga:sourceMedium,ga:eventCategory,ga:eventLabel', false, 'ga:eventCategory==MyPluginOne_orders;ga:eventAction==order_created;ga:eventLabel==' . $post->ID); if (isset($stats['rows'][0][0])) { $source = $stats['rows'][0][0]; $order->update_meta_data('MyPluginOne_woo_order_source', $source); } } } error_log('Final source: ' . $source); echo $source; } }
钩子代码
add_filter('manage_woocommerce_page_wc-orders_columns', array($this, 'source_medium_column')); add_action('manage_woocommerce_page_wc-orders_custom_column', array($this, 'source_medium_value'),10,2);
错误排查分析
- HPOS钩子参数不匹配:
manage_woocommerce_page_wc-orders_custom_column钩子在HPOS模式下,第二个参数是Order对象而非订单ID,你当前函数接收的是$order_id,调用wc_get_order($order_id)会因为传入对象导致获取失败,后续所有元数据读取都无法正常执行。 - 未保存订单元数据:从GA接口获取到数据后,仅调用了
update_meta_data但未执行$order->save(),导致数据无法持久化到数据库,刷新页面后数据丢失。 - 变量未定义错误:非GA4分支代码中使用了未定义的
$post->ID,应该替换为$order->get_id()。 - 订单日期获取不可靠:使用
get_the_date依赖当前全局post环境,HPOS下订单不一定是post类型,应改用Order对象的$order->get_date_created()->format('Y-m-d')方法获取创建日期。
修正后的核心代码示例
public function source_medium_value($column, $order) { // 兼容非HPOS场景,确保获取到正确的Order对象 if (!$order instanceof WC_Order) { $order = wc_get_order($order); } if ($column == 'MyPluginOne_source') { $source = $order->get_meta('MyPluginOne_woo_order_source', true); if ($source) { echo $source; return; } $source_medium_deprecated = $order->get_meta('MyPluginOne_woo_single_source', true); if ($source_medium_deprecated) { echo $source_medium_deprecated; return; } if (!$source && isset($_GET['MyPluginOne_woo_fetch_sale_source'])) { error_log('Fetching source/medium for order ' . $order->get_id()); // 使用Order对象方法获取创建日期 $post_date = $order->get_date_created()->format('Y-m-d'); $start_date = date('Y-m-d', strtotime($post_date . ' - 5 days')); $end_date = date('Y-m-d', strtotime($post_date . ' + 5 days')); if (method_exists('WPMyPluginOne_Utils', 'get_ga_mode') && 'ga4' === WPMyPluginOne_Utils::get_ga_mode()) { $stats = $GLOBALS['WP_MyPluginOne']->get_reports( 'MyPluginOne_woo_order_source', array(), array('start' => $start_date, 'end' => $end_date), array('sourceMedium', 'transactionId'), array(), array( 'logic' => 'AND', 'filters' => array( array( 'type' => 'dimension', 'name' => 'transactionId', 'match_type' => 1, 'value' => $order->get_id() ) ) ), 0, false ); error_log('Stats: ' . print_r($stats, true)); if (!empty($stats['rows'][0]['sourceMedium'])) { $order->update_meta_data('MyPluginOne_woo_order_source', $stats['rows'][0]['sourceMedium']); $order->save(); // 新增保存操作 $source = $stats['rows'][0]['sourceMedium']; } } else { // 修正未定义的$post->ID变量 $stats = $GLOBALS['WP_MyPluginOne']->pa_get_analytics_dashboard( 'ga:totalEvents', $start_date, $end_date, 'ga:sourceMedium,ga:eventCategory,ga:eventLabel', false, 'ga:eventCategory==MyPluginOne_orders;ga:eventAction==order_created;ga:eventLabel==' . $order->get_id() ); if (isset($stats['rows'][0][0])) { $source = $stats['rows'][0][0]; $order->update_meta_data('MyPluginOne_woo_order_source', $source); $order->save(); // 新增保存操作 } } } error_log('Final source: ' . $source); echo $source; } }
内容的提问来源于stack exchange,提问作者ahmedwp
相关产品推荐
相关产品推荐

