简体   繁体   English

Woocommerce 如何在我的帐户页面上重定向自定义端点

[英]Woocommerce how to redirect a custom end point on my-account page

It's the continue of my previous question这是我上一个问题的继续

Woocommerce how to exclude the child pages (endpoints) of myaccount from the template redirect hook? Woocommerce 如何从模板重定向挂钩中排除 myaccount 的子页面(端点)?

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.但它不适用于 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 .是的,我将它们保存在settings > permalinks > save changes ,因为没有它们显示404。我只是在settings > permalinks > save changes中单击“保存更改”。

How to include them in that redirect and to save 'lost-password' excluded?如何将它们包含在该重定向中并排除“丢失密码”?

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;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM