如何在 WooCommerce 中编辑“woocommerce_thankyou”挂钩的输出?

And*_*ewS 2 php wordpress orders woocommerce hook-woocommerce

我确信这个问题已经被问过很多次了。我什至得到了很少的答案,但我真的不知道如何在 Wordpress + WooCommerce 中编辑任何“操作”或“过滤器”挂钩。

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

<?php do_action( 'woocommerce_thankyou', $order->get_id() ); ?>
Run Code Online (Sandbox Code Playgroud)

我尝试了不同的方式在其他文件中搜索“woocommerce_thankyou”,但我找不到任何内容。那么,输出代码是如何生成的呢?必须有一个文件来调用它。

Loi*_*tec 6

您只是错过了 WooCommerce 插件文件,其中在第 260 行触发函数中调用了includes/wc-template-hooks.php操作挂钩,该函数位于第 2641 行到 2560行:woocommerce_thankyou woocommerce_order_details_table()includes/wc-template-functions.php

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,
            )
        );
    }
}
Run Code Online (Sandbox Code Playgroud)

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

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

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

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

相关:WooCommerce 操作挂钩和覆盖模板