Add new extra fields for WooCommerce registration with role set

add_action( ‘woocommerce_register_form_start’, ‘aviweb_extra_register_fields’ );
/**
*
* Validate the extra register fields.
*
*/
function aviweb_validate_extra_register_fields( $username, $email, $validation_errors ) {
if ( isset( $_POST[‘user_type’] ) && empty( $_POST[‘user_type’] ) ) {
$validation_errors->add( ‘user_type_error’, __( ‘<strong>Error</strong>: User type is required!.’, ‘woocommerce’ ) );
}
}
add_action( ‘woocommerce_register_post’, ‘aviweb_validate_extra_register_fields’, 10, 3 );
/**
*
* Save the extra register fields.
*
*/
function aviweb_save_extra_register_fields( $customer_id ) {
if ( isset( $_POST[‘user_type’] ) ) {
update_user_meta( $customer_id, ‘user_type’, sanitize_text_field( $_POST[‘user_type’] ) );
$my_user = new WP_User( $customer_id );
if($_POST[‘user_type’] == “subscriber”){
$my_user->set_role( “subscriber” );
}else if($_POST[‘user_type’] == “customer”){
$my_user->set_role( “customer” );
}
}
}
add_action( ‘woocommerce_created_customer’, ‘aviweb_save_extra_register_fields’ );

Advertisements

Split large SQL file into small files using PHP code

Please find below code to Split large SQL file into small files

$file_handle = fopen(‘sql/file.sql’,’r’); //open big file with fopen
$f = 1; //new file number
while(!feof($file_handle))
{
$newfile = fopen(‘sql/splitfile-‘ . $f . ‘.sql’,’w’); //create new file to write to with file number
for($i = 1; $i <= 3000; $i++) //for 3000 lines
{
$import = fgets($file_handle);
fwrite($newfile,$import);
if(feof($file_handle))
{break;} //If file ends, break loop
}
fclose($newfile);
//MySQL newfile insertion stuff goes here
$f++;
}
fclose($file_handle);