Last active
March 7, 2023 11:09
-
-
Save bekarice/a6a0c9310b04fdd2db02 to your computer and use it in GitHub Desktop.
Automatically approve WooCommerce customers with New User Approve (https://wordpress.org/plugins/new-user-approve/)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Integrates WooCommerce with New User Approve | |
* | |
* Automatically approves new customers who register at checkout or on the My Account page, | |
* but holds all other user registrations for approval (i.e., wholesale customers) | |
* | |
* @param int $customer_id the new customer ID | |
*/ | |
function sww_approve_customer_user( $customer_id ) { | |
$roles = get_userdata( $customer_id )->roles; | |
if ( 'pending' == pw_new_user_approve()->get_user_status( $customer_id ) && in_array( 'customer', $roles ) ) { | |
pw_new_user_approve()->update_user_status( $customer_id, 'approve' ); | |
} | |
} | |
add_action( 'woocommerce_created_customer', 'sww_approve_customer_user' ); |
Sounds like you're looking for array_intersect()
instead of in_array()
:
My example:
🤔"Does this person have the customer role?"
if ( 'pending' == pw_new_user_approve()->get_user_status( $customer_id ) && in_array( 'customer', $roles ) ) {
Check for any role:
🤔"Does this person have any one of my desired roles?"
if ( 'pending' == pw_new_user_approve()->get_user_status( $customer_id ) && array_intersect( [ 'customer', 'shop_manager' ], $roles ) ) {
Finally found out that my code was correct, apart last part:
function user_approve( $user_id ) { $roles = get_userdata( $user_id )->roles; if ( 'pending' == pw_new_user_approve()->get_user_status( $user_id ) && in_array( 'user', $roles ) ) { pw_new_user_approve()->update_user_status( $user_id, 'approve' ); } } add_action( 'user_register', 'user_approve', 999, 1 );
Now it works correct! Thank you for reply:)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@bekarice Do you know how to rewrite this function to accept other wordpress roles?
I wrote following code:
function user_approve( $user_id ) { $roles = get_userdata( $user_id )->roles; if ( 'pending' == pw_new_user_approve()->get_user_status( $user_id ) && in_array( 'user', $roles ) ) { pw_new_user_approve()->update_user_status( $user_id, 'approve' ); } } add_action( 'new_user_approve_approve_user', 'user_approve' );
but it seems this doesnt'work