如何添加 WooCommerce 注册电子邮件验证
已发表: 2021-05-24
您要添加自定义 WooCommerce 注册电子邮件验证吗? 请继续阅读,因为这篇文章将为您提供一个简单的解决方案。
添加 WooCommerce 注册电子邮件验证并不是一个复杂的过程。 但是,您可能需要一些基本的编码技能才能实现这一目标。 是的,您可以使用插件,但它们可能会使您的网站膨胀。 这也是一种安全的定制方式。
我们还建议创建一个子主题。 这将确保您的更改在更新期间不会丢失。
添加 WooCommerce 注册电子邮件验证
在这篇文章的最后,您将能够添加 WooCommerce 注册电子邮件验证。 我们创建了一个自定义代码片段来实现这一点。 我们将引导您完成您需要遵循的所有步骤,以使初学者更容易实施此解决方案。
让我们直接进入它。
添加 WooCommerce 注册电子邮件验证的步骤
在继续之前,请记住备份您的站点。 如果出现问题,这将帮助您恢复到以前的版本。
以下是您需要遵循的简单步骤:
- 登录您的 WordPress 站点并以管理员用户身份访问仪表板。
- 从仪表板菜单中,单击外观菜单 > 主题编辑器菜单。 打开主题编辑器页面后,查找主题函数文件,我们将在其中添加将在 WooCommerce 中的产品上添加 GTIN 编号功能的函数。
- 将以下代码添加到php文件中:
// this is just to prevent the user log in automatically after register
function wc_registration_redirect( $redirect_to ) {
wp_logout();
wp_redirect( '/sign-in/?q=');
exit;
}
// when user login, we will check whether this guy email is verify
function wp_authenticate_user( $userdata ) {
$isActivated = get_user_meta($userdata->ID, 'is_activated', true);
if ( !$isActivated ) {
$userdata = new WP_Error(
'inkfool_confirmation_error',
__( '<strong>ERROR:</strong> Your account has to be activated before you can login. You can resend by clicking <a href="/sign-in/?u='.$userdata->ID.'">here</a>', 'inkfool' )
);
}
&return $userdata;
}
// when a user register we need to send them an email to verify their account
function my_user_register($user_id) {
// get user data
$user_info = get_userdata($user_id);
// create md5 code to verify later
$code = md5(time());
// make it into a code to send it to user via email
$string = array('id'=>$user_id, 'code'=>$code);
// create the activation code and activation status
update_user_meta($user_id, 'is_activated', 0);
update_user_meta($user_id, 'activationcode', $code);
; // create the url
$url = get_site_url(). '/sign-in/?p=' .base64_encode( serialize($string));
// basically we will edit here to make this nicer
$html = 'Please click the following links <br/><br/> <a href="'.$url.'">'.$url.'</a>';
// send an email out to user
wc_mail($user_info->user_email, __('Please activate your account'), $html);
}
// we need this to handle all the getty hacks i made
function my_init(){
// check whether we get the activation message
if(isset($_GET['p'])){
$data = unserialize(base64_decode($_GET['p']));
$code = get_user_meta($data['id'], 'activationcode', true);
// check whether the code given is the same as ours
if($code == $data['code']){
// update the db on the activation process
update_user_meta($data['id'], 'is_activated', 1);
wc_add_notice( __( '<strong>Success:</strong> Your account has been activated! ', 'inkfool' )  );
}else{
wc_add_notice( __( '<strong>Error:</strong> Activation fails, please contact our administrator. ', 'inkfool' )  );
}
}
if(isset($_GET['q'])){
wc_add_notice( __( '<strong>Error:</strong> Your account has to be activated before you can login. Please check your email.', 'inkfool' ) );
}
if(isset($_GET['u'])){
my_user_register($_GET['u']);
wc_add_notice( __( '<strong>Succes:</strong> Your activation email has been resend. Please check your email.', 'inkfool' ) );
}
}
// hooks handler
add_action( 'init', 'my_init' );
add_filter('woocommerce_registration_redirect', 'wc_registration_redirect');
add_filter('wp_authenticate_user', 'wp_authenticate_user',10,2);
add_action('user_register', 'my_user_register',10,2);
结论
到目前为止,您商店中的客户应该在注册后验证他们的电子邮件,以便他们在您的商店中购买产品。 此代码已经过测试并且运行良好。

如果您在实施此解决方案时遇到问题,您应该让专业人员为您插入代码或使用插件。
我们希望此解决方案能帮助您添加 WooCommerce 注册电子邮件验证。
类似文章
- 用于在线预订的 30 多个最佳 WordPress 预订插件
- 如何在 WooCommerce 中添加愿望清单
- 如何在不使用插件的情况下在 WordPress 中创建数字分页
- 如何更改店面主题标语 WooCommerce
- 如何在 WooCommerce 社交登录中添加社交登录
- 如何在 WooCommerce 中添加查看购物车按钮
- 如何在 CSS 中选择除最后一个子元素之外的所有元素 » CSS 不是最后一个子元素示例
- 如何在 WooCommerce 上安装 Facebook Pixel
- 如何在 WooCommerce 中发送有关状态更改的电子邮件
- 如何在 WooCommerce 中更改电子邮件模板
- 如何检查用户是否登录WordPress
- 如何在 WordPress WooCommerce 中删除未分类的类别
- 如何设置 WooCommerce 买一送一
- 如何在 WooCommerce 中添加产品 SKU
- 如何删除产品类别标题 WooCommerce
- 30 多个最佳 WordPress 搜索插件,可改善您的网站搜索和 SEO
- 如何在 WooCommerce 中创建单独的登录和注册页面
