Created
January 18, 2018 13:19
-
-
Save davidtowoju/81cef2ab5fc766043921445daaa9d405 to your computer and use it in GitHub Desktop.
Create WooCommerce vendor managers on user registration
This file contains 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
/** | |
* Turn all subscribers into Woo Vendors | |
*/ | |
function figarts_make_users_vendors( $user_id ) { | |
if (!defined('WC_PRODUCT_VENDORS_TAXONOMY')){ | |
return; | |
} | |
if (empty($user_id)) { | |
wc_add_notice( __( '<strong>ERROR</strong>: Unable to create the vendor account for this user. Please contact the administrator to register your account.', 'localization-domain' ), 'error' ); | |
} | |
$user_data = get_userdata($user_id); | |
$username = $user_data->user_login; | |
$email = $user_data->user_email; | |
// Ensure vendor name is unique | |
if ( term_exists( $username, WC_PRODUCT_VENDORS_TAXONOMY ) ) { | |
$append = 1; | |
$o_username = $username; | |
while ( term_exists( $username, WC_PRODUCT_VENDORS_TAXONOMY ) ) { | |
$username = $o_username . $append; | |
$append ++; | |
} | |
} | |
// Create the new vendor | |
$term = wp_insert_term( | |
$username, | |
WC_PRODUCT_VENDORS_TAXONOMY, | |
array( | |
'description' => sprintf( __( 'The vendor %s', 'localization-domain' ), $username ), | |
'slug' => sanitize_title( $username ) | |
) | |
); | |
if ( is_wp_error( $return ) ) { | |
wc_add_notice( __( '<strong>ERROR</strong>: Unable to create the vendor account for this user. Please contact the administrator to register your account.', 'localization-domain' ), 'error' ); | |
} else { | |
// Update vendor data | |
$vendor_data['paypal'] = $email; // The email used for the account will be used for the payments | |
$vendor_data['commission'] = ''; // The commission is 50% for each order | |
$vendor_data['admins'][] = $user_id; // The registered account is also the admin of the vendor | |
$vendor_data['enable_bookings'] = 'yes'; // If you want vendors to access "Bookings" menu | |
update_term_meta( $term['term_id'], 'vendor_data', apply_filters( 'wcpv_registration_default_vendor_data', $vendor_data ) ); | |
// change this user's role to pending vendor | |
wp_update_user( apply_filters( 'wcpv_registration_default_user_data', array( | |
'ID' => $user_id, | |
'role' => 'wc_product_vendors_manager_vendor', | |
) ) ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment