Advertisement
adam_42

Create a vendor on account registration - regular form

Apr 6th, 2015
402
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*This functions will automatically create a vendor shop (when using the plugin  
  2. * http://www.woothemes.com/products/product-vendors/) when the user signs up using the regular form
  3. *  - based on Nicola Mustone's code found on http://nicolamustone.it/2015/01/12/create-a-vendor-on-account-registration/
  4. * @param int $user_id
  5. * @return boid
  6. */
  7.  
  8. <?php
  9.  
  10. add_action( 'user_register', 'custom_registration_save', 10, 1 );
  11.  
  12. function custom_registration_save( $user_id ) {
  13. $user = get_userdata( $user_id );
  14. $username = $user->user_login;
  15. $email = $user->user_email;
  16.  
  17. //$_POST from the register form is two radio buttons with value either 'customer' or 'vendor'
  18. $create_shop = $_POST['accounttype'];
  19.  
  20. if ($create_shop == 'vendor') {
  21.  
  22.  
  23. //removing the default role 'customer' 
  24. $user->remove_role( 'customer' );
  25.  
  26. //adding the a new role 'vendor' to the vendor
  27. $user->add_role( 'vendor' );   
  28.    
  29.    
  30. // Ensure vendor name is unique
  31.     if ( term_exists( $username, 'shop_vendor' ) ) {
  32.         $append     = 1;
  33.         $o_username = $username;
  34.  
  35.         while ( term_exists( $username, 'shop_vendor' ) ) {
  36.             $username = $o_username . $append;
  37.             $append ++;
  38.         }
  39.     }
  40.  
  41. // Create the new vendor
  42.     $return = wp_insert_term(
  43.         $username,
  44.         'shop_vendor',
  45.         array(
  46.             'description' => sprintf( __( 'The vendor shop for %s', 'localization-domain' ), $username ),
  47.             'slug'        => sanitize_title( $username )
  48.         )
  49.     );
  50.    
  51. if ( is_wp_error( $return ) ) {
  52.         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' );
  53.     } else {
  54. // Update vendor data
  55.         $vendor_data['paypal_email'] = $email;
  56.         $vendor_data['commission']   = '50';
  57.         $vendor_data['admins'][]     = $user_id;
  58.         $vendor_data['banner']   = '/wp-content/uploads/banner.png';  
  59.  
  60.         update_option( 'shop_vendor_' . $return['term_id'], $vendor_data );
  61.        
  62.         update_user_meta( $user_id, 'product_vadd_action( 'user_register', 'custom_registration_save', 10, 1 );
  63.  
  64. function custom_registration_save( $user_id ) {
  65. $user = get_userdata( $user_id );
  66. $username = $user->user_login;
  67. $email = $user->user_email;
  68.    
  69. // Ensure vendor name is unique
  70.     if ( term_exists( $username, 'shop_vendor' ) ) {
  71.         $append     = 1;
  72.         $o_username = $username;
  73.  
  74.         while ( term_exists( $username, 'shop_vendor' ) ) {
  75.             $username = $o_username . $append;
  76.             $append ++;
  77.         }
  78.     }
  79.  
  80. // Create the new vendor
  81.     $return = wp_insert_term(
  82.         $username,
  83.         'shop_vendor',
  84.         array(
  85.             'description' => sprintf( __( 'The vendor shop for %s', 'localization-domain' ), $username ),
  86.             'slug'        => sanitize_title( $username )
  87.         )
  88.     );
  89. if ( is_wp_error( $return ) ) {
  90.         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' );
  91.     } else {
  92. // Update vendor data
  93.         $vendor_data['paypal_email'] = $email;
  94.         $vendor_data['commission']   = '50';
  95.         $vendor_data['admins'][]     = $user_id;
  96.        
  97.         update_option( 'shop_vendor_' . $return['term_id'], $vendor_data );
  98.        
  99.         $caps = array(
  100.             "edit_product",
  101.             "read_product",
  102.             "delete_product",
  103.             "edit_products",
  104.             "edit_others_products",
  105.             "delete_products",
  106.             "delete_published_products",
  107.             "delete_others_products",
  108.             "edit_published_products",
  109.             "assign_product_terms",
  110.             "upload_files",
  111.             "manage_bookings",
  112.         );
  113.  
  114.         $skip_review = get_option( 'woocommerce_product_vendors_skip_review' ) == 'yes' ? true : false;
  115.         if( $skip_review ) {
  116.             $caps[] = 'publish_products';
  117.         }
  118.  
  119.         $caps = apply_filters( 'product_vendors_admin_caps', $caps );
  120.  
  121.         foreach( $caps as $cap ) {
  122.             $user->add_cap( $cap );
  123.         }
  124.     }
  125.    
  126. }endor', $return['term_id'] );
  127.        
  128.         $caps = array(
  129.             "publish_product",
  130.             "edit_product",
  131.             "read_product",
  132.             "delete_product",
  133.             "edit_products",
  134.             "edit_others_products",
  135.             "delete_products",
  136.             "edit_tags",
  137.             "delete_published_products",
  138.             "delete_others_products",
  139.             "edit_published_products",
  140.             "assign_product_terms",
  141.             "upload_files",
  142.             "manage_bookings",
  143.         );
  144.  
  145.        
  146.  
  147.         $caps = apply_filters( 'product_vendors_admin_caps', $caps );
  148.  
  149.         foreach( $caps as $cap ) {
  150.             $user->add_cap( $cap );
  151.         }
  152.     }
  153.    
  154.     } else if ($create_vendor == 'customer') {
  155.     exit;
  156.     }
  157.    
  158. }
  159. ?>
Advertisement
RAW Paste Data Copied
Advertisement