在woocommerce网站登录和注册 [英] Login and Register in woocommerce site

查看:32
本文介绍了在woocommerce网站登录和注册的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 Wordpress 5.2 和 Woocommerce 3.6.2.我需要当访问者到达站点时,在菜单栏中访问者会看到登录"和注册"选项.一旦用户在网站上注册或登录,登录"和注册"选项就会消失,网站会在购物车"、结束购买"选项旁边的下拉菜单中显示我的帐户"选项,退出"

I use Wordpress 5.2 with Woocommerce 3.6.2. I need when the visitor reaches the site, in the menu bar the visitant see the options "Login" and "Register". Once the user registers or login on the site, the options "Login" and "Register" disappear and the site show the options of "My Account" as a drop-down menu next to the options "Cart", "End Purchase", "Logout"

我怎样才能做到这一点?我正在寻找信息,但我还没有给出解决方案.

How can I achieve this? I'm looking for information but I do not give yet with a solution.

推荐答案

您需要使用以下内容扩展您的(子)主题 functions.php:

You'll need to extend your (child-) themes functions.php with the following:

function so_loginout_menu_links( $items, $args ) {
   if ( $args->theme_location == 'primary' && function_exists('is_woocommerce') ) {
      if (is_user_logged_in()) {
         $items .= '<li><a href="'. wp_logout_url() .'">'. __("Log Out") .'</a></li>';
         $items .= '<li><a href="'. get_permalink( get_option('woocommerce_myaccount_page_id') ).'">'. __("My Account") .'</a></li>';
      } else {
         $items .= '<li><a href="'. wp_registration_url() .'">'. __("Log In") .'</a></li>';
         $items .= '<li><a href="'. site_url('/wp-login.php?action=register').'">'. __("Register") .'</a></li>';
      }
   }
   return $items;
}
add_filter( 'wp_nav_menu_items', 'so_loginout_menu_links', 10, 2 );

根据用户是否登录,这通过过滤来扩展菜单主要"有两个链接.我还添加了对 woocommerce 的检查,因为帐户页面是 woocommerce 特定的,如果它被停用,可能会引发错误.

This extends the menu "primary" with two links by filtering it, dependant on whether the user is logged in or not. I've added a check for woocommerce as well, as the account page is woocommerce specific and might throw errors in case it's deactivated.

上面代码中的primary"可能需要替换为primary-menu"、top"或任何你的主题主导航菜单.

"primary" in the code above might need to be replaced with "primary-menu", "top" or whatever your themes primary navigation menu is called.

还要确保管理>设置>常规>会员资格任何人都可以注册"被选中,否则链接可能不会显示.

Also make sure that Administration > Settings > General > Membership "Anyone can register" is checked, otherwise the link may not be shown.

请注意,我的示例中现在没有额外的 CSS,因此额外的项目尚未出现在下拉列表或任何内容中.为此,请检查您现有的菜单项,并相应地将相应的类和其他顶级项添加到上面的代码中.

Note there's no additional CSS in my example now, so the additional items will not yet be in a dropdown or anything. To get that, inspect your existing menu items and add the according classes and additional top level items into the code above accordingly.

这篇关于在woocommerce网站登录和注册的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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