Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*This functions will automatically create a vendor shop (when using the plugin
- * http://www.woothemes.com/products/product-vendors/) when the user signs up using the regular form
- * - based on Nicola Mustone's code found on http://nicolamustone.it/2015/01/12/create-a-vendor-on-account-registration/
- * @param int $user_id
- * @return boid
- */
- <?php
- add_action( 'user_register', 'custom_registration_save', 10, 1 );
- function custom_registration_save( $user_id ) {
- $user = get_userdata( $user_id );
- $username = $user->user_login;
- $email = $user->user_email;
- //$_POST from the register form is two radio buttons with value either 'customer' or 'vendor'
- $create_shop = $_POST['accounttype'];
- if ($create_shop == 'vendor') {
- //removing the default role 'customer'
- $user->remove_role( 'customer' );
- //adding the a new role 'vendor' to the vendor
- $user->add_role( 'vendor' );
- // Ensure vendor name is unique
- if ( term_exists( $username, 'shop_vendor' ) ) {
- $append = 1;
- $o_username = $username;
- while ( term_exists( $username, 'shop_vendor' ) ) {
- $username = $o_username . $append;
- $append ++;
- }
- }
- // Create the new vendor
- $return = wp_insert_term(
- $username,
- 'shop_vendor',
- array(
- 'description' => sprintf( __( 'The vendor shop for %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'] = $email;
- $vendor_data['commission'] = '50';
- $vendor_data['admins'][] = $user_id;
- $vendor_data['banner'] = '/wp-content/uploads/banner.png';
- update_option( 'shop_vendor_' . $return['term_id'], $vendor_data );
- update_user_meta( $user_id, 'product_vadd_action( 'user_register', 'custom_registration_save', 10, 1 );
- function custom_registration_save( $user_id ) {
- $user = get_userdata( $user_id );
- $username = $user->user_login;
- $email = $user->user_email;
- // Ensure vendor name is unique
- if ( term_exists( $username, 'shop_vendor' ) ) {
- $append = 1;
- $o_username = $username;
- while ( term_exists( $username, 'shop_vendor' ) ) {
- $username = $o_username . $append;
- $append ++;
- }
- }
- // Create the new vendor
- $return = wp_insert_term(
- $username,
- 'shop_vendor',
- array(
- 'description' => sprintf( __( 'The vendor shop for %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'] = $email;
- $vendor_data['commission'] = '50';
- $vendor_data['admins'][] = $user_id;
- update_option( 'shop_vendor_' . $return['term_id'], $vendor_data );
- $caps = array(
- "edit_product",
- "read_product",
- "delete_product",
- "edit_products",
- "edit_others_products",
- "delete_products",
- "delete_published_products",
- "delete_others_products",
- "edit_published_products",
- "assign_product_terms",
- "upload_files",
- "manage_bookings",
- );
- $skip_review = get_option( 'woocommerce_product_vendors_skip_review' ) == 'yes' ? true : false;
- if( $skip_review ) {
- $caps[] = 'publish_products';
- }
- $caps = apply_filters( 'product_vendors_admin_caps', $caps );
- foreach( $caps as $cap ) {
- $user->add_cap( $cap );
- }
- }
- }endor', $return['term_id'] );
- $caps = array(
- "publish_product",
- "edit_product",
- "read_product",
- "delete_product",
- "edit_products",
- "edit_others_products",
- "delete_products",
- "edit_tags",
- "delete_published_products",
- "delete_others_products",
- "edit_published_products",
- "assign_product_terms",
- "upload_files",
- "manage_bookings",
- );
- $caps = apply_filters( 'product_vendors_admin_caps', $caps );
- foreach( $caps as $cap ) {
- $user->add_cap( $cap );
- }
- }
- } else if ($create_vendor == 'customer') {
- exit;
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement