Skip to content

Instantly share code, notes, and snippets.

@davidtowoju
Last active November 14, 2021 12:52
Show Gist options
  • Save davidtowoju/895516b9957123ea7987a68ffd92aea9 to your computer and use it in GitHub Desktop.
Save davidtowoju/895516b9957123ea7987a68ffd92aea9 to your computer and use it in GitHub Desktop.
<?php
/**
* 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