如何识别 WooCommerce 挂钩使用的代码 'woocommerce_view_order'

How to Identify the code used by WooCommerce hook 'woocommerce_view_order'

在 WooCommerce 中,我一直在对模板进行一些编辑。到目前为止,这是直截了当的。

现在我想在 'my-account > view-order' 下的 'Order Details' table 中添加一些列。

我在模板视图中-order.php 这是 WooCommerce 'myaccount' 下的一个模板。

我没有看到要编辑的模板中的某些代码,而是看到了以下代码:

<?php do_action( 'woocommerce_view_order', $order_id ); ?>

调用此操作的代码在哪里,我可以编辑它吗?

感谢您一直以来的帮助。

你需要看看 WooCommerce 插件 includes/wc-template-hooks.php core file (line 259):

add_action( 'woocommerce_view_order', 'woocommerce_order_details_table', 10 );

如你所见,函数woocommerce_order_details_table()被hook了。那么现在让我们找到这个位于includes/wc-template-functions.php core file (starting line 2584).

的函数

如你所见this hooked function call the template file order/order-details.php

所以现在你可以做一些改变了:

1).覆盖 template file order/order-details.php via your active child theme or theme as explained in this documentation.

注意: 模板文件 order/order-details.php 也用于收到的订单(谢谢),因此请注意使用以下条件来定位您的更改:

// For view order
if ( is_wc_endpoint_url( 'view-order' ) ) {
    // Here your changes
} 
// For other cases
else {
    // Here keep the original code
}

2). Or/and 你也可以删除这个钩子函数,用你自己的自定义函数替换它,比如:

remove_action( 'woocommerce_view_order', 'woocommerce_order_details_table', 10 );
add_action( 'woocommerce_view_order', 'custom_order_details_table', 10 );
function custom_order_details_table( $order_id ) {
    if ( ! $order_id ) {
        return;
    }

    // Here below add your own custom code
}

You can also call your own custom template in that custom function, that will be used exclusively in order view endpoint...

相关:

WooCommerce 文档: