Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Code snippet that automatically redirects user to login page if user tries to add tickets without being logged in.
- Code should go to your child theme functions.php file.
- */
- add_action( 'woocommerce_add_to_cart_validation', 'custom_redirect_to_login_page', 5, 4);
- function custom_redirect_to_login_page($pass, $product_id, $quantity, $variation_id = 0){
- if ( is_user_logged_in() ){
- return $pass;
- }
- if ( function_exists( 'wc_get_product' ) ) {
- $product = wc_get_product( $product_id );
- } else {
- $product = new WC_Product( $product_id );
- }
- if ( $product->get_type() == 'lottery' ) {
- $max_tickets_per_user = $product->get_max_tickets_per_user() ? $product->get_max_tickets_per_user() : false;
- if ( $max_tickets_per_user == false OR $max_tickets_per_user == $product->get_max_tickets() ) {
- return true;
- }
- $redirect = add_query_arg( 'redirect_to', get_permalink( $product->get_id() ), get_permalink( get_option( 'woocommerce_myaccount_page_id' ) ) );
- wp_redirect( $redirect );
- exit();
- }
- }
- function custom_login_redirect( $redirect_to ){
- if( isset( $_REQUEST['redirect_to'] ) ) {
- return $_REQUEST['redirect_to'];
- } else {
- return $redirect_to;
- }
- }
- add_filter( 'login_redirect', 'custom_login_redirect');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement