如何添加 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 中創建單獨的登錄和註冊頁面
