基于页面ID的WooCommerce注册重定向

时间:2020-01-19 作者:Sylvain Roulin

我正在尝试更改WooCommerce注册按钮重定向链接。这是我用于重定向的代码(效果很好):

function custom_registration_redirect() {
        if(is_user_logged_in()) {
            return home_url(\'/checkout/\');
        }
    }

add_action(\'woocommerce_registration_redirect\', \'custom_registration_redirect\', 2);
问题是:我需要根据用户实际所在的页面更改重定向,所以我尝试这样做:

add_action(\'wp\', \'page_check\');
function page_check() {
if (is_page(29179)) {
    function custom_registration_redirect() {
        if(is_user_logged_in()) {
            return home_url(\'/checkout/\');
        }
    }
    add_action(\'woocommerce_registration_redirect\', \'custom_registration_redirect\', 2);
}
但由于某种原因,它不起作用。。。我知道我可能错过了什么,但我不知道。。。

提前感谢您的帮助!:-)

1 个回复
SO网友:Andrea Somovigo

这个woocommerce_registration_redirect 是过滤器挂钩,但不是动作挂钩。我已经尝试了您在上面评论中提到的链接中提供的快捷码,它以这种方式对我有效:

function AS_redirection_after_registration($redirection_url) {

   if($redirection_url =="first-page-slug") // $redirection_url is the slug of the current page where the shortcode is placed
    return home_url(\'/my-page\');
   elseif($redirection_url=="second-page-slug")
    return home_url(\'/my-second-page\');

  return home_url(\'/checkout/\');
}
add_filter( \'woocommerce_registration_redirect\', \'AS_redirection_after_registration\', 10, 1 );
所以$redirection_url 传递给过滤器的参数将为您提供放置短代码的当前页面slug,并在此基础上,您可以决定注册后重定向到哪里。记住在使用过滤器挂钩时总是要返回一些东西,