Skip to content
Advertisement

Change WooCommerce thankyou page title based on order status

Attempting to change the title of the thank you page based on specific order statuses, by combining a filter and action as follows:

add_action( 'woocommerce_thankyou', 'order_thank_you_status' );

function order_thank_you_status( $order_id ){

    // Get an instance of the `WC_Order` Object
    $order = wc_get_order( $order_id );
    // Get the order number
    $order_number  = $order->get_order_number();
    // Get the order status name
    $status_name  = wc_get_order_status_name( $order->get_status() );
    // Get the order key
    $test_order_key = $order->get_order_key();

    if ( $order->has_status('on-hold') || $order->has_status('mockup-requested') || $order->has_status('mockup-sent')|| $order->has_status('mockup-approved')) {
        echo 'helllo world';

        add_filter( 'the_title', 'woo_title_order_received', 10, 2 );
        function woo_title_order_received( $title, $id ) {
            if ( function_exists( 'is_order_received_page' ) &&
                is_order_received_page() && get_the_ID() === $id ) {
                $title = "Mockup request received";
            }
            return $title;
        }
    }
}

but I have tried multiple ways to combine the two and without any success.

Advertisement

Answer

You can use the following to change “Order received” page title based on order statuses:

add_action( 'the_title', 'change_order_received_page_title_based_on_order_status' );
function change_order_received_page_title_based_on_order_status( $title ){
    if ( is_wc_endpoint_url('order-received') && $title === 'Order received' ) {
        global $wp;

        $targeted_statuses = array('on-hold', 'mockup-requested', 'mockup-sent', 'mockup-approved' );

        // Get an instance of the `WC_Order` Object
        $order = wc_get_order( absint($wp->query_vars['order-received']) );

        if ( is_a( $order, 'WC_Order' ) && in_array( $order->get_status(), $targeted_statuses ) ) {
            return __("Mockup request received", "woocommerce");
        }
    }
    return $title;
}

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

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement