18

In Wordpressis_page() can check page by ID,name or by slug but how can we check WooCommerce thank you page whether its a part of checkout page.

Also We have a lot of WooCommerce conditional tags but cant find something solve my issue

for example I try

if(is_page('checkout')) {
   //some thing for only checkout page
}else if(is_page('thankyou') && !is_page('checkout')){
  //some thing for only thank you page but not on checkout page
}else{
  //some thing for all other page
}
Firefog
  • 3,094
  • 7
  • 48
  • 86
  • 9
    Both of `is_order_received_page()` and `is_wc_endpoint_url( 'order-received' )` will work to check if you are on the thankyou page in the frontend. Check [Woo documentation here](https://docs.woocommerce.com/document/woocommerce-endpoints-2-1/#section-1). – Dhaval Shah Sep 08 '18 at 17:38
  • 5
    Definitively use `if( is_wc_endpoint_url( 'order-received' ) ){ … }` see [Woocommerce conditional tags](https://docs.woocommerce.com/document/conditional-tags/#section-11) – LoicTheAztec Sep 08 '18 at 18:04
  • `if ( is_checkout() && !empty( $wp->query_vars['order-received'] ) )` – Firefog Oct 06 '20 at 08:32

2 Answers2

37

This sample code may work:

if ( is_checkout() && !empty( is_wc_endpoint_url('order-received') ) ) {
    ...
}
Kelvin Mariano
  • 991
  • 8
  • 15
  • 1
    It's unnecessary to wrap `empty()` on `is_wc_endpoint_url()` because the `is_wc_endpoint_url()` returns boolean. – Terry Lin Nov 30 '22 at 07:16
3

I think better to use endpoint like

if ( is_wc_endpoint_url( 'order-received' ) ) {
global $wp;
//Get Order ID
$current_order_id =  intval( str_replace( 'checkout/order-received/', '', $wp->request ) );
echo $current_order_id;
}