Home > Software engineering >  Add text in woocommerce_thankyou if order status is complete
Add text in woocommerce_thankyou if order status is complete

Time:12-24

I need to add a custom text that is displayed on the thank you page, if the order has been processed and has a completed status.

I am using the following code in the "woocommerce_order_details_before_order_table" hook and it works perfectly, but when I use it in the "woocommerce_thankyou" hook it generates errors.

add_action( 'woocommerce_thankyou', 'complete_custom_text' );

function complete_custom_text( $order ) {
    $status = $order->get_status();
    if(($status === 'completed')){
    echo '<p><b>Text (Custom) 5:</b> Perfect.</p>';
    }
}

If someone could help me I would appreciate it.

CodePudding user response:

The woocommerce_thankyou hook passes the order ID as an argument, not the order object itself. You can use the wc_get_order function to retrieve the order object from the order ID:

add_action( 'woocommerce_thankyou', 'complete_custom_text' );

function complete_custom_text( $order_id ) {
  $order = wc_get_order( $order_id );
  $status = $order->get_status();
  if(($status === 'completed')){
    echo '<p><b>Text (Custom) 5:</b> Perfect.</p>';
  }
}

This should allow you to use the woocommerce_thankyou hook to display your custom text on the thank you page for completed orders.

CodePudding user response:

You are receiving order_id in that hook, use $order = wc_get_order( $order_id ); to get the order before reading the status.

  • Related