wc_template_redirect()
Handle redirects before content is output – hooked into template_redirect so is_page works.
Description Description
Source Source
File: includes/wc-template-functions.php
function wc_template_redirect() { global $wp_query, $wp; // When default permalinks are enabled, redirect shop page to post type archive url. if ( ! empty( $_GET['page_id'] ) && '' === get_option( 'permalink_structure' ) && wc_get_page_id( 'shop' ) === absint( $_GET['page_id'] ) && get_post_type_archive_link( 'product' ) ) { // WPCS: input var ok, CSRF ok. wp_safe_redirect( get_post_type_archive_link( 'product' ) ); exit; } // When on the checkout with an empty cart, redirect to cart page. if ( is_page( wc_get_page_id( 'checkout' ) ) && wc_get_page_id( 'checkout' ) !== wc_get_page_id( 'cart' ) && WC()->cart->is_empty() && empty( $wp->query_vars['order-pay'] ) && ! isset( $wp->query_vars['order-received'] ) && ! is_customize_preview() && apply_filters( 'woocommerce_checkout_redirect_empty_cart', true ) ) { wc_add_notice( __( 'Checkout is not available whilst your cart is empty.', 'woocommerce' ), 'notice' ); wp_safe_redirect( wc_get_cart_url() ); exit; } // Logout. if ( isset( $wp->query_vars['customer-logout'] ) && ! empty( $_REQUEST['_wpnonce'] ) && wp_verify_nonce( sanitize_key( $_REQUEST['_wpnonce'] ), 'customer-logout' ) ) { // WPCS: input var ok, CSRF ok. wp_safe_redirect( str_replace( '&', '&', wp_logout_url( wc_get_page_permalink( 'myaccount' ) ) ) ); exit; } // Redirect to the correct logout endpoint. if ( isset( $wp->query_vars['customer-logout'] ) && 'true' === $wp->query_vars['customer-logout'] ) { wp_safe_redirect( esc_url_raw( wc_get_account_endpoint_url( 'customer-logout' ) ) ); exit; } // Trigger 404 if trying to access an endpoint on wrong page. if ( is_wc_endpoint_url() && ! is_account_page() && ! is_checkout() && apply_filters( 'woocommerce_account_endpoint_page_not_found', true ) ) { $wp_query->set_404(); status_header( 404 ); include get_query_template( '404' ); exit; } // Redirect to the product page if we have a single product. if ( is_search() && is_post_type_archive( 'product' ) && apply_filters( 'woocommerce_redirect_single_search_result', true ) && 1 === absint( $wp_query->found_posts ) ) { $product = wc_get_product( $wp_query->post ); if ( $product && $product->is_visible() ) { wp_safe_redirect( get_permalink( $product->get_id() ), 302 ); exit; } } // Ensure gateways and shipping methods are loaded early. if ( is_add_payment_method_page() || is_checkout() ) { // Buffer the checkout page. ob_start(); // Ensure gateways and shipping methods are loaded early. WC()->payment_gateways(); WC()->shipping(); } }