Adding a checkbox in WooCommerce registration form - wordpress

I want to add an custom checkbox for agreeing to terms and condition on a WooCommerce registration form. I did that, but how do I make that checkbox appear between the Password and Register Buttons. Here is a link to my Registration form: https://otpeople.com/my-account/
It's showing there, but I want it on bottom of all fields, and a link to my terms and condition page.
Code:
<?php
function wooc_extra_register_fields() {
?>
<p class="form-row form-row-wide">
<label for="reg_billing_first_name"><?php _e( 'Full Name', 'woocommerce' ); ?><span class="required">*</span></label>
<input type="text" required 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>
<div class="clear"></div>
<p class="form-row form-row-wide">
<label for="reg_billing_phone"><?php _e( 'Mobile Number', 'woocommerce' ); ?><span class="required">*</span></label>
<input type="Number" required class="input-text" name="billing_phone" id="reg_billing_phone" value="<?php if ( ! empty( $_POST['billing_phone'] ) ) esc_attr_e( $_POST['billing_phone'] ); ?>" />
</p>
<p class="form-row form-row-wide">
<label for="reg_num"><?php _e( 'MCI Registration Number', 'woocommerce' ); ?><span class="required">*</span></label>
<input type="text" required class="input-text" name="reg_num" id="reg_num" value="<?php if ( ! empty( $_POST['reg_num'] ) ) esc_attr_e( $_POST['reg_num'] ); ?>" />
</p>
<p class="form-row form-row-wide">
<label for="Year_of_reg"><?php _e( 'MCI Year of Registration', 'woocommerce' ); ?><span class="required">*</span></label>
<input type="Number" required class="input-text" name="Year_of_reg" id="Year_of_reg" value="<?php if ( ! empty( $_POST['Year_of_reg'] ) ) esc_attr_e( $_POST['Year_of_reg'] ); ?>" />
</p>
<p class="form-row form-row-wide">
<label for="qualification"><?php _e( 'Qualification', 'woocommerce' ); ?><span class="required">*</span></label>
<input type="text" required class="input-text" name="qualification" id="qualification" value="<?php if ( ! empty( $_POST['qualification'] ) ) esc_attr_e( $_POST['qualification'] ); ?>" />
</p>
<label for="reg_billing_check_box"><?php _e( 'Agree to Terms & Condition', 'woocommerce' ); ?><span class="required">*</span></label>
<input type="checkbox" class="input-text" name="billink_checkbox" id="reg_billing_first_name" value="<?php if ( ! empty( $_POST['billing_checkbox'] ) ) esc_attr_e( $_POST['billing_checkbox'] ); ?>"/>

I guess you have this start section of registration area with this code
add_action( 'woocommerce_register_form_start', 'wooc_extra_register_fields' );
You can use _end part instead, in that case your checkbox will appear after all fields:
add_action( 'woocommerce_register_form_end', 'wooc_extra_register_fields' );
To make submit button to be at bottom, use this CSS:
.register .woocommerce-Button {position: absolute !important; bottom: 45px;}
.register {padding-bottom:70px !important}

Related

get_product_search_form not showing products in archive_products layout

When i use get_product_search_form i get normal search results - to be precise, i get the products description.
Here's the product_searchform.php code:
<form role="search" method="get" class="woocommerce-product-search" action="<?php echo esc_url( home_url( '/' ) ); ?>">
<input type="submit" class="search-button" value="">
<input type="search" id="woocommerce-product-search-field-<?php echo isset( $index ) ? absint( $index ) : 0; ?>" class="search-field" placeholder="<?php echo esc_attr__( 'Search products…', 'woocommerce' ); ?>" value="<?php echo get_search_query(); ?>" name="s" />
<input type="hidden" name="post_type" value="product" />
</form>

Clicking on register button getting registration page in woocommerce

I am having login form and registration on same page side by side but i want to display only login form and at the bottom i need to display a button as register once i click on register then the register form should be displayed and login form should be hided and in register if i click on login button that form should be displayed.
do_action( 'woocommerce_before_customer_login_form' ); ?>
<?php if ( get_option( 'woocommerce_enable_myaccount_registration' ) === 'yes' ) : ?>
<div class="u-columns col2-set" id="customer_login">
<div class="u-column1 col-1">
<?php endif; ?>
<h2><?php esc_html_e( 'Login', 'woocommerce' ); ?></h2>
<form class="woocommerce-form woocommerce-form-login login" method="post">
<?php do_action( 'woocommerce_login_form_start' ); ?>
<p class="woocommerce-form-row woocommerce-form-row--wide form-row form-row-wide">
<label for="username"><?php esc_html_e( 'Username or email address', 'woocommerce' ); ?> <span class="required">*</span></label>
<input type="text" class="woocommerce-Input woocommerce-Input--text input-text" name="username" id="username" autocomplete="username" value="<?php echo ( ! empty( $_POST['username'] ) ) ? esc_attr( wp_unslash( $_POST['username'] ) ) : ''; ?>" /><?php // #codingStandardsIgnoreLine ?>
</p>
<p class="woocommerce-form-row woocommerce-form-row--wide form-row form-row-wide">
<label for="password"><?php esc_html_e( 'Password', 'woocommerce' ); ?> <span class="required">*</span></label>
<input class="woocommerce-Input woocommerce-Input--text input-text" type="password" name="password" id="password" autocomplete="current-password" />
</p>
<?php do_action( 'woocommerce_login_form' ); ?>
<p class="form-row">
<?php wp_nonce_field( 'woocommerce-login', 'woocommerce-login-nonce' ); ?>
<button type="submit" class="woocommerce-Button button" name="login" value="<?php esc_attr_e( 'Log in', 'woocommerce' ); ?>"><?php esc_html_e( 'Log in', 'woocommerce' ); ?></button>
<a class="woocommerce-Button button" id="registerbutton" >Register</a>
<label class="woocommerce-form__label woocommerce-form__label-for-checkbox inline">
<input class="woocommerce-form__input woocommerce-form__input-checkbox" name="rememberme" type="checkbox" id="rememberme" value="forever" /> <span><?php esc_html_e( 'Remember me', 'woocommerce' ); ?></span>
</label>
</p>
<p class="woocommerce-LostPassword lost_password">
<?php esc_html_e( 'Lost your password?', 'woocommerce' ); ?>
</p>
<?php do_action( 'woocommerce_login_form_end' ); ?>
</form>
<?php if ( get_option( 'woocommerce_enable_myaccount_registration' ) === 'yes' ) : ?>
</div>
<div class="u-column2 col-2" id="registrationforms">
<h2><?php esc_html_e( 'Register', 'woocommerce' ); ?></h2>
<form method="post" class="woocommerce-form woocommerce-form-register register" <?php do_action( 'woocommerce_register_form_tag' ); ?> >
<?php do_action( 'woocommerce_register_form_start' ); ?>
<?php if ( 'no' === get_option( 'woocommerce_registration_generate_username' ) ) : ?>
<p class="woocommerce-form-row woocommerce-form-row--wide form-row form-row-wide">
<label for="reg_username"><?php esc_html_e( 'Username', 'woocommerce' ); ?> <span class="required">*</span></label>
<input type="text" class="woocommerce-Input woocommerce-Input--text input-text" name="username" id="reg_username" autocomplete="username" value="<?php echo ( ! empty( $_POST['username'] ) ) ? esc_attr( wp_unslash( $_POST['username'] ) ) : ''; ?>" /><?php // #codingStandardsIgnoreLine ?>
</p>
<?php endif; ?>
<p class="woocommerce-form-row woocommerce-form-row--wide form-row form-row-wide">
<label for="reg_email"><?php esc_html_e( 'Email address', 'woocommerce' ); ?> <span class="required">*</span></label>
<input type="email" class="woocommerce-Input woocommerce-Input--text input-text" name="email" id="reg_email" autocomplete="email" value="<?php echo ( ! empty( $_POST['email'] ) ) ? esc_attr( wp_unslash( $_POST['email'] ) ) : ''; ?>" /><?php // #codingStandardsIgnoreLine ?>
</p>
<?php if ( 'no' === get_option( 'woocommerce_registration_generate_password' ) ) : ?>
<p class="woocommerce-form-row woocommerce-form-row--wide form-row form-row-wide">
<label for="reg_password"><?php esc_html_e( 'Password', 'woocommerce' ); ?> <span class="required">*</span></label>
<input type="password" class="woocommerce-Input woocommerce-Input--text input-text" name="password" id="reg_password" autocomplete="new-password" />
</p>
<?php endif; ?>
<?php do_action( 'woocommerce_register_form' ); ?>
<p class="woocommerce-FormRow form-row">
<?php wp_nonce_field( 'woocommerce-register', 'woocommerce-register-nonce' ); ?>
<button type="submit" class="woocommerce-Button button" name="register" value="<?php esc_attr_e( 'Register', 'woocommerce' ); ?>"><?php esc_html_e( 'Register', 'woocommerce' ); ?></button>
<a class="woocommerce-Button button" id="loginbutton" >Login</a>
</p>
<?php do_action( 'woocommerce_register_form_end' ); ?>
</form>
</div>
</div>
<?php endif; ?>
<?php do_action( 'woocommerce_after_customer_login_form' ); ?>
Solved by adding JQUERY for that.
<script type="text/javascript">
jQuery(function($) {
// Then hide the second div
jQuery("#registrationform").hide();
// Then add a click handlers to the buttons
jQuery("#registerbutton").click(function() {
jQuery("#registrationform").show();
jQuery("#loginform").hide();
});
jQuery("#loginbutton").click(function() {
jQuery("#registrationform").hide();
jQuery("#loginform").show();
});
})
</script>

Give error if checkbox isn't selected on Woocommerce Account registration

I'm trying to add some custom fields in the Woocommerce User account registration form.
I've succesfully added First Name and last name, nut i'm trying to add a T&C agreement checkbox, that if it isn't selected it doesn't let the user continue the registration.
I've managed to display it correctly the i cannot make the validation work.
What am i doing wrong?
PS. just to know, is it possibile to make these checks live, without needing to reload the page?
Thanks
The code i've used:
add_action( 'woocommerce_register_form_start', 'cbi_custom_woo_account_registration' );
function cbi_custom_woo_account_registration() {
?>
<p class="woocommerce-FormRow woocommerce-FormRow--wide form-row form-row-wide first">
<label for="reg_billing_first_name"><?php _e( 'First name', 'custom-cbi-parts' ); ?> <span class="required">*</span></label>
<input type="text" class="woocommerce-Input woocommerce-Input--text input-text form-control" 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="woocommerce-FormRow woocommerce-FormRow--wide form-row form-row-wide">
<label for="reg_billing_last_name"><?php _e( 'Last name', 'custom-cbi-parts' ); ?> <span class="required">*</span></label>
<input type="text" class="woocommerce-Input woocommerce-Input--text input-text form-control" 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>
<p class="woocommerce-FormRow woocommerce-FormRow--wide form-row form-row-wide">
<label for="accept_terms"><?php _e( 'I agree TeC', 'custom-cbi-parts' ); ?> <span class="required">*</span></label>
<input type="checkbox" class="woocommerce-Input woocommerce-Input--text input-text form-control" name="accept_terms" id="accept_terms" value="<?php if ( ! empty( $_POST['accept_terms'] ) ) esc_attr_e( $_POST['accept_terms'] ); ?>" />
</p>
<div class="clear"></div>
<?php
}
// 2. VALIDATE FIELDS
add_filter( 'woocommerce_registration_errors', 'cbi_validate_custom_reg_fields', 10, 3 );
function cbi_validate_custom_reg_fields( $errors, $username, $email ) {
if ( isset( $_POST['billing_first_name'] ) && empty( $_POST['billing_first_name'] ) ) {
$errors->add( 'billing_first_name_error', __( 'First name is required!', 'custom-cbi-parts' ) );
}
if ( isset( $_POST['billing_last_name'] ) && empty( $_POST['billing_last_name'] ) ) {
$errors->add( 'billing_last_name_error', __( 'Last name is required!.', 'custom-cbi-parts' ) );
}
if ( isset($_POST['accept_terms']) && $_POST['accept_terms'] == 0 ) {
$errors->add( 'accept_terms_error', __( 'You must accept Terms and Conditions!.', 'custom-cbi-parts' ) );
}
return $errors;
}
I have fixed your code.
add_action( 'woocommerce_register_form_start', 'cbi_custom_woo_account_registration' );
function cbi_custom_woo_account_registration() {
?>
<p class="woocommerce-FormRow woocommerce-FormRow--wide form-row form-row-wide first">
<label for="reg_billing_first_name"><?php _e( 'First name', 'custom-cbi-parts' ); ?> <span class="required">*</span></label>
<input type="text" class="woocommerce-Input woocommerce-Input--text input-text form-control" 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="woocommerce-FormRow woocommerce-FormRow--wide form-row form-row-wide">
<label for="reg_billing_last_name"><?php _e( 'Last name', 'custom-cbi-parts' ); ?> <span class="required">*</span></label>
<input type="text" class="woocommerce-Input woocommerce-Input--text input-text form-control" 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>
<p class="woocommerce-FormRow woocommerce-FormRow--wide form-row form-row-wide">
<label for="accept_terms"><?php _e( 'I agree TeC', 'custom-cbi-parts' ); ?> <span class="required">*</span></label>
<input type="checkbox" class="woocommerce-Input woocommerce-Input--text input-text form-control" name="accept_terms" id="accept_terms" <?php if( isset($_POST['accept_terms'])){ echo "checked"; } ?> />
</p>
<div class="clear"></div>
<?php
}
// 2. VALIDATE FIELDS
add_action( 'woocommerce_registration_errors', 'cbi_validate_custom_reg_fields', 10, 3 );
function cbi_validate_custom_reg_fields( $errors, $username, $email ) {
if ( isset( $_POST['billing_first_name'] ) && empty( $_POST['billing_first_name'] ) ) {
$errors->add( 'billing_first_name_error', __( 'First name is required!', 'custom-cbi-parts' ) );
}
if ( isset( $_POST['billing_last_name'] ) && empty( $_POST['billing_last_name'] ) ) {
$errors->add( 'billing_last_name_error', __( 'Last name is required!.', 'custom-cbi-parts' ) );
}
if ( !isset($_POST['accept_terms']) ) {
$errors->add( 'accept_terms_error', __( 'You must accept Terms and Conditions!.', 'custom-cbi-parts' ) );
}
return $errors;
}

Automatically check the “Remember me” box on woocommerce login form?

how can i Automatically check the “Remember me” box on woocommerce login form?
<?php do_action( 'woocommerce_login_form' ); ?>
<p class="form-row">
<?php wp_nonce_field( 'woocommerce-login' ); ?>
<input type="submit" class="button" name="login" value="<?php _e( 'Login', 'woocommerce' ); ?>" />
<label for="rememberme" class="inline">
<input name="rememberme" type="checkbox" id="rememberme" value="forever" /> <?php _e( 'Remember me', 'woocommerce' ); ?>
</label>
</p>
<p class="lost_password">
<?php _e( 'Lost your password?', 'woocommerce' ); ?>
</p>
use checked="checked"
<input name="rememberme" type="checkbox" id="rememberme" value="forever" checked="checked" />

Separate registration page in WooCommerce website

Can anybody help me build a separate registration page in WooCommerce instead of displaying it in the my-account page?
In the my-account page I want to display a link which will take the buyer to the registration page.
edit your form-login.php file and seperate the login form and registration form in two different sections say section A and B.
now check for a GET parameter in the page which will define which section to show. By default login will be shown, if parameter is found and is "register", show registration section
if( isset( $_GET['action']) && $_GET['action'] == "register"){
// Section for registration
}else {
// Section for Login form
}
you can provide a link for registration as
register
The Code for form-login.php
is
--- START SECTION ---
<?php
/**
* Login Form
*
* #author WooThemes
* #package WooCommerce/Templates
* #version 2.2.6
*/
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
?>
<?php wc_print_notices(); ?>
<?php do_action( 'woocommerce_before_customer_login_form' ); ?>
<?php if ( get_option( 'woocommerce_enable_myaccount_registration' ) === 'yes' ) : ?>
<div class="col2-set" id="customer_login">
<div class="col-1">
<?php endif; ?>
<h2><?php _e( 'Login', 'woocommerce' ); ?></h2>
<form method="post" class="login">
<?php do_action( 'woocommerce_login_form_start' ); ?>
<p class="form-row form-row-wide">
<label for="username"><?php _e( 'Username or email address', 'woocommerce' ); ?> <span class="required">*</span></label>
<input type="text" class="input-text" name="username" id="username" value="<?php if ( ! empty( $_POST['username'] ) ) echo esc_attr( $_POST['username'] ); ?>" />
</p>
<p class="form-row form-row-wide">
<label for="password"><?php _e( 'Password', 'woocommerce' ); ?> <span class="required">*</span></label>
<input class="input-text" type="password" name="password" id="password" />
</p>
<?php do_action( 'woocommerce_login_form' ); ?>
<p class="form-row">
<?php wp_nonce_field( 'woocommerce-login' ); ?>
<input type="submit" class="button" name="login" value="<?php esc_attr_e( 'Login', 'woocommerce' ); ?>" />
<label for="rememberme" class="inline">
<input name="rememberme" type="checkbox" id="rememberme" value="forever" /> <?php _e( 'Remember me', 'woocommerce' ); ?>
</label>
</p>
<p class="lost_password">
<?php _e( 'Lost your password?', 'woocommerce' ); ?>
</p>
<?php do_action( 'woocommerce_login_form_end' ); ?>
</form>
<?php if ( get_option( 'woocommerce_enable_myaccount_registration' ) === 'yes' ) : ?>
</div>
<div class="col-2">
<h2><?php _e( 'Register', 'woocommerce' ); ?></h2>
<form method="post" class="register">
<?php do_action( 'woocommerce_register_form_start' ); ?>
<?php if ( 'no' === get_option( 'woocommerce_registration_generate_username' ) ) : ?>
<p class="form-row form-row-wide">
<label for="reg_username"><?php _e( 'Username', 'woocommerce' ); ?> <span class="required">*</span></label>
<input type="text" class="input-text" name="username" id="reg_username" value="<?php if ( ! empty( $_POST['username'] ) ) echo esc_attr( $_POST['username'] ); ?>" />
</p>
<?php endif; ?>
<p class="form-row form-row-wide">
<label for="reg_email"><?php _e( 'Email address', 'woocommerce' ); ?> <span class="required">*</span></label>
<input type="email" class="input-text" name="email" id="reg_email" value="<?php if ( ! empty( $_POST['email'] ) ) echo esc_attr( $_POST['email'] ); ?>" />
</p>
<?php if ( 'no' === get_option( 'woocommerce_registration_generate_password' ) ) : ?>
<p class="form-row form-row-wide">
<label for="reg_password"><?php _e( 'Password', 'woocommerce' ); ?> <span class="required">*</span></label>
<input type="password" class="input-text" name="password" id="reg_password" />
</p>
<?php endif; ?>
<!-- Spam Trap -->
<div style="<?php echo ( ( is_rtl() ) ? 'right' : 'left' ); ?>: -999em; position: absolute;"><label for="trap"><?php _e( 'Anti-spam', 'woocommerce' ); ?></label><input type="text" name="email_2" id="trap" tabindex="-1" /></div>
<?php do_action( 'woocommerce_register_form' ); ?>
<?php do_action( 'register_form' ); ?>
<p class="form-row">
<?php wp_nonce_field( 'woocommerce-register' ); ?>
<input type="submit" class="button" name="register" value="<?php esc_attr_e( 'Register', 'woocommerce' ); ?>" />
</p>
<?php do_action( 'woocommerce_register_form_end' ); ?>
</form>
</div>
</div>
<?php endif; ?>
<?php do_action( 'woocommerce_after_customer_login_form' ); ?>
--- END SECTION ---
Whet can i put this snippet :
--- START SECTION ---
if( isset( $_GET['action']) && $_GET['action'] == "register"){
// Section for registration
}else {
// Section for Login form
}
--- END START
and this URL
--- START SECTION ---
register
--- END SECTION ---
I finally figured out the code to have separate registration and login pages via individual links in the header.
You will need to edit 2 files in your child theme:
functions.php and
form-login.php
I have attached txt files for the code
1) functions.php: You want to create a register link in the header that links to the login page but also sets an action indicating you have clicked register.
So this:
$aux_links_output .= ''. __("Login", "swiftframework") .''. "\n";
becomes this:
$aux_links_output .= ''. __("Login", "swiftframework") .''. "\n";
$aux_links_output .= ''. __("Register", "swiftframework") .''. "\n";
2) form-login.php code:
Here you want to create an if,else statement. If you clicked register then goto register page, else goto login page:
<?php if( isset( $_GET['action']) && $_GET['action'] == "register") : ?>
Section for registration
<?php else : ?>
Section for Login form
<?php endif; ?>
Be careful of the wrappings
Thanks

Resources