如何在 WooCommerce 中编辑 "woocommerce_thankyou" 钩子的输出?

How to edit the output of "woocommerce_thankyou" hook in WooCommerce?

我相信这个问题已经被问过很多次了。我什至得到了很少的答案,但我真的不知道如何在 Wordpress + WooCommerce 中编辑任何 "action" 或 "filter" 挂钩。

以结帐页面为例,底部通常是购物车的摘要,此信息由以下代码生成:

<?php do_action( 'woocommerce_thankyou', $order->get_id() ); ?>

我尝试了不同的方法在其他文件中搜索 "woocommerce_thankyou",但我找不到任何东西。那么,输出代码是如何生成的呢?必须有一个文件被调用来执行它。

您只是错过了 WooCommerce 插件文件 includes/wc-template-hooks.php where the action hook woocommerce_thankyou is called on line 260 triggering woocommerce_order_details_table() function, which is located on includes/wc-template-functions.php from line 2641 to 2560:

if ( ! function_exists( 'woocommerce_order_details_table' ) ) {

    /**
     * Displays order details in a table.
     *
     * @param mixed $order_id Order ID.
     */
    function woocommerce_order_details_table( $order_id ) {
        if ( ! $order_id ) {
            return;
        }

        wc_get_template(
            'order/order-details.php',
            array(
                'order_id' => $order_id,
            )
        );
    }
}

如您所见,此函数调用 WooCommerce 模板 order/order-details.php,输出相关的订单详细信息。

因此您必须编辑 WooCommerce 模板 order/order-details.php 才能进行更改。 但此模板在其他页面上多次使用。

所以您可以使用 is_wc_endpoint_url('order-received') 条件标签定位 WooCommerce 谢谢页面,您将能够使用它覆盖此模板:

if ( is_wc_endpoint_url('order-received') ) {
    // The altered code for the thankyou page
} else {
    // The normal code for all other pages
}

相关: