Question

since WooCommerce 2.1 pages such as order-received has been removed and replaced with WC endpoints. My checkout page had a custom page template (page-checkout.php) and now all checkout endpoints are also using this custom page template.

I need to modify my header and footer only when my customers are in the /checkout/ page, but I want to show different content when they are in a checkout endpoints. I have found this conditional:

if(is_wc_endpoint_url("order-received")) echo "yes";

It works when we are in the "order-received" checkout endpoint. But I am looking for a conditional logic that tells me when we are not in an endpoint, something like:

if(!is_wc_endpoint()) echo "yes";

Thank you.

Was it helpful?

Solution 2

Try following function:

function is_wc_endpoint() {
    if ( empty( $_SERVER['REQUEST_URI'] ) ) return false;
    $url = parse_url( $_SERVER['REQUEST_URI'] ); 
    if ( empty( $url['query'] ) ) return false;
    global $wpdb;
    $all_woocommerce_endpoints = array();
    $results = $wpdb->get_results( "SELECT option_name, option_value FROM {$wpdb->prefix}options WHERE option_name LIKE 'woocommerce_%_endpoint'", 'ARRAY_A' );
    foreach ( $results as $result ) {
        $all_woocommerce_endpoints[$result['option_name']] = $result['option_value'];
    }
    foreach ( $all_woocommerce_endpoints as $woocommerce_endpoint ) {
        if ( strpos( $url['query'], $woocommerce_endpoint ) !== false ) {
            return true;
        }
    }
    return false;
}

Hope it'll give you result you are expecting.

OTHER TIPS

The questions seemed to be answered and bit old. But i found a better solution, which may help somebody else.

you can use the following function. Documentation

is_wc_endpoint_url()

if used without a parameter, it will check current url against all endpoints. If an endpoint is specified like

is_wc_endpoint_url('edit-account');

it will check either the url is of specific endpoint or not.

This is a never version of the is_wc_endpoint_url function that will be included in the future woocommerce version. So just give it a different name and put into your functions.php for example.

function is_wc_endpoint_url( $endpoint = false ) {
 global $wp;

 $wc_endpoints = WC()->query->get_query_vars();

 if ( $endpoint ) {
     if ( ! isset( $wc_endpoints[ $endpoint ] ) ) {
         return false;
     } else {
         $endpoint_var = $wc_endpoints[ $endpoint ];
     }

     return isset( $wp->query_vars[ $endpoint_var ] );
 } else {
     foreach ( $wc_endpoints as $key => $value ) {
         if ( isset( $wp->query_vars[ $key ] ) ) {
             return true;
         }
     }
     return false;
 }
}

I tried the default WC function:

is_wc_endpoint_url('my-custom-endpoint');

but it always returned false for me. So I created my own function:

function yourtheme_is_wc_endpoint($endpoint) {
    // Use the default WC function if the $endpoint is not provided
    if (empty($endpoint)) return is_wc_endpoint_url();
    // Query vars check
    global $wp;
    if (empty($wp->query_vars)) return false;
    $queryVars = $wp->query_vars;
    if (
        !empty($queryVars['pagename'])
        // Check if we are on the Woocommerce my-account page
        && $queryVars['pagename'] == 'my-account'
    ) {
        // Endpoint matched i.e. we are on the endpoint page
        if (isset($queryVars[$endpoint])) return true;
        // Dashboard my-account page special check - check whether the url ends with "my-account"
        if ($endpoint == 'dashboard') {
            $requestParts = explode('/', trim($wp->request, ' \/'));
            if (end($requestParts) == 'my-account') return true;
        }
    }
    return false;
}

Example:

yourtheme_is_wc_endpoint('my-custom-endpoint');

Or:

yourtheme_is_wc_endpoint('edit-account');
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top