Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save tradesouthwest/966008d943be20c6f4f490bf562d519f to your computer and use it in GitHub Desktop.
Save tradesouthwest/966008d943be20c6f4f490bf562d519f to your computer and use it in GitHub Desktop.
WooCommerce add checkbox to Registration Form
<?php
/***
SETUP
custom fields
@hook woocommerce_register_form_start
@hook woocommerce_edit_account_form
@hook woocommerce_save_account_detail
@altUse woocommerce_after_order_notes to add to checkout
****/
// -------------------
// 1. Show field @ My Account Registration
add_action( 'woocommerce_register_form_start', 'celedonia_add_name_woo_account_registration' );
function celedonia_add_name_woo_account_registration() {
?>
<p class="form-row form-rwo-first"><span style="float:left;width:47%">
<label for="reg_billing_first_name"><?php _e( 'Nombre', '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'] ); ?>" />
</span>
<span style="width:47%;float:right;display:block;">
<label for="reg_billing_last_name"><?php _e( 'Apellidos', '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'] ); ?>" />
</span></p>
<div class="clear"></div>
<br>
<?php
}
add_action( 'woocommerce_register_form', 'celedonia_extra_register_checkbox_field' );
function celedonia_extra_register_checkbox_field() {
?>
<p class="form-row form-row-wide">
<label for="cel_news" style="color:#419a84">
<input type="hidden" name="cel_news" id="cel_news" value="no" />
<input type="checkbox" name="cel_news" id="cel_news" value="si" />
<?php _e( 'Recibiré quincenalmente los trabajos más actuales, ofertas y el magazine ',
'woocommerce' ); ?></label>
</p>
<?php
}
// -------------------
// 2. Save field on Customer Created action
add_action( 'woocommerce_created_customer', 'celedonia_save_extra_register_checkbox_field' );
function celedonia_save_extra_register_checkbox_field( $customer_id ) {
if ( isset( $_POST['cel_news'] ) ) {
update_user_meta( $customer_id, 'cel_news', $_POST['cel_news'] );
}
if ( isset( $_POST['billing_first_name'] ) ) {
update_user_meta( $customer_id, 'billing_first_name', sanitize_text_field( $_POST['billing_first_name'] ) );
update_user_meta( $customer_id, 'first_name', sanitize_text_field($_POST['billing_first_name']) );
}
if ( isset( $_POST['billing_last_name'] ) ) {
update_user_meta( $customer_id, 'billing_last_name', sanitize_text_field( $_POST['billing_last_name'] ) );
update_user_meta( $customer_id, 'last_name', sanitize_text_field($_POST['billing_last_name']) );
}
}
// -------------------
// 3. Display Select Field @ User Profile (admin) and My Account Edit page (front end)
add_action( 'show_user_profile', 'celedonia_show_extra_register_checkbox_field', 30 );
add_action( 'edit_user_profile', 'celedonia_show_extra_register_checkbox_field', 30 );
add_action( 'woocommerce_edit_account_form', 'celedonia_show_extra_register_checkbox_field', 30 );
function celedonia_show_extra_register_checkbox_field($user){
if (empty ($user) ) {
$user_id = get_current_user_id();
$user = get_userdata( $user_id );
}
?>
<p class="form-row form-row-wide">
<label for="cel_news">
<input type="hidden" name="cel_news" id="cel_news" value="no" />
<input type="checkbox" name="cel_news" id="cel_news" value="si" />
<?php _e( 'Recibiré quincenalmente los trabajos más actuales, ofertas y el magazine',
'woocommerce' ); ?></label>
</p>
<?php
}
// -------------------
// 4. Save User Field When Changed From the Admin/Front End Forms
add_action( 'personal_options_update', 'celedonia_save_extra_register_checkbox_field_admin' );
add_action( 'edit_user_profile_update', 'celedonia_save_extra_register_checkbox_field_admin' );
add_action( 'woocommerce_save_account_details', 'celedonia_save_extra_register_checkbox_field_admin' );
function celedonia_save_extra_register_checkbox_field_admin( $customer_id ){
update_user_meta( $customer_id, 'cel_news', $_POST['cel_news'] );
}
///////////////////////////////
// 2. VALIDATE FIELDS
add_filter( 'woocommerce_registration_errors', 'celedonia_validate_name_fields', 10, 3 );
function celedonia_validate_name_fields( $errors, $username, $email=null ) {
if ( isset( $_POST['billing_first_name'] ) && empty( $_POST['billing_first_name'] ) ) {
$errors->add( 'billing_first_name_error', __( '<strong>!</strong>: Nombre!', 'woocommerce' ) );
}
if ( isset( $_POST['billing_last_name'] ) && empty( $_POST['billing_last_name'] ) ) {
$errors->add( 'billing_last_name_error', __( '<strong>!</strong>: Apellidos!.', 'woocommerce' ) );
}
return $errors;
}
@tradesouthwest
Copy link
Author

good

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment