Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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

share|improve this question

2 Answers 2

up vote 1 down vote accepted

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.

share|improve this answer
    
thank you do you know which page would have to be modified to redirect users who view the initial shop page? –  bugz Sep 27 '12 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 Sep 27 '12 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 Sep 27 '12 at 18:10
    
Sorry I made a lazy syntax error, delete that and try this pastie.org/4830960 –  Kyle Sep 27 '12 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 Sep 27 '12 at 18:36

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
share|improve this answer
    
thanks for the recommendation, i will try adding this to the functions.php –  bugz Sep 27 '12 at 14:03

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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