user6580560
user6580560

Reputation:

Conditionally remove order details section in Woocommerce

I want to remove the order details table in WooCommerce in my functions.php if a if statement is true. I've searched a lot but don't know how to do this.

This is how the file is included in WooCommerce 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,
        ) );
    }
}

So I need something like this:

if ( value != true ) {
    hide_order_details();
}

Upvotes: 2

Views: 2701

Answers (2)

LoicTheAztec
LoicTheAztec

Reputation: 254373

Updated (Optionally showing customer details)

You can simply use the following hooked function (that has the $order_id as available argument) with your condition in an if statement (where you will define $value)

The following will remove the order details table in My account > View order:

add_action( 'woocommerce_view_order', 'custom_action_view_order', 5, 1 );
function custom_action_view_order( $order_id ){
    $value = false;

    if( ! $value ){
        remove_action( 'woocommerce_view_order', 'woocommerce_order_details_table', 10 );

        ## ----- Optionally show customer details (if needed) ----- ##

        if ( ! $order = wc_get_order( $order_id ) ) {
            return;
        }

        if( is_user_logged_in() ){
            wc_get_template( 'order/order-details-customer.php', array( 'order' => $order ) );
        }
    }
}

Code goes in function.php file of your active child theme (or active theme). Tested and works.

1) With customer details:

enter image description here

2) Without customer details:

enter image description here

Upvotes: 2

Mtxz
Mtxz

Reputation: 3879

From what I see, there is no hook you can use in the template.

But you can easily override the order/order-details.php template in your theme to add a condition on whether to output the detail table or not.

The concerned template is in woocommerce/templates/order/order-details.php. You can copy it to your-theme/woocommerce/templates/order/order-details.php and make the required change.

This way, you don't edit original Woocommerce files and use the right way to override woocommerce outputs. Check the order-details template yourself, you'll see there is no hook here permitting to prevent outputting the table. But a simple if wrapper with your condition around the <table> code should do the trick.

Edit: it seems that the filter woocommerce_order_item_visible used in order-details-item.php template can help you to prevent displaying some lines in the order details table. But the template is called within the order detail table html, so you cannot fully remove the table using it.

Note: I'm not sure if this template part is used somewhere else. If it's the case, you should add to your display condition to check if the actual page is the one you want to apply changes on (customer dashboard order detail). If the template is used somewhere else, It'll apply your changes in every places this template is used.

Upvotes: 0

Related Questions