Woocommerce how to redirect a custom end point on my-account page
Asked Answered
B

1

3

It's the continue of my previous question

Woocommerce how to exclude the child pages (endpoints) of myaccount from the template redirect hook?

The login-register form has to be shown only like popup, so I've made redirect, to avoid default my-account page for not logged users. I use that code to make redirect from my-account page itself, and to exclude 'lost-password' from that rule, to make possible for non-logged users to renew their passwords.

add_action( 'template_redirect', 'wish_custom_redirect' );

function wish_custom_redirect() {
  global $wp;

  if (
        !is_user_logged_in() 
        &&
        ('my-account' == $wp->request)
        &&
        ('lost-password' != $wp->request)
     ) 
  {
    wp_redirect( home_url() );
    exit;
  }
}
add_action('wp_logout','auto_redirect_after_logout');

function auto_redirect_after_logout(){

  wp_redirect( home_url() );
  exit();
}

But it doesn't work with custom endpoint tabs in myaccount. If user log out, they can return to previous page and see that standard login form page, which has to be hidden.
How I created this endpoints. One for example, others are made by the same way.

/**
 * 1. Register new endpoint slug to use for My Account page
 */

/**
 * @important-note  Resave Permalinks or it will give 404 error
 */
function ts_custom_add_my_returns_endpoint() {
    add_rewrite_endpoint( 'my_returns', EP_ROOT | EP_PAGES );
}
  
add_action( 'init', 'ts_custom_add_my_returns_endpoint' );
  
  
/**
 * 2. Add new query var
 */
  
function ts_custom_my_returns_query_vars( $vars ) {
    $vars[] = 'my_returns';
    return $vars;
}
  
add_filter( 'woocommerce_get_query_vars', 'ts_custom_my_returns_query_vars', 0 );
  
  
/**
 * 3. Insert the new endpoint into the My Account menu
 */
  
function ts_custom_add_my_returns_link_my_account( $items ) {
    $items['my_returns'] = 'My returns';
    return $items;
}
  
add_filter( 'woocommerce_account_menu_items', 'ts_custom_add_my_returns_link_my_account' );
  
  
/**
 * 4. Add content to the new endpoint
 */
  
function ts_custom_my_returns_content() {?>
       <h2 class="text-center woocommerce-products-header__title page-title need-title"><?php echo get_theme_mod('my_returns_tab_heading') ?></h2>
    <section class="order">     
<?php 
$orders = wc_get_orders( array(
  'numberposts' => -1,  
   'orderby' => 'date',
   'order' => 'DESC',  
   'customer_id' => get_current_user_id(),
   'status' => array('refunded','cancelled'),
) );



//* Loop through each WC_Order object
foreach( $orders as $order ){?>
    <div class="order-wrapper product d-flex col-12">
        <?php 
        $order_data = $order->get_data(); // The Order data
        $order_id = $order_data['id'];
        $order_currency = $order_data['currency'];
        $order_status = $order_data['status'];        
         ?>
        <div class="order-number">#<?php echo  $order_id;?></div>         
        <div class="order-inner">
            <div class="order-inner-top">
                <?php    
                foreach ($order->get_items() as $key => $lineItem) {
                $product_id = $lineItem['product_id'];
                $product = wc_get_product( $product_id );
               
                $item_meta_data = $lineItem->get_meta_data();
                $colormeta = $lineItem->get_meta( 'pa_color', true );
                $sizemeta = $lineItem->get_meta( 'pa_size', true ); ?>
                <div class="order-inner-top-inner">
                    <div class="order-slider-inner"> 
                        <div class="order-inner-left">
                            <div class="order-image">
                                <?php echo $product->get_image(['322', '304']);?>                                    
                            </div>
                        </div>
                        <div class="order-inner-right">
                            <div class="order-info-item order-info-item-name">
                                <?php echo $lineItem['name'] ?>
                           </div>
                           <div class="order-info-item order-info-item ">
                                <span class="order-price"><?php echo $lineItem['total'] . $order_currency?></span>
                           </div>
                           <div class="order-item-quantity"><?php esc_html_e( 'Quantity', 'woocommerce' )?>: <?php echo $lineItem['qty']?></div>
                           <!-- <div class="order-item-metas"><?php echo $colormeta . ' , ' . $sizemeta ?></div>-->
                        </div>
                    </div>
                </div>
                <?php }  ?>          
            </div>          
          
          <div class="order-inner-bottom">
               <div class="d-flex justify-content-center">
                    <button class="order-total"><?php echo get_theme_mod('orders_total_button');?></button> 
              </div>
              <div class="totals-toggle">
                  <div class="order-info-item bottom"> <span> <?php esc_html_e( 'Price', 'woocommerce' )?>:</span><span class="order-total-price"> <?php echo $order->get_total() . '  ' . $order_currency; ?></span></div>       
                  <div class="order-info-item bottom"> <span> <?php esc_html_e( 'Quantity', 'woocommerce' )?>:</span> <?php echo  $order->get_item_count(); ?></div>         
                  <div class="order-info-item bottom"> <span><?php esc_html_e( 'Status', 'woocommerce' )?>:</span> <?php

                   if( 'cancelled'== $order->get_status() ) {
                     echo _x( 'Cancelled', 'Order status', 'woocommerce' );                     
                  }                    
                  if( 'refunded'== $order->get_status() ) {
                     echo _x( 'Refunded', 'Order status', 'woocommerce' );                     
                    }
                                
                 
                   ?></div>
                  <div class="order-info-item bottom"> <span><?php esc_html_e( 'Order Date', 'woocommerce' )?></span> <?php 
                    if( $date_created = $order->get_date_created() ){
                        // Display the localized formatted date
                         $formated_date_created = $date_created->date_i18n('d.m.Y ');
                         echo  $formated_date_created;
                    }                   

                   ?></div>
                   <div class="order-info-item bottom"> <span><?php echo get_theme_mod('delivery_date_text')?>: </span> 
                   <?php 
                        // The orders date
                         $date_created = $order->get_date_created();
                         $date_created =  $date_created->date('d.m.Y');
                        // The order date + 5 days
                        $delivery_date = date_i18n( 'd.m.Y', strtotime( $date_created . ' +21 days' ));                       
                        echo $delivery_date;
                    ?>
                     </div>
                </div>
          </div> 

        </div>
       
 </div> 
<?php  }?>

</section>

 <?php  
}
add_action( 'woocommerce_account_my_returns_endpoint', 'ts_custom_my_returns_content' );

