Skip to content

Instantly share code, notes, and snippets.

@bekarice
Last active March 7, 2023 11:09
Show Gist options
  • Save bekarice/a6a0c9310b04fdd2db02 to your computer and use it in GitHub Desktop.
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/)
<?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' );
@Workolyk
Copy link

Workolyk commented May 10, 2017

Hi,
Where does this files must be placed?
Is there anything else to do? A call to this file?

Thanks

@Emad-salah
Copy link

You can add this snippet to your theme's functions.php file

@spacemind-studio
Copy link

@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

@bekarice
Copy link
Author

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 ) ) {

@spacemind-studio
Copy link

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