Skip to content

Instantly share code, notes, and snippets.

@devinsays
Last active December 28, 2017 20:58
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save devinsays/348c4f00d1ab4d47f98604939c353af0 to your computer and use it in GitHub Desktop.
Save devinsays/348c4f00d1ab4d47f98604939c353af0 to your computer and use it in GitHub Desktop.
Adds meta fields to account profiles for business_account (wholesale) meta.
<?php
/**
* Add extra profile fields for users in admin.
*
* @author Nano
* @version 2.2.0
*/
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
if ( ! class_exists( 'Nano_User_Profile', false ) ) :
/**
* Nano_User_Profile Class.
*/
class Nano_User_Profile {
/**
* Hook in tabs.
*/
public function __construct() {
add_action( 'show_user_profile', array( $this, 'add_user_meta_fields' ) );
add_action( 'edit_user_profile', array( $this, 'add_user_meta_fields' ) );
add_action( 'user_new_form', array( $this, 'add_user_meta_fields' ) );
add_action( 'personal_options_update', array( $this, 'save_customer_meta_fields' ) );
add_action( 'edit_user_profile_update', array( $this, 'save_customer_meta_fields' ) );
add_action( 'user_new_form', array( $this, 'save_customer_meta_fields' ) );
}
/**
* Get Address Fields for the edit user pages.
*
* @return array Fields to display which are filtered through woocommerce_customer_meta_fields before being returned
*/
public function get_user_meta_fields() {
$fields = array(
'business_account' => array(
'fields' => array(
'business_account' => array(
'type' => 'checkbox',
'label' => __( 'Business account', 'nano' ),
'description' => 'This customer has a business account.',
'class' => '',
)
),
),
);
return $fields;
}
/**
* Show Address Fields on edit user pages.
*
* @param WP_User $user
*/
public function add_user_meta_fields( $user ) {
if ( ! current_user_can( 'manage_woocommerce' ) ) {
return;
}
$show_fields = $this->get_user_meta_fields();
foreach ( $show_fields as $fieldset_key => $fieldset ) :
?>
<table class="form-table" id="<?php echo esc_attr( 'fieldset-' . $fieldset_key ); ?>">
<?php
foreach ( $fieldset['fields'] as $key => $field ) :
$value = 0;
if ( isset( $user->ID ) ) {
$value = get_user_meta( $user->ID, $key, true );
}
?>
<tr>
<th><label for="<?php echo esc_attr( $key ); ?>"><?php echo esc_html( $field['label'] ); ?></label></th>
<td>
<?php if ( ! empty( $field['type'] ) && 'checkbox' === $field['type'] ) : ?>
<label for="<?php echo esc_attr( $key ); ?>">
<input type="checkbox" name="<?php echo esc_attr( $key ); ?>" id="<?php echo esc_attr( $key ); ?>" value="1" class="<?php echo esc_attr( $field['class'] ); ?>" <?php checked( (int) $value, 1, true ); ?> />
<span><?php echo wp_kses_post( $field['description'] ); ?></span>
</label>
<?php endif; ?>
<br/>
</td>
</tr>
<?php
endforeach;
?>
</table>
<?php
endforeach;
}
/**
* Save Address Fields on edit user pages.
*
* @param int $user_id User ID of the user being saved
*/
public function save_customer_meta_fields( $user_id ) {
$save_fields = $this->get_user_meta_fields();
foreach ( $save_fields as $fieldset ) {
foreach ( $fieldset['fields'] as $key => $field ) {
if ( isset( $field['type'] ) && 'checkbox' === $field['type'] ) {
$value = isset( $_POST[ $key ] );
// Adds business account capability if field is checked
if ( 'business_account' === $key ) :
$user = new WP_User( $user_id );
if ( 1 === (int) $value ) {
$user->add_cap( 'business_account' );
} else {
$user->remove_cap( 'business_account' );
}
endif;
update_user_meta( $user_id, $key, $value );
}
}
}
}
}
endif;
return new Nano_User_Profile();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment