由于WoodCommmerce插件的升级,显示媒介/源的滤镜消失了.我知道如何检索MEDIUM/SOURCE列,但我的数据没有填入其中.代码使用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不兼容.我通过查看文档进行了兼容,但是源/媒介仍然没有显示出来.现在我只想知道我在哪里做错了.

推荐答案

Important note: WooCommerce HPOS要求所有WC订单元数据都已使用CRUD方法(而不是WordPress POST元函数)设置.只有与WooCommerce相关的setter方法才会填充HPOS使用的WooCommerce定制数据库表.

由于您使用的是WC Order自定义域和外部类/方法,因此我无法真正测试第二个函数中的代码.

现在,对于manage_woocommerce_page_wc-orders_custom_column钩子,两个可用的参数是$column$order(但不是$order_id),所以我修改了下面的代码:

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

public 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'])) {
                    $source = $stats['rows'][0]['sourceMedium'];
                    $order->update_meta_data('analytify_woo_order_source', $source);
                    $order->save();
                }
            } 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);

它应该工作(untested).

相关:Add columns to admin orders list in WooCommerce (+ HPOS)

Php相关问答推荐

如何发送woocommerce订单跟踪码与短信

PHP DOMDocument忽略第一个表S结束标记

如何将对我的域的请求重定向到子文件夹中的索引,同时保留域URL并使用.htaccess?

在没有谷歌云客户端PHP库的情况下将图像上传到PHP中的Google存储API?

如何将隐式乘法添加到PHPPEG基本计算器语法中?

PHP MySQL求和子树并与父树累加

不包括WooCommerce中单独销售的产品

目标类[Set_Locale]不存在.拉威尔

将下拉 Select 的值从WooCommerce后端保存并显示到前端

我的邮箱中的链接在我的URL中添加了一个

在WordPress中从CPT中删除插件

函数存储到变量中

为什么PHP PDO忽略NOT NULL约束?

将购物车按钮重定向到Checkout-Wooccommerce

Woocommerce单一产品Ajax添加到带有自定义购物车项目数据的购物车

安装 Herd 并返回 Valet 后为 502

如何搜索和替换 XML 文件中的一段文本?

如何从此字符串创建 Carbon 对象:2022-06-21T05:52:46.648533678Z?

PHP 中是否有 is_closure() 函数?

在全局安装 Composer 包后运行命令