Johnny
Johnny

Reputation: 576

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

Within WooCommerce I have been making some edits to templates. So far this has been straight forward.

Now I am trying to add some columns into the 'Order Details' table under 'my-account > view-order'.

I am in the template view-order.php which is a template under 'myaccount' in WooCommerce.

Instead of seeing some code in a template to edit, I am seeing the following code:

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

Where is the code from this action called and can I edit it?

Thanks for all time and help.

Upvotes: 1

Views: 3932

Answers (1)

LoicTheAztec
LoicTheAztec

Reputation: 253804

You need to look at WooCommerce plugin includes/wc-template-hooks.php core file (line 259):

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

As you can see, the function woocommerce_order_details_table() is hooked in. So now let's find this function that is located in includes/wc-template-functions.php core file (starting line 2584).

As you will see this hooked function call the template file order/order-details.php.

So now you can make some changes:

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

Note: The template file order/order-details.php is also used in Order received (thankyou), so take care to target your changes using the following condition:

// 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 you could also remove this hooked function to replace it by your own custom function, with something like:

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...

Related: WooCommerce action hooks and overriding templates

WooCommerce Documentations:

Upvotes: 5

Related Questions