University Woocommerce Store - Student Number Required on Checkout - wordpress

I have built a Woocommerce store for a university client for the sole purpose of selling study tours to the students twice a year. Client is requesting that on check out (or on sign up) students are required to enter their student number and this number needs to be be approved in the WordPress or Woocommerce database before the system will let them continue.
There are about 100 students that can apply, so I can unload the individual student numbers if needed (no need to link to a secondary system if this makes things easier). The students can only order 1 item from the store, so the item needs to link to their student number and once the sale is made they can not order any other products from the store. I already added single lifetime purchases plugin, but this links to their email addresses and some students then use alternative email addresses to purchase more items when they are not allowed to. They only have 1 unique student number so it should rather link to this number.
I was thinking either:
A) Upload the active student numbers and on checkout verify the number is correct and hasn't been used already, or
B) Make the students register before they can access the site and have the system check the student number is valid before hand.
Can anyone point me in the right direction?

Hands down the best, and most optimal solution for this problem would be your B) choice. Forcing students to enter their student number when registering for the site would be the best option, since the ability to then compare the value saved to their profile with the value entered on checkout would be relatively easy on the front-end side.
I can help you out with a partial solution to your problem, and that's to give you a head start on what you can do hook onto the Woo-commerce registration form to create additional fields as required:
Note: this is an example of how to add Phone Number, First Name, and Last Name to the form.
function wooc_extra_register_fields() {?>
      <p class="form-row form-row-wide">
      <label for="reg_billing_phone"><?php _e( 'Phone', 'woocommerce' ); ?></label>
      <input type="text" class="input-text" name="billing_phone" id="reg_billing_phone" value="<?php esc_attr_e( $_POST['billing_phone'] ); ?>" />
      </p>
      <p class="form-row form-row-first">
      <label for="reg_billing_first_name"><?php _e( 'First name', 'woocommerce' ); ?><span class="required">*</span></label>
      <input type="text" class="input-text" name="billing_first_name" id="reg_billing_first_name" value="<?php if ( ! empty( $_POST['billing_first_name'] ) ) esc_attr_e( $_POST['billing_first_name'] ); ?>" />
      </p>
      <p class="form-row form-row-last">
      <label for="reg_billing_last_name"><?php _e( 'Last name', 'woocommerce' ); ?><span class="required">*</span></label>
      <input type="text" class="input-text" name="billing_last_name" id="reg_billing_last_name" value="<?php if ( ! empty( $_POST['billing_last_name'] ) ) esc_attr_e( $_POST['billing_last_name'] ); ?>" />
      </p>
      <div class="clear"></div>
      <?php
}
add_action( 'woocommerce_register_form_start', 'wooc_extra_register_fields' );
You can easily edit this code and change it to one added input, namely the student number which you're looking for. Hope this helps, good luck!

Related

Why is this piece of code making two fields?

The problem is i need a custom field for my buyers to input some dates in a textarea, and when i make the code for it with a plugin or hardcode a function for it, the code makes 2 input fields as shown in this picture/gyazo, i i cant seem to figure out why :/
https://gyazo.com/97e61de41071324491268258d84f8aae
You can see the code in the next gyazo:
https://gyazo.com/e811efd8b0e9c60b361dc52027693797
I have tried everything that i could figure out with my compatebilities
//Custom Product Option
function custom_field_by_category() {
global $product;
if ( is_product_category( 'work-date' ) ) {
return;
}
?>
<div class="custom-date-field">
<label for="custom-date"><?php _e( 'Engraving (10 characters)', 'iconic' ); ?></label>
<input type="text" id="custom-date" name="dato_oensker" placeholder="<?php _e( 'Dato Ønsker', 'iconic' ); ?>" maxlength="10">
</div>
<?php
}
add_action( 'woocommerce_before_add_to_cart_button', 'custom_field_by_category', 10 );

Add Address fields in seller registration page of dokan plugin

I want to add address fields on seller registration page and also save this fields and updated in seller profile. I am using dokan wordpress plugin for multivendor. Please provide me suggestion and code.
Thanks in advance
Seller can input his address in the seller welcome wizard that comes immediately after registration. But if you still need this in the registration-form you need to override this registration template inside your theme /plugins/dokan/templates/global/seller-registration-form.php
Add the address fields as set in this template :
/plugins/dokan/templates/settings/address-form.php
Then use woocommerce_created_customer hook to save your $POST data accordingly.
For saving address you can check this file as reference :
/plugins/dokan/classes/template-settings.php line: 462
Thanks
<p class="form-row form-group form-row-wide">
<label for="dokan_address[city]"><?php _e( 'City', 'dokan-lite' ); ?> <span class="required">*</span></label>
<input type="text" class="input-text form-control" name="city" id="dokan_address[city]" value="<?php if ( ! empty( $postdata['city'] ) ) echo esc_attr($postdata['dokan_address']); ?>" required="required" />
</p>

check registration custom fields are filed by logged in user woocommerce

In woocommerce customer registration I need to add 2 additional fields (mandatory) year of passing (select box), Name of college (select box).
I need to add these 2 fields to the registration process and prompt existing users to fill them if they try to login.
As in the process will be as follows
User tries to login (need to check whether the above fields are filled by user or not - If yes then make him logged in else proceed to step 2)
Will get registration page / form to fill in the above additional fields.
He will then get logged in to the site.
Thanks in advance
Yes, that can be done.
As per I understood, you want to add some extra fields to your registration page. Here's how:
add_action( 'register_form', 'myplugin_register_form' );
function myplugin_register_form() {
$first_name = ( ! empty( $_POST['first_name'] ) ) ? trim( $_POST['first_name'] ) : '';
?>
<p>
<label for="first_name"><?php _e( 'Full Name', 'mydomain' ) ?><br />
<input type="text" name="first_name" id="first_name" class="input" value="<?php echo esc_attr( wp_unslash( $first_name ) ); ?>" size="25" required /></label>
</p>
<?php
}
Put the code in function.php and there you go.
It will work perfectly.

Woocommerce wc_create_new_customer override on checkout

I am using the guest checkout and have enabled "Automatically generate username from customer email" and "Automatically generate customer password", also I created a hidden input
<input class="input-checkbox" id="createaccount" <?php checked( ( true === $checkout->get_value( 'createaccount' ) || ( true === apply_filters( 'woocommerce_create_account_default_checked', false ) ) ), true) ?> type="checkbox" name="createaccount" value="1" /> <label for="createaccount" class="checkbox"><?php _e( 'Create an account?', 'woocommerce' ); ?></label> , which helps automatically create a user on checkout so customers didn't know that a user has been generated by their email. But if the same user, with the same email tries to fill the same email on the checkout field, receives the message: "An account is already registered with your email address. Please login." , which comes from wc_create_new_customer function inside the /woocommerce/includes/class-wc-checkout.php.
I would like to override wc_create_new_customer function and return the $customer_id for the existing user instead of firing and error for already registered account. Tried hooks and filters, but none of them works. Any help?
Thanks in advance!

How would I add a user registration detail to wp_users?

Here is my predicament right now, I currently run a Minecraft Server as alot of people in my community like to play it. The way I currently have it set up, users who sign up on the website have access to the Minecraft server. Minecraft connects to the database and checks to see if they have registered or if their name is there at all. But the problem with this is that users have to sign up with their Minecraft username, some user;s do not like to do that. Right now I would just like a column in wp_users to display a customizable text field saying Minecraft username.
Sorry if this is a duplicate at all.
Thank you for the help.
Instead of creating a new column in the wp_users table which I would advise against, you can use user metadata. You can update and get usermeta.
If you are wanting to code this yourself I think you will need to create your own signup form and the accompanying PHP file to handle it as I don't think there are the correct hooks to get everything you need, although I could be wrong. I'm happy to run you through this if required.
Otherwise, you could try some plugins, although I've not done this. Perhaps this or this - Note I've not tried either of these and not 100% sure they will do what you are after.
To save and show user meta fields on the users profile, try this in your functions.php file;
<?php
add_action( 'show_user_profile', 'add_user_meta_fields' );
add_action( 'edit_user_profile', 'add_user_meta_fields' );
add_action( 'personal_options_update', 'update_user_meta_fields' );
add_action( 'edit_user_profile_update', 'update_user_meta_fields' );
function update_user_meta_fields( $user_id ) {
if ( !current_user_can( 'edit_user', $user_id ) ) { return false; }
update_user_meta( $user_id, 'mincraftUser', $_POST['mincraftUser'] );
}
function add_user_meta_fields( $user ) { ?>
<h3>Extra Custom Meta Fields</h3>
<table class="form-table">
<tr>
<th><label for="mincraftUser">Twitter User Name</label></th>
<td>
<input type="text" id="mincraftUser" name="mincraftUser" size="20" value="<?php echo esc_attr( get_the_author_meta( 'mincraftUser', $user->ID )); ?>">
<span class="description">Please enter your Twitter Account User name, eg: oneTarek</span>
</td>
</tr>
</table>
<?php }

Resources