這是我上一個問題的繼續
Woocommerce 如何從模板重定向鉤子中排除 myaccount 的子頁面(端點)?
登錄注冊表只能像彈出視窗一樣顯示,所以我進行了重定向,以避免未登錄用戶的默認我的帳戶頁面。我使用該代碼從我的帳戶頁面本身進行重定向,并從該規則中排除“丟失密碼”,以使未登錄的用戶可以更新他們的密碼。
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();
}
但它不適用于 myaccount 中的自定義端點選項卡。如果用戶退出,他們可以回傳上一頁并看到必須隱藏的標準登錄表單頁面。
我是如何創建這個端點的。例如一個,其他的都是用同樣的方法制作的。
/**
* 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' );
我試圖將它們包含在查詢中,但沒有幫助。我試過有斜線和沒有斜線。
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?
uj5u.com熱心網友回復:
你把那些重定向規則搞混了!嘗試以下代碼段:
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;
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/384913.html
標籤:php wordpress redirect woocommerce woocommerce-theming
