2

I am currently using woocommerce and would like to redirect none logged in users to the home page, however i am having a little trouble determining where i should implements this

I have done research and determined that using the is_user_logged_in() i can see if the user is logged in.

I tryed implementing in the woocommerce/woocommerce-functions.php

// When default permalinks are enabled, redirect shop page to post type archive url
    if (is_user_logged_in() && isset($_GET['page_id']) && $_GET['page_id'] > 0 && get_option( 'permalink_structure' )=="" && $_GET['page_id'] ==     woocommerce_get_page_id('shop') ) :
        wp_safe_redirect( get_post_type_archive_link('product') );
        exit;
    endif;

but it still allows the page to be visible

3 Answers 3

0

You are going to want to insert your code at the top of the page templates for the Woo pages you would like the redirect to affect rather than the functions file. So for example if you want to redirect users when they try to access an individual product page you could put it at the top of the plugins->woocommerce->templates->single-product.php page.

When editing templates though I would recommend making a copy and placing it in your theme directory, so that when Woo updates it will not override any changes you made. So for the example I gave above (applied to my theme). I would copy single-product.php and place it in the themes->agency file and then make the changes to that file, there. Your theme will then always use that file before the one in the Woo template file.

8
  • thank you do you know which page would have to be modified to redirect users who view the initial shop page?
    – bugz
    Commented Sep 27, 2012 at 14:00
  • Since the shop page is actually a loop, you could put a hook in your functions.php file. copy this in and let me know if it works. pastie.org/4830106 . I have some strange stuff on my shop page (I essentially deleted it) so I can't test it myself. If it doesn't work I'll fix it.
    – Kyle
    Commented Sep 27, 2012 at 15:19
  • hmm after adding that to my themes functions.php file the entire site stopped working. was i supposed to add it to woocommerce functions file?
    – bugz
    Commented Sep 27, 2012 at 18:10
  • Sorry I made a lazy syntax error, delete that and try this pastie.org/4830960
    – Kyle
    Commented Sep 27, 2012 at 18:17
  • Okay, I went ahead and tested this code and it definitely works : pastie.org/4831059 . Paste that in the functions.php file
    – Kyle
    Commented Sep 27, 2012 at 18:36
1

First off never edit other peoples plugins or core wordpress files.

Also use braces rather than pretty formatting, as pretty formatting breaks IDE code highlighting, brace matching, syntax checkers and other handy tools

So put this code:

// When default permalinks are enabled, redirect shop page to post type archive url
if (is_user_logged_in() && isset($_GET['page_id']) && $_GET['page_id'] > 0 && get_option( 'permalink_structure' )=="" && $_GET['page_id'] ==     woocommerce_get_page_id('shop') ) {
    wp_redirect( get_post_type_archive_link('product') );
    die();
}

Either in:

  • the top of functions.php
  • A dedicated plugin
  • On the Init hook call
1
  • thanks for the recommendation, i will try adding this to the functions.php
    – bugz
    Commented Sep 27, 2012 at 14:03
0

Use the code functions.php:

add_filter('woocommerce_login_redirect', 'wc_login_redirect');

function wc_login_redirect( $redirect_to ) {
     $redirect_to = 'https://www.example.com/';
     return $redirect_to;
}
1
  • 3
    Please edit your answer and provide some explanation. Dumping a code snippet without explanation is a bad answer. Commented Jul 27, 2015 at 9:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.