【问题标题】:Override functionalities in Woocommerce Reports sectionWoocommerce 报告部分中的覆盖功能
【发布时间】:2017-11-23 03:33:53
【问题描述】:

我需要在 WooCommerce 中自定义报告,所以我必须编辑核心文件 class-wc-report-sales-by-date.php,这样的文件不使用任何钩子。

在 WC 源代码中检查这个:includes/admin/reports/class-wc-report-sales-by-date.php

我必须编辑 393 行,这个变量:$this->report_data->total_sales 我需要自定义 Total Sales 金额以添加另一个值。

如何覆盖 WooCommerce 这个核心文件?

【问题讨论】:

    标签: wordpress woocommerce report hook-woocommerce orders


    【解决方案1】:

    永远不要覆盖核心文件……还有其他方法可以做到这一点。如果您查看 line 411,您有 woocommerce_admin_report_data 过滤器挂钩来进行更改,这样(示例):

    add_filter( 'woocommerce_admin_report_data', 'custom_admin_report_data', 10, 1 );
    function custom_admin_report_data( $report_data ){
        // HERE you make your calculations and changes
        // New amout to set (example)
        $new_calculated_amount = 100;
    
        // Set the new amounts for "total_sales" key
        $report_data->total_sales = $new_calculated_amount;
    
        // Raw data output just for testing, to get the keys and the structure of the data
        // to be removed
        echo '<pre>'; print_r($report_data); echo '</pre>';
    
        // Return the changed data object
        return $report_data;
    }
    

    代码进入您的活动子主题(或主题)的 function.php 文件或任何插件文件中。

    经过测试并且有效。

    我已经包含了一行代码,用于输出您应该删除的原始数据……只是为了查看数据结构以及函数对"total_sales" 值所做的更改……


    原始数据输出是这样的(为您提供数据结构以更好地进行更改)

    stdClass Object
    (
        [order_counts] => Array
            (
                [0] => stdClass Object
                    (
                        [count] => 1
                        [post_date] => 2017-11-21 16:45:43
                    )
            )
        [coupons] => Array
            (
            )
        [order_items] => Array
            (
                [0] => stdClass Object
                    (
                        [order_item_count] => 1
                        [post_date] => 2017-11-21 16:45:43
                    )
            )
        [refunded_order_items] => 0
        [orders] => Array
            (
                [0] => stdClass Object
                    (
                        [total_sales] => 48
                        [total_shipping] => 15
                        [total_tax] => 5
                        [total_shipping_tax] => 3
                        [post_date] => 2017-11-21 16:45:43
                    )
            )
        [full_refunds] => Array
            (
            )
        [partial_refunds] => Array
            (
            )
        [refund_lines] => Array
            (
            )
        [total_tax_refunded] => 0
        [total_shipping_refunded] => 0
        [total_shipping_tax_refunded] => 0
        [total_refunds] => 0
        [total_tax] => 5.00
        [total_shipping] => 15.00
        [total_shipping_tax] => 3.00
        [total_sales] => 48.00
        [net_sales] => 25.00
        [average_sales] => 3.57
        [average_total_sales] => 6.86
        [total_coupons] => 0.00
        [total_refunded_orders] => 0
        [total_orders] => 1
        [total_items] => 1
    )
    

    如您所见,您还需要对“订单”对象数据进行更改,因为您还拥有"total_sales" 键……

    【讨论】:

    • 谢谢。这就是我需要的。非常感谢您的时间和帮助:)
    • 这是否也可以在“按产品销售”的图表侧边栏/图例中操作数据或添加其他项目,此过滤器仅影响“按日期销售”?
    猜你喜欢
    • 2016-10-20
    • 1970-01-01
    • 1970-01-01
    • 2022-10-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-19
    相关资源
    最近更新 更多