Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save zakariabinsaifullah/9db1aab7d84aa2d4dbdcc5f32d1ffb48 to your computer and use it in GitHub Desktop.
Save zakariabinsaifullah/9db1aab7d84aa2d4dbdcc5f32d1ffb48 to your computer and use it in GitHub Desktop.
<?php
/**
* WooCommerce functions
*/
add_shortcode( 'wc_reg_form', 'dwc_separate_registration_form' );
function dwc_separate_registration_form() {
if ( is_admin() ) return;
if ( is_user_logged_in() ) return;
ob_start();
// NOTE: THE FOLLOWING <FORM></FORM> IS COPIED FROM woocommerce\templates\myaccount\form-login.php
// IF WOOCOMMERCE RELEASES AN UPDATE TO THAT TEMPLATE, YOU MUST CHANGE THIS ACCORDINGLY
do_action( 'woocommerce_before_customer_login_form' );
?>
<form method="post" class="woocommerce-form woocommerce-form-register register" <?php do_action( 'woocommerce_register_form_tag' ); ?> >
<?php do_action( 'woocommerce_register_form_start' ); ?>
<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>
<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'] ) ) : ''; ?>" />
</p>
<p class="woocommerce-form-row woocommerce-form-row--wide form-row form-row-wide">
<label for="reg_billing_company"><?php esc_html_e( 'Company Name', 'woocommerce' ); ?> </label>
<input type="text" class="woocommerce-Input woocommerce-Input--text input-text" name="billing_company" id="reg_billing_company" value="<?php echo ( ! empty( $_POST['billing_company'] ) ) ? esc_attr( wp_unslash( $_POST['billing_company'] ) ) : ''; ?>" />
</p>
<p class="woocommerce-form-row woocommerce-form-row--wide form-row form-row-wide">
<label for="reg_billing_address_1"><?php esc_html_e( 'Address', 'woocommerce' ); ?> <span class="required">*</span></label>
<input type="text" class="woocommerce-Input woocommerce-Input--text input-text" name="billing_address_1" id="reg_billing_address_1" value="<?php echo ( ! empty( $_POST['billing_address_1'] ) ) ? esc_attr( wp_unslash( $_POST['billing_address_1'] ) ) : ''; ?>" />
</p>
<p class="woocommerce-form-row woocommerce-form-row--wide form-row form-row-wide">
<label for="reg_billing_phone"><?php esc_html_e( 'Phone', 'woocommerce' ); ?> <span class="required">*</span></label>
<input type="tel" class="woocommerce-Input woocommerce-Input--text input-text" name="billing_phone" id="reg_billing_phone" value="<?php echo ( ! empty( $_POST['billing_phone'] ) ) ? esc_attr( wp_unslash( $_POST['billing_phone'] ) ) : ''; ?>" />
</p>
<?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'] ) ) : ''; ?>" />
</p>
<?php endif; ?>
<p class="woocommerce-form-row woocommerce-form-row--wide form-row form-row-wide">
<label for="reg_billing_ic"><?php esc_html_e( 'Business ID', 'woocommerce' ); ?> </label>
<input type="text" class="woocommerce-Input woocommerce-Input--text input-text" name="billing_ic" id="reg_billing_ic" value="<?php echo ( ! empty( $_POST['billing_ic'] ) ) ? esc_attr( wp_unslash( $_POST['billing_ic'] ) ) : ''; ?>" />
</p>
<p class="woocommerce-form-row woocommerce-form-row--wide form-row form-row-wide">
<label for="reg_billing_dic"><?php esc_html_e( 'Tax ID', 'woocommerce' ); ?> </label>
<input type="text" class="woocommerce-Input woocommerce-Input--text input-text" name="billing_dic" id="reg_billing_dic" value="<?php echo ( ! empty( $_POST['billing_dic'] ) ) ? esc_attr( wp_unslash( $_POST['billing_dic'] ) ) : ''; ?>" />
</p>
<p class="woocommerce-form-row woocommerce-form-row--wide form-row form-row-wide">
<label for="phone_number"><?php esc_html_e( 'phone_number Fucking Value', 'woocommerce' ); ?> </label>
<input type="text" class="woocommerce-Input woocommerce-Input--text input-text" name="phone_number" id="phone_number" value="<?php echo ( ! empty( $_POST['phone_number'] ) ) ? esc_attr( wp_unslash( $_POST['phone_number'] ) ) : ''; ?>" />
</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 else : ?>
<p><?php esc_html_e( 'A password will be sent to your email address.', 'woocommerce' ); ?></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 woocommerce-button button woocommerce-form-register__submit" name="register" value="<?php esc_attr_e( 'Register', 'woocommerce' ); ?>"><?php esc_html_e( 'Register', 'woocommerce' ); ?></button>
</p>
<?php do_action( 'woocommerce_register_form_end' ); ?>
</form>
<?php
return ob_get_clean();
}
add_action( 'woocommerce_created_customer', 'bbloomer_save_name_fields' );
function bbloomer_save_name_fields( $customer_id ) {
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']) );
}
if ( isset( $_POST['billing_company'] ) ) {
update_user_meta( $customer_id, 'billing_company', sanitize_text_field( $_POST['billing_company'] ) );
update_user_meta( $customer_id, 'billing_company', sanitize_text_field($_POST['billing_company']) );
}
if ( isset( $_POST['billing_ic'] ) ) {
update_user_meta( $customer_id, 'billing_ic', sanitize_text_field( $_POST['billing_ic'] ) );
update_user_meta( $customer_id, 'billing_ic', sanitize_text_field( $_POST['billing_ic'] ) );
}
if ( isset( $_POST['billing_dic'] ) ) {
update_user_meta( $customer_id, 'billing_dic', sanitize_text_field( $_POST['billing_dic'] ) );
update_user_meta( $customer_id, 'billing_dic', sanitize_text_field( $_POST['billing_dic'] ) );
}
if ( isset( $_POST['phone_number'] ) ) {
update_user_meta( $customer_id, 'phone_number', sanitize_text_field( $_POST['phone_number'] ) );
update_user_meta( $customer_id, 'phone_number', sanitize_text_field( $_POST['phone_number'] ) );
}
}
add_filter( 'woocommerce_billing_fields', 'misha_add_field' );
function misha_add_field( $fields ) {
$fields[ 'billing_ic' ] = array(
'label' => __('Business ID', 'woocommerce'),
'required' => true,
'class' => array( 'form-row-wide'),
'priority' => 120,
'placeholder' => 'Business ID',
);
$fields[ 'billing_dic' ] = array(
'label' => __('Tax ID', 'woocommerce'),
'required' => true,
'class' => array( 'form-row-wide'),
'priority' => 121,
'placeholder' => 'Tax ID',
);
return $fields;
}
// accout details
add_action( 'woocommerce_edit_account_form', 'misha_add_field_edit_account_form' );
function misha_add_field_edit_account_form() {
woocommerce_form_field(
'phone_number',
array(
'type' => 'tel',
'required' => true, // remember, this doesn't make the field required, just adds an "*"
'label' => 'Phone number',
'placeholder' => 'Phone number',
'class' => array( 'form-row-wide' ),
'priority' => 20,
),
get_user_meta( get_current_user_id(), 'phone_number', true ) // get the data
);
}
// Save field value
add_action( 'woocommerce_save_account_details', 'misha_save_account_details' );
function misha_save_account_details( $user_id ) {
update_user_meta( $user_id, 'phone_number', wc_clean( $_POST[ 'phone_number' ] ) );
}
// Make it required
add_filter( 'woocommerce_save_account_details_required_fields', 'misha_make_field_required' );
function misha_make_field_required( $required_fields ){
$required_fields[ 'phone_number' ] = 'Phone number';
return $required_fields;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment