-
-
Save bekarice/a6a0c9310b04fdd2db02 to your computer and use it in GitHub Desktop.
<?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' ); |
Hi,
Where does this files must be placed?
Is there anything else to do? A call to this file?
Thanks
You can add this snippet to your theme's functions.php file
@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
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:)
Hi,
I was exactly searching this how to auto approve woocommerce customers in my site through new user approve plugin.Can you guide me where to add these code of line?