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 ><?php echo get_theme_mod('my_returns_tab_heading') ?></h2>
<section >
<?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 >
<?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 >#<?php echo $order_id;?></div>
<div >
<div >
<?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 >
<div >
<div >
<div >
<?php echo $product->get_image(['322', '304']);?>
</div>
</div>
<div >
<div >
<?php echo $lineItem['name'] ?>
</div>
<div >
<span ><?php echo $lineItem['total'] . $order_currency?></span>
</div>
<div ><?php esc_html_e( 'Quantity', 'woocommerce' )?>: <?php echo $lineItem['qty']?></div>
<!-- <div ><?php echo $colormeta . ' , ' . $sizemeta ?></div>-->
</div>
</div>
</div>
<?php } ?>
</div>
<div >
<div >
<button ><?php echo get_theme_mod('orders_total_button');?></button>
</div>
<div >
<div > <span> <?php esc_html_e( 'Price', 'woocommerce' )?>:</span><span > <?php echo $order->get_total() . ' ' . $order_currency; ?></span></div>
<div > <span> <?php esc_html_e( 'Quantity', 'woocommerce' )?>:</span> <?php echo $order->get_item_count(); ?></div>
<div > <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 > <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 > <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?
CodePudding user response:
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;
}
}