I tried to include them in query, but it didn't help. I tried with and without slash.

add_action( 'template_redirect', 'wish_custom_redirect' );

function wish_custom_redirect() {
  global $wp;

  if (
        !is_user_logged_in() 
        &&
        ('my-account' == $wp->request)
         &&
        ('my-account/my_returns' == $wp->request)
         or
         &&
        ('my_returns' == $wp->request)
       /*with and without slash in the end and in the beginning*/
        &&
        ('lost-password' != $wp->request)
     ) 
  {
    wp_redirect( home_url() );
    exit;
  }
}

Yes, I saved them in settings > permalinks > save changes after creating, cause without that they showed 404. I just clicked 'save changes' in settings > permalinks > save changes.

How to include them in that redirect and to save 'lost-password' excluded?

Botulism answered 17/12, 2021 at 15:58 Comment(3)
Can you tell me exactly and briefly, what conditions are you looking for to redirect? I mean spelling them in plain english would be enough. Thanks!Roorback
I also see a typo or in the middle of your snippet.Roorback
'or' I wrote when I posted this, to explain that I tried all with slash and without it. It has to redirect non-logged users from all myaccount endpoints to the home page, but not from 'lost-password' endpoint. all my endpoint tabs 1)/my-account/edit-account/, 2)/my-account/my_bag/, 3)/my-account/orders/ 4)/my-account/my_purchases/ 5)/my-account/my_returns/ 6)/my-account/my_wishlist/ 7)/my-account/where_is_my_order/ 8)/my-account/how_to_return/ 9)/my-account/need_to_contact/ But the separate PAGE(it's NOT a tab) /my-account/lost-password/has to be without that redirect to the home page.Botulism
R
3

You've got those redirect rules mixed up! Try the following snippet:

add_action('template_redirect', 'wish_custom_redirect_extended');

function wish_custom_redirect_extended()
{
    global $wp, $wp_query;

    if (
        !is_user_logged_in()
        &&
        ('my-account' == $wp->request)
        ||
        ('my-account/my_returns' == $wp->request)
        &&
        ('lost-password' != $wp->request)
    ) {
        wp_safe_redirect(site_url());
        exit;
    }
}
Roorback answered 17/12, 2021 at 16:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.