覆盖 Woocommerce 报告部分中的功能
Override functionalities in Woocommerce Reports section
我需要在 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 这个核心文件?
永远不要覆盖核心文件……还有其他方法可以做到这一点。如果你查看 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 文件或任何插件文件。
已测试并有效。
I have included a line of code that outputs the raw data that you should remove… It's just to see the data structure and the changes made by the function on the "total_sales"
value…
原始数据输出是这样的(为您提供数据结构以更好地进行更改):
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
)
So as you can see you also need to make changes on the "orders" object data as you also have the "total_sales"
key…
我需要在 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 这个核心文件?
永远不要覆盖核心文件……还有其他方法可以做到这一点。如果你查看 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 文件或任何插件文件。
已测试并有效。
I have included a line of code that outputs the raw data that you should remove… It's just to see the data structure and the changes made by the function on the
"total_sales"
value…
原始数据输出是这样的(为您提供数据结构以更好地进行更改):
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
)
So as you can see you also need to make changes on the "orders" object data as you also have the
"total_sales"
key…