wc_template_redirect

Handle redirects before content is output - hooked into template_redirect so is_page works.

Description

wc_template_redirect(); 

Usage

  1. if ( !function_exists( 'wc_template_redirect' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'woocommerce/includes/wc-template-functions.php'; 
  3.  
  4.  
  5. // NOTICE! Understand what this does before running. 
  6. $result = wc_template_redirect(); 
  7.  

Defined (1)

The function is defined in the following location(s).

/includes/wc-template-functions.php  
  1. function wc_template_redirect() { 
  2. global $wp_query, $wp; 
  3.  
  4. if ( ! empty( $_GET['page_id'] ) && '' === get_option( 'permalink_structure' ) && wc_get_page_id( 'shop' ) == $_GET['page_id'] ) { 
  5.  
  6. // When default permalinks are enabled, redirect shop page to post type archive url 
  7. wp_safe_redirect( get_post_type_archive_link( 'product' ) ); 
  8. exit; 
  9.  
  10. } elseif ( 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'] ) ) { 
  11.  
  12. // When on the checkout with an empty cart, redirect to cart page 
  13. wc_add_notice( __( 'Checkout is not available whilst your cart is empty.', woocommerce ), 'notice' ); 
  14. wp_redirect( wc_get_page_permalink( 'cart' ) ); 
  15. exit; 
  16.  
  17. } elseif ( isset( $wp->query_vars['customer-logout'] ) ) { 
  18.  
  19. // Logout 
  20. wp_redirect( str_replace( '&', '&', wp_logout_url( wc_get_page_permalink( 'myaccount' ) ) ) ); 
  21. exit; 
  22.  
  23. } elseif ( is_search() && is_post_type_archive( 'product' ) && apply_filters( 'woocommerce_redirect_single_search_result', true ) && 1 === absint( $wp_query->found_posts ) ) { 
  24.  
  25. // Redirect to the product page if we have a single product 
  26. $product = wc_get_product( $wp_query->post ); 
  27.  
  28. if ( $product && $product->is_visible() ) { 
  29. wp_safe_redirect( get_permalink( $product->get_id() ), 302 ); 
  30. exit; 
  31. } elseif ( is_add_payment_method_page() ) { 
  32.  
  33. // Ensure payment gateways are loaded early 
  34. WC()->payment_gateways(); 
  35.  
  36. } elseif ( is_checkout() ) { 
  37.  
  38. // Checkout pages handling 
  39. // Buffer the checkout page 
  40. ob_start(); 
  41.  
  42. // Ensure gateways and shipping methods are loaded early 
  43. WC()->payment_gateways(); 
  44. WC()->shipping(); 
  45.