As an organization (whether for profit or not for profit) you are trying to accomplish…
Request
We got a request to add a custom field to user profiles allowing multiple codes separated by commas and then add a select containing those codes to the checkout page. These codes were going to be used to add a cc: to pdf invoice emails being sent out separately.
Plugins Required
Code
<?php /** // Add Billing Codes user profile field. Used as a comma deliniated list // See custom_override_checkout_fields() **/ add_filter( 'user_contactmethods', 'modify_contact_methods' ); function modify_contact_methods( $profile_fields ) { // Add new fields $profile_fields['billing_code'] = 'Billing Codes'; return $profile_fields; } /** // Add a billing code to the checkout screen. Customzied to show different values based on the // value of the User Field "Billing Code"|'billing_code' from modify_contact_methods() **/ add_filter( 'woocommerce_checkout_fields', 'custom_override_checkout_fields' ); function custom_override_checkout_fields( $fields ) { $billing_code = get_user_meta ( get_current_user_id() ); $billing_code = $billing_code['billing_code']['0']; if ( empty ( $billing_code ) ) { $num_billing_codes = 0 ; } else { $billing_code = explode ( ',', $billing_code ); $num_billing_codes = count ( $billing_code ); } $fields['billing']['billing_code'] = array( 'label' => __('Bill To', 'woocommerce'), 'placeholder' => _x('', 'placeholder', 'woocommerce'), 'required' => true, 'clear' => false, 'type' => 'select', 'class' => array('form-row-wide'), ); $i = 0; if ( $num_freshmed_billing_codes == 0 ) { $fields['billing']['billing_code']['type'] = 'text'; } elseif ( $num_billing_codes == 1 ) { $fields['billing']['billing_code']['options'] = array( $billing_code['0'] => __( $freshmed_billing_code['0'], 'woocommerce' ), ); } elseif ( $num_freshmed_billing_codes > 1 ) { $fields['billing']['billing_code']['options'][""] = __("Please Select Bill To" , 'woocommerce'); foreach ( $freshmed_billing_code as $code ) { $fields['billing']['billing_code']['options']["$code"] = __($code, 'woocommerce'); $i++; } } return $fields; } ?>
Display
User Profile
Checkout Page
Display
Element Inspector
Disclaimer
Purrly Digital LLC cannot be held responsible for the functionality of this code. Please make sure you test it on a development site before adding the code to your production website. There is no support available for this (and other) code snippet(s) posted on this website. If you’d like Purrly Digital to do custom development to help with your custom implementation please send a contact request.
This Post Has 0 Comments