WooCommerce HPOS 管理订单列表中包含订单元数据的自定义列

问题描述 投票:0回答:1

由于 woocommmerce 插件的升级,显示媒体/源的过滤器消失了。我知道如何检索介质/源列,但我的数据没有填充其中。该代码完全使用 HPOS 进行了优化,我的代码是:

这是显示栏的部分:

    public function source_medium_column($columns)
    {

        $new_columns                     = (is_array($columns)) ? $columns : array();
        $new_columns['analytify_source'] = __('Source/Medium', 'wp-analytify-woocommerce');

        return $new_columns;
    }

这是显示介质/源数据的部分,但这部分不起作用:

public function source_medium_value($column, $order_id)
{
    $order = wc_get_order($order_id);

    if ($column == 'analytify_source') {

        // Check for the new meta key first
        $source = $order->get_meta('analytify_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('analytify_woo_single_source', true);

        if ($source_medium_deprecated) {
            echo $source_medium_deprecated;
            return;
        }

        // Fetch source/medium on request.
        if (!$source && isset($_GET['analytify_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('WPANALYTIFY_Utils', 'get_ga_mode') && 'ga4' === WPANALYTIFY_Utils::get_ga_mode()) {
                $stats = $GLOBALS['WP_ANALYTIFY']->get_reports(
                    'analytify_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('analytify_woo_order_source', $stats['rows'][0]['sourceMedium']);
                    $source = $stats['rows'][0]['sourceMedium'];
                }
            } else {
                $stats = $GLOBALS['WP_ANALYTIFY']->pa_get_analytics_dashboard('ga:totalEvents', $start_date, $end_date, 'ga:sourceMedium,ga:eventCategory,ga:eventLabel', false, 'ga:eventCategory==analytify_orders;ga:eventAction==order_created;ga:eventLabel==' . $post->ID);

                if (isset($stats['rows'][0][0])) {
                    $source = $stats['rows'][0][0];
                    $order->update_meta_data('analytify_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 不兼容。我通过查看文档来实现兼容性,但源/介质仍然没有显示。我现在只想知道我在哪里做错了。

php wordpress woocommerce admin orders
1个回答
0
投票

对于

manage_woocommerce_page_wc-orders_custom_column
,2 个可用参数是
$column
$order
(但不是
$order_id
),所以我修改了下面的代码:

function source_medium_column( $columns ) {
    $columns['analytify_source'] = __('Source/Medium', 'wp-analytify-woocommerce');
    return $columns;
}

function source_medium_value( $column, $order )
{
    if ($column === 'analytify_source') {

        // Check for the new meta key first
        $source = $order->get_meta('analytify_woo_order_source');

        if ( $source ) {
            echo $source; return;
        }

        // If the new meta key is not found, try the deprecated one
        $source_medium_deprecated = $order->get_meta('analytify_woo_single_source');

        if ( $source_medium_deprecated ) {
            echo $source_medium_deprecated; return;
        }

        // Fetch source/medium on request.
        if ( ! $source && isset($_GET['analytify_woo_fetch_sale_source']) ) {
            error_log('Fetching source/medium for order ' . $order->get_id());

            $date_created = $order->get_date_created()->format('Y-m-d');
            $start_date   = date('Y-m-d', strtotime($date_created . ' - 5 days'));
            $end_date     = date('Y-m-d', strtotime($date_created . ' + 5 days'));

            // Get sources from ga4.
            if (method_exists('WPANALYTIFY_Utils', 'get_ga_mode') && 'ga4' === WPANALYTIFY_Utils::get_ga_mode()) {
                $stats = $GLOBALS['WP_ANALYTIFY']->get_reports(
                    'analytify_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('analytify_woo_order_source', $stats['rows'][0]['sourceMedium']);
                    $order->save();
                    $source = $stats['rows'][0]['sourceMedium'];
                }
            } else {
                $stats = $GLOBALS['WP_ANALYTIFY']->pa_get_analytics_dashboard('ga:totalEvents', $start_date, $end_date, 'ga:sourceMedium,ga:eventCategory,ga:eventLabel', false, 'ga:eventCategory==analytify_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('analytify_woo_order_source', $source);
                    $order->save();
                }
            }
        }
        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);

应该会更好用

© www.soinside.com 2019 - 2024. All rights reserved.