woocommerceendpoints

How to check that we are not in a Woocommerce endpoint


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.


Solution

  • 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.