Woocommerce 如何在我的帐户页面上重定向自定义端点
Woocommerce how to redirect a custom end point on my-account page
这是我上一个问题的延续
登录注册表格必须仅像弹出窗口一样显示,所以我进行了重定向,以避免未登录用户使用默认的我的帐户页面。
我使用该代码从我的帐户页面本身进行重定向,并从该规则中排除 'lost-password',以使未登录的用户能够更新他们的密码。
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();
}
但它不适用于我的帐户中的自定义端点选项卡。如果用户注销,他们可以 return 到上一页并看到必须隐藏的标准登录表单页面。
我是如何创建这个端点的。比如一个,其他的都是同样的方法制作的。
/**
* 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;
}
}
是的,我在创建后将它们保存在 settings > permalinks > save changes
中,因为没有它们显示 404。我只是在 settings > permalinks > save changes
中单击了 'save changes'。
如何将它们包含在该重定向中并保存 'lost-password' 排除?
您混淆了这些重定向规则!尝试以下代码段:
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;
}
}
这是我上一个问题的延续
登录注册表格必须仅像弹出窗口一样显示,所以我进行了重定向,以避免未登录用户使用默认的我的帐户页面。 我使用该代码从我的帐户页面本身进行重定向,并从该规则中排除 'lost-password',以使未登录的用户能够更新他们的密码。
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();
}
但它不适用于我的帐户中的自定义端点选项卡。如果用户注销,他们可以 return 到上一页并看到必须隐藏的标准登录表单页面。
我是如何创建这个端点的。比如一个,其他的都是同样的方法制作的。
/**
* 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;
}
}
是的,我在创建后将它们保存在 settings > permalinks > save changes
中,因为没有它们显示 404。我只是在 settings > permalinks > save changes
中单击了 'save changes'。
如何将它们包含在该重定向中并保存 'lost-password' 排除?
您混淆了这些重定向规则!尝试以下代码段:
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;
}
}