我的主题 Woocommerce 不同页面上的登录重定向 [英] Login redirection on different pages on my theme Woocommerce

查看:27
本文介绍了我的主题 Woocommerce 不同页面上的登录重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发自己的主题 Woocommerce ,现在我需要在登录我使用 Woocommerce 登录表单的特定页面后进行重定向.为此,我在functions.php 中添加了以下函数

I'm developing my own theme Woocommerce , and now I need to do a redirect after logging in a particular page where I am using Woocommerce login form. To this I added the following function in functions.php

if (!is_account_page()) {
add_filter('woocommerce_login_redirect', 'redirect_after_login_cart');
   function redirect_after_login_cart(){
       wp_redirect( get_permalink( get_page_by_path(‘checkout’) ) );
       exit;
   }
}

我只想要登录购物车页面,登录到结帐页面后进行重定向.my-account 的表单仍然重定向到 my-account 页面.它的方式是,我所有的 Woocommerce 登录表单都重定向到结帐页面.条件未得到遵守.

I want only the login-cart page, do the redirect after login to the checkout page. The form of the my-account is still redirecting to the my-account page. The way it is , all my Woocommerce login forms are redirecting to the checkout page.  The conditional is not being respected.

在进入结帐页面之前,我会检查您是否已登录.如果您已登录,则重定向到结帐,但重定向到使用登录表单创建的页面.登录该页面时,不是转到我的帐户页面,而是重定向到结帐.

Before going to the checkout page, I check if you are logged in or not. If you are logged in, redirect to checkout, but redirects to a page created with the login form. When logging on that page, instead of going to my account page, redirects to the checkout.

默认的 Woocommerce 是每次您登录时,转到我的帐户,只有当您在我创建的登录购物车页面上时,我才会更改此表单

The default Woocommerce is every time you log in, go to my account, and I'm changing this form only if you are on page I created login-cart

add_action('template_redirect','check_if_logged_in');
function check_if_logged_in(){
   if(!is_user_logged_in() && is_checkout()){
    wp_redirect( get_permalink( get_page_by_path('login-cart') ) );
    exit;
   }
}

如果有人可以帮助我,我将不胜感激.

If anyone can help me , I will be very grateful .

推荐答案

我设法解决了我的问题.按照下面的代码:

I managed to solve my problem. Follow the code below:

add_action( 'woocommerce_login_form', 'redirect_user_to_checkout' );
function redirect_user_to_checkout() {
    $referer = get_permalink( get_page_by_path('finalizar-compra'));
if( $referer ) {
    if(!is_account_page()) { ?>
            <input type="hidden" name="redirect-user" value="<?php echo $referer; ?>"><?php
        }
    }
}



function custom_woocommerce_login_redirect_to_checkout_page( $redirect, $user ) {
    if( isset( $_POST['redirect-user'] ) ) {
        $redirect = esc_url( $_POST['redirect-user'] );
    }
    return $redirect;
}
add_filter( 'woocommerce_login_redirect', 'custom_woocommerce_login_redirect_to_checkout_page' );

这篇关于我的主题 Woocommerce 不同页面上的登录重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