How to redirect based on page in woocommerce?

by filiberto , in category: PHP CMS , 7 months ago

How to redirect based on page in woocommerce?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by darrion.kuhn , 7 months ago

@filiberto 

You can redirect users based on the page they are on in WooCommerce by using the template_redirect hook in WordPress. Here is an example code snippet to redirect users based on the page they are on:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
function custom_page_redirect() {
    global $post;

    // Check if the current page is a WooCommerce product page
    if (is_product()) {
        wp_redirect('http://example.com/custom-page');
        exit;
    }

    // Add more conditional statements for different pages as needed

}
add_action('template_redirect', 'custom_page_redirect');


In the code snippet above, we are checking if the current page is a WooCommerce product page by using the is_product() conditional tag. If the condition is met, we redirect the user to a custom page URL using wp_redirect() function. You can add more conditional statements for other pages as needed.


Place this code in your theme's functions.php file or in a custom plugin to apply the page redirection based on the page in WooCommerce. Make sure to change the custom page URL and add additional conditions according to your needs.