Advertisement
adam_42

create_vendor on regular registration

Apr 3rd, 2015
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.80 KB | None | 0 0
  1. <?php
  2. add_action( 'user_register', 'custom_registration_save', 10, 1 );
  3.  
  4. function custom_registration_save( $user_id ) {
  5. $user = wp_get_current_user( $user_id );
  6. $username = $user->user_login;
  7. $email = $user->user_email;
  8.    
  9. // Ensure vendor name is unique
  10.     if ( term_exists( $username, 'shop_vendor' ) ) {
  11.         $append     = 1;
  12.         $o_username = $username;
  13.  
  14.         while ( term_exists( $username, 'shop_vendor' ) ) {
  15.             $username = $o_username . $append;
  16.             $append ++;
  17.         }
  18.     }
  19.  
  20. // Create the new vendor
  21.     $return = wp_insert_term(
  22.         $username,
  23.         'shop_vendor',
  24.         array(
  25.             'description' => sprintf( __( 'The vendor shop for %s', 'localization-domain' ), $username ),
  26.             'slug'        => sanitize_title( $username )
  27.         )
  28.     );
  29. if ( is_wp_error( $return ) ) {
  30.         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' );
  31.     } else {
  32. // Update vendor data
  33.         $vendor_data['paypal_email'] = $email;
  34.         $vendor_data['commission']   = '50';
  35.         $vendor_data['admins'][]     = $user_id;
  36.  
  37.         update_option( 'shop_vendor_' . $return['term_id'], $vendor_data );
  38.        
  39.         $caps = array(
  40.             "edit_product",
  41.             "read_product",
  42.             "delete_product",
  43.             "edit_products",
  44.             "edit_others_products",
  45.             "delete_products",
  46.             "delete_published_products",
  47.             "delete_others_products",
  48.             "edit_published_products",
  49.             "assign_product_terms",
  50.             "upload_files",
  51.             "manage_bookings",
  52.         );
  53.  
  54.         $skip_review = get_option( 'woocommerce_product_vendors_skip_review' ) == 'yes' ? true : false;
  55.         if( $skip_review ) {
  56.             $caps[] = 'publish_products';
  57.         }
  58.  
  59.         $caps = apply_filters( 'product_vendors_admin_caps', $caps );
  60.  
  61.        
  62.         foreach( $caps as $cap ) {
  63.             $user->add_cap( $cap );
  64.         }
  65.     }
  66. }
  67. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement