Instantly share code, notes, and snippets.

Embed
What would you like to do?
/**
* Turn all subscribers into Woo Vendors
*/
function figarts_make_customers_vendors( $user_id, $user_data ) {
if (!defined('WC_PRODUCT_VENDORS_TAXONOMY')){
return;
}
$username = $user_data['user_login'];
$email = $user_data['user_email'];
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' );
}
// 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