Forked from NickMkrtchyan/gist:bbd3f05d233dff84484cd09260be0475
Created
December 16, 2024 16:28
-
-
Save dexit/7dce5d51a374f1e3bbed6efc8bf70d62 to your computer and use it in GitHub Desktop.
Complate Corporate/Customer Woo Script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Custom registration form for WooCommerce | |
function custom_woocommerce_register_form() { | |
$user_role = ( ! empty( $_POST['user_role'] ) ) ? sanitize_text_field( $_POST['user_role'] ) : ''; | |
$company_id = ( ! empty( $_POST['company_id'] ) ) ? sanitize_text_field( $_POST['company_id'] ) : ''; | |
?> | |
<p class="form-row form-row-wide"> | |
<label for="user_role"><?php _e( 'Choose account type', 'woodmart' ); ?></label> | |
<select name="user_role" id="user_role"> | |
<option value="customer" <?php selected( $user_role, 'customer' ); ?>><?php _e( 'Physical person', 'woodmart' ); ?></option> | |
<option value="company" <?php selected( $user_role, 'company' ); ?>><?php _e( 'Corporate person', 'woodmart' ); ?></option> | |
</select> | |
</p> | |
<p class="form-row form-row-wide company-id-field" style="display: none;"> | |
<label for="company_id"><?php _e( 'Company Tax ID', 'woodmart' ); ?> <span class="required">*</span></label> | |
<input type="text" name="company_id" id="company_id" value="<?php echo esc_attr( $company_id ); ?>" <?php echo ( $user_role === 'company' ) ? 'required' : ''; ?> /> | |
</p> | |
<p class="form-row form-row-wide passport-id-field" style="display: block;"> | |
<label for="passport_id"><?php _e( 'Passport series', 'woodmart' ); ?> <span class="required">*</span></label> | |
<input type="text" name="passport_id" id="passport_id" value="<?php echo esc_attr( $passport_id ); ?>" <?php echo ( $user_role === 'customer' ) ? 'required' : ''; ?> /> | |
</p> | |
<script> | |
jQuery(function($) { | |
$('#user_role').on('change', function() { | |
if ( $(this).val() === 'company' ) { | |
$('.company-id-field').show(); | |
$('.passport-id-field').hide(); | |
$('#company_id').prop('required', true); | |
$('#passport_id').prop('required', false); | |
} else { | |
$('.company-id-field').hide(); | |
$('.passport-id-field').show(); | |
$('#company_id').prop('required', false); | |
$('#passport_id').prop('required', true); | |
} | |
}); | |
}); | |
</script> | |
<?php | |
} | |
add_action( 'woocommerce_register_form', 'custom_woocommerce_register_form' ); | |
// Save custom fields in WooCommerce registration form | |
function custom_woocommerce_registration_save( $customer_id ) { | |
if ( isset( $_POST['user_role'] ) ) { | |
$user = new WP_User( $customer_id ); | |
$user->set_role( sanitize_text_field( $_POST['user_role'] ) ); | |
} | |
if ( isset( $_POST['company_id'] ) ) { | |
update_user_meta( $customer_id, 'company_id', sanitize_text_field( $_POST['company_id'] ) ); | |
} | |
if ( isset( $_POST['passport_id'] ) && sanitize_text_field( $_POST['user_role'] ) === 'customer' ) { | |
update_user_meta( $customer_id, 'passport_id', sanitize_text_field( $_POST['passport_id'] ) ); | |
} | |
} | |
add_action( 'woocommerce_created_customer', 'custom_woocommerce_registration_save' ); | |
// Display custom fields in WooCommerce edit account page | |
function custom_woocommerce_edit_account_form() { | |
$user = wp_get_current_user(); | |
$user_id = get_current_user_id(); | |
$user_role = wp_get_current_user()->roles[0]; | |
$company_id = get_user_meta( $user_id, 'company_id', true ); | |
$passport_id = get_user_meta( $user_id, 'passport_id', true ); | |
if ( $user_role === 'company' ) { | |
?> | |
<p class="woocommerce-form-row woocommerce-form-row--wide form-row form-row-wide"> | |
<label for="account_username"><?php _e( 'Username', 'woodmart' ); ?> <span class="required">*</span></label> | |
<input type="text" class="woocommerce-Input woocommerce-Input--text input-text" name="account_username" id="account_username" value="<?php echo esc_attr( $user->user_login ); ?>" autocomplete="username" disabled /> | |
</p> | |
<p class="form-row form-row-wide"> | |
<label for="company_id"><?php _e( 'Company Tax ID', 'woodmart' ); ?> <span class="required">*</span></label> | |
<input type="text" name="company_id" id="company_id" value="<?php echo esc_attr( $company_id ); ?>" required /> | |
</p> | |
<?php | |
} elseif ( $user_role === 'customer' ) { | |
?> | |
<p class="woocommerce-form-row woocommerce-form-row--wide form-row form-row-wide"> | |
<label for="account_username"><?php _e( 'Username', 'woodmart' ); ?> <span class="required">*</span></label> | |
<input type="text" class="woocommerce-Input woocommerce-Input--text input-text" name="account_username" id="account_username" value="<?php echo esc_attr( $user->user_login ); ?>" autocomplete="username" disabled /> | |
</p> | |
<p class="form-row form-row-wide"> | |
<label for="passport_id"><?php _e( 'Passport series', 'woodmart' ); ?> <span class="required">*</span></label> | |
<input type="text" name="passport_id" id="passport_id" value="<?php echo esc_attr( $passport_id ); ?>" required /> | |
</p> | |
<p class="woocommerce-form-row woocommerce-form-row--first form-row form-row-first"> | |
<label for="passport_date_of_issue"><?php _e( 'Date of issue', 'woodmart' ); ?> <span class="required">*</span></label> | |
<input type="text" name="passport_date_of_issue" id="passport_date_of_issue" value="<?php echo esc_attr( get_user_meta( $user_id, 'passport_date_of_issue', true ) ); ?>" required /> | |
</p> | |
<p class="woocommerce-form-row woocommerce-form-row--last form-row form-row-last"> | |
<label for="passport_issued_by"><?php _e( 'Issued by', 'woodmart' ); ?> <span class="required">*</span></label> | |
<input type="text" name="passport_issued_by" id="passport_issued_by" value="<?php echo esc_attr( get_user_meta( $user_id, 'passport_issued_by', true ) ); ?>" required /> | |
</p> | |
<?php | |
} | |
} | |
add_action( 'woocommerce_edit_account_form_start', 'custom_woocommerce_edit_account_form' ); | |
// Save custom fields in WooCommerce edit account page | |
function custom_woocommerce_edit_account_save( $user_id ) { | |
if ( isset( $_POST['company_id'] ) ) { | |
update_user_meta( $user_id, 'company_id', sanitize_text_field( $_POST['company_id'] ) ); | |
} | |
$user = get_user_by( 'id', $user_id ); | |
$user_role = $user->roles[0]; | |
if ( isset( $_POST['passport_id'] ) && $user_role === 'customer' ) { | |
update_user_meta( $user_id, 'passport_id', sanitize_text_field( $_POST['passport_id'] ) ); | |
update_user_meta( $user_id, 'passport_issued_by', sanitize_text_field( $_POST['passport_issued_by'] ) ); | |
update_user_meta( $user_id, 'passport_date_of_issue', sanitize_text_field( $_POST['passport_date_of_issue'] ) ); | |
} | |
} | |
add_action( 'woocommerce_save_account_details', 'custom_woocommerce_edit_account_save' ); | |
// Display custom fields in WooCommerce checkout page | |
function custom_woocommerce_checkout_fields( $fields ) { | |
$user_role = wp_get_current_user()->roles[0]; | |
if ( $user_role === 'company' ) { | |
$fields['billing']['company_id'] = array( | |
'label' => __( 'Company Tax ID', 'woodmart' ), | |
'placeholder' => '', | |
'required' => true, | |
'class' => array( 'form-row-wide' ), | |
'clear' => true | |
); | |
} | |
return $fields; | |
} | |
add_filter( 'woocommerce_checkout_fields', 'custom_woocommerce_checkout_fields' ); | |
// Save custom fields in WooCommerce checkout page | |
function custom_woocommerce_checkout_update_order_meta( $order_id ) { | |
if ( ! empty( $_POST['company_id'] ) ) { | |
update_post_meta( $order_id, 'Company Tax ID', sanitize_text_field( $_POST['company_id'] ) ); | |
} | |
} | |
add_action( 'woocommerce_checkout_update_order_meta', 'custom_woocommerce_checkout_update_order_meta' ); | |
// dddddddddddd Add HVHH to Admin Orders | |
function add_company_id_to_order_details( $order ) { | |
$user_id = $order->get_user_id(); | |
$user_role = get_user_meta( $user_id, 'wp_capabilities', true ); | |
if ( array_key_exists( 'company', $user_role ) ) { | |
$company_id = get_user_meta( $user_id, 'company_id', true ); | |
echo '<p><strong>' . __( 'Company Tax ID', 'woodmart' ) . ':</strong> ' . $company_id . '</p>'; | |
} | |
} | |
add_action( 'woocommerce_admin_order_data_after_billing_address', 'add_company_id_to_order_details', 10, 1 ); | |
// Add HVHH to order-received | |
function add_company_id_to_order_received_page( $order ) { | |
$user_id = $order->get_user_id(); | |
$user_role = get_user_meta( $user_id, 'wp_capabilities', true ); | |
if ( array_key_exists( 'company', $user_role ) ) { | |
$company_id = get_user_meta( $user_id, 'company_id', true ); | |
echo '<p><strong>' . __( 'Company Tax ID ', 'woodmart' ) . ':</strong> ' . $company_id . '</p>'; | |
} | |
} | |
add_action( 'woocommerce_order_details_after_customer_details', 'add_company_id_to_order_received_page' ); | |
// | |
add_action( 'woocommerce_order_details_after_order_table', 'add_custom_billing_address_fields' ); | |
function add_custom_billing_address_fields( $order ) { | |
?> | |
<h2><?php esc_html_e( 'Billing address', 'woodmart' ); ?></h2> | |
<table> | |
<tbody> | |
<tr> | |
<th><?php esc_html_e( 'Name:', 'woodmart' ); ?></th> | |
<td><?php echo esc_html( $order->get_billing_first_name() . ' ' . $order->get_billing_last_name() ); ?></td> | |
</tr> | |
<tr> | |
<th><?php esc_html_e( 'Email:', 'woodmart' ); ?></th> | |
<td><?php echo esc_html( $order->get_billing_email() ); ?></td> | |
</tr> | |
<tr> | |
<th><?php esc_html_e( 'Phone:', 'woodmart' ); ?></th> | |
<td><?php echo esc_html( $order->get_billing_phone() ); ?></td> | |
</tr> | |
<tr> | |
<th><?php esc_html_e( 'Address:', 'woodmart' ); ?></th> | |
<td><?php echo esc_html( $order->get_billing_address_1() . ' ' . $order->get_billing_address_2() ); ?></td> | |
</tr> | |
<tr> | |
<th><?php esc_html_e( 'City:', 'woodmart' ); ?></th> | |
<td><?php echo esc_html( $order->get_billing_city() ); ?></td> | |
</tr> | |
<tr> | |
<th><?php esc_html_e( 'State:', 'woodmart' ); ?></th> | |
<td><?php echo esc_html( $order->get_billing_state() ); ?></td> | |
</tr> | |
<tr> | |
<th><?php esc_html_e( 'Postcode:', 'woodmart' ); ?></th> | |
<td><?php echo esc_html( $order->get_billing_postcode() ); ?></td> | |
</tr> | |
<tr> | |
<th><?php esc_html_e( 'Country:', 'woodmart' ); ?></th> | |
<td><?php echo esc_html( $order->get_billing_country() ); ?></td> | |
</tr> | |
<?php | |
$user_id = get_current_user_id(); | |
$user_role = wp_get_current_user()->roles[0]; | |
$company_id = get_user_meta( $user_id, 'company_id', true ); | |
if ( $user_role === 'company' ) { | |
?> | |
<tr> | |
<th><?php esc_html_e( 'Company Tax ID', 'woodmart' ); ?>:</th> | |
<td><?php echo esc_html( $company_id ); ?></td> | |
</tr> | |
<?php | |
} ?> | |
</tbody> | |
</table | |
<?php | |
} | |
// Admin dashboard account user meta | |
function add_company_id_field( $user ) { | |
$company_id = get_user_meta( $user->ID, 'company_id', true ); | |
$passport_id = get_user_meta( $user->ID, 'passport_id', true ); | |
$passport_issued_by = get_user_meta( $user->ID, 'passport_issued_by', true ); | |
$passport_date_of_issue = get_user_meta( $user->ID, 'passport_date_of_issue', true ); | |
?> | |
<h2><?php _e( 'About customer', 'woodmart' ); ?></h2> | |
<table class="form-table"> | |
<tr> | |
<th><label for="company_id"><?php _e( 'Company Tax ID', 'woodmart' ); ?></label></th> | |
<td><input type="text" name="company_id" id="company_id" value="<?php echo esc_attr( $company_id ); ?>" class="regular-text"></td> | |
</tr> | |
<tr> | |
<th><label for="passport_id"><?php _e( 'Passport series', 'woodmart' ); ?></label></th> | |
<td><input type="text" name="passport_id" id="passport_id" value="<?php echo esc_attr( $passport_id ); ?>" class="regular-text"></td> | |
</tr> | |
<tr> | |
<th><label for="passport_issued_by"><?php _e( 'Issued by', 'woodmart' ); ?></label></th> | |
<td><input type="text" name="passport_issued_by" id="passport_issued_by" value="<?php echo esc_attr( $passport_issued_by ); ?>" class="regular-text"></td> | |
</tr> | |
<tr> | |
<th><label for="passport_date_of_issue"><?php _e( 'Date of issue', 'woodmart' ); ?></label></th> | |
<td><input type="text" name="passport_date_of_issue" id="passport_date_of_issue" value="<?php echo esc_attr( $passport_date_of_issue ); ?>" class="regular-text"></td> | |
</tr> | |
</table> | |
<?php | |
} | |
add_action( 'show_user_profile', 'add_company_id_field' ); | |
add_action( 'edit_user_profile', 'add_company_id_field' ); | |
// Save custom user meta field for company ID | |
function save_company_id_field( $user_id ) { | |
if ( current_user_can( 'edit_user', $user_id ) ) { | |
update_user_meta( $user_id, 'company_id', sanitize_text_field( $_POST['company_id'] ) ); | |
} | |
if ( current_user_can( 'edit_user', $user_id ) ) { | |
update_user_meta( $user_id, 'passport_id', sanitize_text_field( $_POST['passport_id'] ) ); | |
update_user_meta( $user_id, 'passport_issued_by', sanitize_text_field( $_POST['passport_issued_by'] ) ); | |
update_user_meta( $user_id, 'passport_date_of_issue', sanitize_text_field( $_POST['passport_date_of_issue'] ) ); | |
} | |
} | |
add_action( 'personal_options_update', 'save_company_id_field', 1, 2 ); | |
add_action( 'edit_user_profile_update', 'save_company_id_field', 1, 2 ); | |
// if ( isset( $_POST['company_id'] ) ) { | |
// update_user_meta( $user_id, 'company_id', sanitize_text_field( $_POST['company_id'] ) ); | |
// } | |
// $user = get_user_by( 'id', $user_id ); | |
// $user_role = $user->roles[0]; | |
// if ( isset( $_POST['passport_id'] ) && $user_role === 'customer' ) { | |
// update_user_meta( $user_id, 'passport_id', sanitize_text_field( $_POST['passport_id'] ) ); | |
// update_user_meta( $user_id, 'passport_issued_by', sanitize_text_field( $_POST['passport_issued_by'] ) ); | |
// update_user_meta( $user_id, 'passport_date_of_issue', sanitize_text_field( $_POST['passport_date_of_issue'] ) ); | |
// } | |
// } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment