在 Woocommmerce 中更改 HPOS 后,WC_Admin_Report::get_order_report_data() 不起作用。有什么解决方法?

After HPOS change in Woocommmerce WC_Admin_Report::get_order_report_data() is not working. What can be the workaround?

提问人:Shiva Dubey 提问时间:10/17/2023 最后编辑:Shiva Dubey 更新时间:10/17/2023 访问量:33

问:

我尝试使用 WC_Admin_Report::get_order_report_data(),但它取决于post_meta表而不是wc_orders表,因此它不会获取数据。我尝试使用 $order->get_meta('key') 和 $order->update_meta_data('key', $value) 使我的插件 HPOS 兼容。有没有其他解决方案。

    public static function elex_mv_product_data_last_seven_days() {
    
        include_once( WP_PLUGIN_DIR . '/woocommerce/includes/admin/reports/class-wc-admin-report.php' );
        include_once( WP_PLUGIN_DIR . '/woocommerce/includes/admin/reports/class-wc-report-sales-by-date.php' );

        $WC_Admin_Report = new \WC_Admin_Report();
        $spark_line_data = array();
        $order_statuses = array( 'wc-processing', 'wc-completed' );
        if ( ! ( isset( $_POST['_ajax_nonce'] ) && wp_verify_nonce( sanitize_text_field( $_POST['_ajax_nonce'] ), 'mv_nonce' ) ) ) {
            return;
        }
        
        $days = isset( $_POST['days'] ) ? sanitize_text_field( $_POST['days'] ) : 7;
        if ( isset( $_POST['product_id'] ) && ! empty( $_POST['product_id'] ) ) {
        
            $data = $WC_Admin_Report->get_order_report_data(
                array(
                    'data'         => array(
                        '_product_id' => array(
                            'type'            => 'order_item_meta',
                            'order_item_type' => 'line_item',
                            'function'        => '',
                            'name'          => 'product_id',
                        ),
                        '_line_total'     => array(
                            'type'            => 'order_item_meta',
                            'order_item_type' => 'line_item',
                            'function'        => 'SUM',
                            'name'            => 'sparkline_value',
                        ),
                        '_qty'    => array(
                            'type'            => 'order_item_meta',
                            'order_item_type' => 'line_item',
                            'function'        => 'SUM',
                            'name'            => 'quantity',
                        ),
                        'post_date'   => array(
                            'type'     => 'post_data',
                            'function' => '',
                            'name'     => 'post_date',
                        ),
                        'elex_mv_store_id' => array(
                            'type'     => 'meta',
                            'function' => '',
                            'name'     => 'elex_mv_store_id',
                        ),
                        'vendor_earning_commission' => array(
                            'type'     => 'meta',
                            'function' => 'SUM',
                            'name'     => 'commission',
                        ),
                        
                    ),
                    
                    
                    'where'        => array(
                        array(
                            'key'      => 'post_date',
                            'value'    => gmdate( 'Y-m-d', strtotime( 'midnight -' . ( $days - 1 ) . ' days', current_time( 'timestamp' ) ) ),
                            'operator' => '>',
                        ),

                        array(
                            'key'      => 'order_item_meta__product_id.meta_value',
                            'value'    => sanitize_text_field( $_POST['product_id'] ),
                            'operator' => '=',
                        ),
                        array(
                            'key'      => 'post_status',
                            'value'    => $order_statuses,
                            'operator' => 'IN',
                        ), 
                        
                    ),
                    'group_by'     => 'YEAR(posts.post_date), MONTH(posts.post_date), DAY(posts.post_date)',
                    'query_type'   => 'get_results',
                    'filter_range' => false,
                )
            );
        } else {

            $report_Array = array(
                'data'         => array(
                    '_order_total' => array(
                        'type'     => 'meta',
                        'function' => 'SUM',
                        'name'     => 'sparkline_value',
                    ),
                    'post_date'    => array(
                        'type'     => 'post_data',
                        'function' => '',
                        'name'     => 'post_date',
                    ),
                    '_qty'    => array(
                        'type'            => 'order_item_meta',
                        'order_item_type' => 'line_item',
                        'function'        => 'SUM',
                        'name'            => 'quantity',
                    ),
                    'elex_mv_store_id' => array(
                        'type'     => 'meta',
                        'function' => '',
                        'name'     => 'elex_mv_store_id',
                    ),
                    'vendor_earning_commission' => array(
                        'type'     => 'meta',
                        'function' => 'SUM',
                        'name'     => 'commission',
                    ),
                
                    
                ),
            
                'where'        => array(
                    array(
                        'key'      => 'post_date',
                        'value'    => gmdate( 'Y-m-d', strtotime( 'midnight -' . ( $days - 1 ) . ' days', current_time( 'timestamp' ) ) ),
                        'operator' => '>',
                    ), 
                
                    array(
                        'key'      => 'post_status',
                        'value'    => $order_statuses,
                        'operator' => 'IN',
                    ), 
                    array(
                        'key'      => 'meta_elex_mv_store_id.meta_value',
                        'value'    => isset( $_POST['store_id'] ) ? sanitize_text_field( $_POST['store_id'] ) : '',
                        'operator' => '=',
                    ),
                    
                
                ),
                
                
                'group_by'     => 'YEAR(posts.post_date), MONTH(posts.post_date), DAY(posts.post_date)',
                'query_type'   => 'get_results',
                'filter_range' => false,            
                
            );


            $data = $WC_Admin_Report->get_order_report_data($report_Array);

        }
    
        
    
    
        $filteredArray = array_filter(
            $data,
            function( $element ) {
                if ( ! ( isset( $_POST['_ajax_nonce'] ) && wp_verify_nonce( sanitize_text_field( $_POST['_ajax_nonce'] ), 'mv_nonce' ) ) ) {
                    return;
                }
                if ( ! empty( $_POST['store_id'] ) ) {
                    $store_id = sanitize_text_field( $_POST['store_id'] );
                    return $element->elex_mv_store_id === $store_id ;
                }
            
            }
        );

        


        $sparkline_data = array_values( $WC_Admin_Report->prepare_chart_data( $filteredArray, 'post_date', 'sparkline_value', $days - 1, strtotime( 'midnight -' . ( $days - 1 ) . ' days', current_time( 'timestamp' ) ), 'day' ) );
        $sparkline_qty_data = array_values( $WC_Admin_Report->prepare_chart_data( $filteredArray, 'post_date', 'commission', $days - 1, strtotime( 'midnight -' . ( $days - 1 ) . ' days', current_time( 'timestamp' ) ), 'day' ) );
        foreach ( $sparkline_data as $key => $data ) {
        
            $timestamp = $data[0] / 1000; // convert milliseconds to seconds
            $date = gmdate( 'd F', strtotime( gmdate( 'Y-m-d', $timestamp ) ) ); 
            if ( $data[1] < 0 ) {
                $data[1] = 0;
            }
            $spark_line_data['quantity'][] = $data[1];
            $spark_line_data['data'][] = $date;
        }
        foreach ( $sparkline_qty_data as $key => $data ) {
            $spark_line_data['labels'][] = $data[1];
        }
        
        

        return wp_send_json_success( wp_json_encode( $spark_line_data ) );

    }type here

我试图在平台上搜索解决方案,但找不到。现在,我正在考虑直接使用 SQl 而不是这个函数。

php mysql wordpress woocommerce 图表 .js

评论


答: 暂无答案