AndrewS
AndrewS

Reputation: 1565

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

I am sure this have beed asked many time. I even got on few answers but I realy dont get the idea of how to edit any "action" or "filter" hooks in Wordpress + WooCommerce.

As an example on checkout page, at the bottom where is usually the summary of the shopping cart, this info is generated by this code:

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

I tried different way to search for "woocommerce_thankyou" in other files but I cant find anything. So, how the output code is generated? There must be a file which is called for doing it.

Upvotes: 2

Views: 4839

Answers (1)

LoicTheAztec
LoicTheAztec

Reputation: 254491

You simply missed the WooCommerce plugin file 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,
            )
        );
    }
}

As you can see this function calls the WooCommerce template order/order-details.php, that outputs the related order details.

So you will have to edit the WooCommerce template order/order-details.php to make changes. But this template is used many times on other pages.

So you can target the WooCommerce thankyou page using is_wc_endpoint_url('order-received') conditional tag and you will be able to override this template using it like:

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

Related: WooCommerce action hooks and overriding templates

Upvotes: 6

Related Questions