2011-12-21 23:44:08 +00:00
|
|
|
<?php
|
|
|
|
/**
|
2012-08-14 12:21:34 +00:00
|
|
|
* Admin user functions
|
|
|
|
*
|
|
|
|
* Functions used for modifying the users panel in admin.
|
2011-12-21 23:44:08 +00:00
|
|
|
*
|
|
|
|
* @author WooThemes
|
|
|
|
* @category Admin
|
2012-08-14 12:21:34 +00:00
|
|
|
* @package WooCommerce/Admin/Users
|
|
|
|
* @version 1.6.4
|
2011-12-21 23:44:08 +00:00
|
|
|
*/
|
|
|
|
|
2012-10-15 10:32:24 +00:00
|
|
|
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
|
|
|
|
|
2011-12-22 19:55:08 +00:00
|
|
|
/**
|
2012-08-14 12:21:34 +00:00
|
|
|
* Define columns to show on the users page.
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @param array $columns Columns on the manage users page
|
|
|
|
* @return array The modified columns
|
2011-12-22 19:55:08 +00:00
|
|
|
*/
|
2011-12-21 23:44:08 +00:00
|
|
|
function woocommerce_user_columns( $columns ) {
|
2012-08-14 12:21:34 +00:00
|
|
|
if ( ! current_user_can( 'manage_woocommerce' ) )
|
|
|
|
return $columns;
|
2011-12-21 23:44:08 +00:00
|
|
|
|
2012-10-16 09:45:33 +00:00
|
|
|
$columns['woocommerce_billing_address'] = __( 'Billing Address', 'woocommerce' );
|
|
|
|
$columns['woocommerce_shipping_address'] = __( 'Shipping Address', 'woocommerce' );
|
|
|
|
$columns['woocommerce_paying_customer'] = __( 'Paying Customer?', 'woocommerce' );
|
|
|
|
$columns['woocommerce_order_count'] = __( 'Orders', 'woocommerce' );
|
2011-12-21 23:44:08 +00:00
|
|
|
return $columns;
|
|
|
|
}
|
2012-08-14 12:21:34 +00:00
|
|
|
|
|
|
|
add_filter( 'manage_users_columns', 'woocommerce_user_columns', 10, 1 );
|
|
|
|
|
|
|
|
|
2011-12-22 19:55:08 +00:00
|
|
|
/**
|
2012-08-14 12:21:34 +00:00
|
|
|
* Define values for custom columns.
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @param mixed $value The value of the column being displayed
|
|
|
|
* @param mixed $column_name The name of the column being displayed
|
|
|
|
* @param mixed $user_id The ID of the user being displayed
|
|
|
|
* @return string Value for the column
|
2011-12-22 19:55:08 +00:00
|
|
|
*/
|
2012-08-14 12:21:34 +00:00
|
|
|
function woocommerce_user_column_values( $value, $column_name, $user_id ) {
|
2011-12-21 23:44:08 +00:00
|
|
|
global $woocommerce, $wpdb;
|
|
|
|
switch ($column_name) :
|
|
|
|
case "woocommerce_order_count" :
|
2012-08-14 12:21:34 +00:00
|
|
|
|
2012-10-16 14:46:21 +00:00
|
|
|
$count = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*)
|
2012-08-14 12:21:34 +00:00
|
|
|
FROM $wpdb->posts
|
2011-12-21 23:44:08 +00:00
|
|
|
LEFT JOIN $wpdb->postmeta ON $wpdb->posts.ID = $wpdb->postmeta.post_id
|
2012-08-14 12:21:34 +00:00
|
|
|
WHERE meta_value = $user_id
|
2011-12-21 23:44:08 +00:00
|
|
|
AND meta_key = '_customer_user'
|
2012-08-14 12:21:34 +00:00
|
|
|
AND post_type IN ('shop_order')
|
2012-10-16 14:46:21 +00:00
|
|
|
AND post_status = 'publish'" ) );
|
2012-08-14 12:21:34 +00:00
|
|
|
|
2011-12-21 23:44:08 +00:00
|
|
|
$value = '<a href="'.admin_url('edit.php?post_status=all&post_type=shop_order&_customer_user='.$user_id.'').'">'.$count.'</a>';
|
2012-08-14 12:21:34 +00:00
|
|
|
|
2011-12-21 23:44:08 +00:00
|
|
|
break;
|
|
|
|
case "woocommerce_billing_address" :
|
|
|
|
$address = array(
|
|
|
|
'first_name' => get_user_meta( $user_id, 'billing_first_name', true ),
|
|
|
|
'last_name' => get_user_meta( $user_id, 'billing_last_name', true ),
|
|
|
|
'company' => get_user_meta( $user_id, 'billing_company', true ),
|
|
|
|
'address_1' => get_user_meta( $user_id, 'billing_address_1', true ),
|
|
|
|
'address_2' => get_user_meta( $user_id, 'billing_address_2', true ),
|
2012-08-14 12:21:34 +00:00
|
|
|
'city' => get_user_meta( $user_id, 'billing_city', true ),
|
2011-12-21 23:44:08 +00:00
|
|
|
'state' => get_user_meta( $user_id, 'billing_state', true ),
|
|
|
|
'postcode' => get_user_meta( $user_id, 'billing_postcode', true ),
|
|
|
|
'country' => get_user_meta( $user_id, 'billing_country', true )
|
|
|
|
);
|
|
|
|
|
|
|
|
$formatted_address = $woocommerce->countries->get_formatted_address( $address );
|
2012-08-14 12:21:34 +00:00
|
|
|
|
2012-10-16 09:45:33 +00:00
|
|
|
if (!$formatted_address) $value = __( 'N/A', 'woocommerce' ); else $value = $formatted_address;
|
2012-08-14 12:21:34 +00:00
|
|
|
|
2011-12-21 23:44:08 +00:00
|
|
|
$value = wpautop($value);
|
|
|
|
break;
|
|
|
|
case "woocommerce_shipping_address" :
|
|
|
|
$address = array(
|
|
|
|
'first_name' => get_user_meta( $user_id, 'shipping_first_name', true ),
|
|
|
|
'last_name' => get_user_meta( $user_id, 'shipping_last_name', true ),
|
|
|
|
'company' => get_user_meta( $user_id, 'shipping_company', true ),
|
|
|
|
'address_1' => get_user_meta( $user_id, 'shipping_address_1', true ),
|
|
|
|
'address_2' => get_user_meta( $user_id, 'shipping_address_2', true ),
|
2012-08-14 12:21:34 +00:00
|
|
|
'city' => get_user_meta( $user_id, 'shipping_city', true ),
|
2011-12-21 23:44:08 +00:00
|
|
|
'state' => get_user_meta( $user_id, 'shipping_state', true ),
|
|
|
|
'postcode' => get_user_meta( $user_id, 'shipping_postcode', true ),
|
|
|
|
'country' => get_user_meta( $user_id, 'shipping_country', true )
|
|
|
|
);
|
|
|
|
|
|
|
|
$formatted_address = $woocommerce->countries->get_formatted_address( $address );
|
2012-08-14 12:21:34 +00:00
|
|
|
|
2012-10-16 09:45:33 +00:00
|
|
|
if (!$formatted_address) $value = __( 'N/A', 'woocommerce' ); else $value = $formatted_address;
|
2012-08-14 12:21:34 +00:00
|
|
|
|
2011-12-21 23:44:08 +00:00
|
|
|
$value = wpautop($value);
|
|
|
|
break;
|
|
|
|
case "woocommerce_paying_customer" :
|
2012-08-14 12:21:34 +00:00
|
|
|
|
2011-12-21 23:44:08 +00:00
|
|
|
$paying_customer = get_user_meta( $user_id, 'paying_customer', true );
|
2012-08-14 12:21:34 +00:00
|
|
|
|
2012-01-28 15:04:53 +00:00
|
|
|
if ($paying_customer) $value = '<img src="'.$woocommerce->plugin_url().'/assets/images/success.png" alt="yes" />';
|
|
|
|
else $value = '<img src="'.$woocommerce->plugin_url().'/assets/images/success-off.png" alt="no" />';
|
2012-08-14 12:21:34 +00:00
|
|
|
|
2011-12-21 23:44:08 +00:00
|
|
|
break;
|
|
|
|
endswitch;
|
|
|
|
return $value;
|
|
|
|
}
|
2012-08-14 12:21:34 +00:00
|
|
|
|
|
|
|
add_action( 'manage_users_custom_column', 'woocommerce_user_column_values', 10, 3 );
|
|
|
|
|
|
|
|
|
2011-12-22 19:55:08 +00:00
|
|
|
/**
|
2012-08-14 12:21:34 +00:00
|
|
|
* Get Address Fields for the edit user pages.
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @return array Fields to display which are filtered through woocommerce_customer_meta_fields before being returned
|
2011-12-22 19:55:08 +00:00
|
|
|
*/
|
2012-01-06 14:43:03 +00:00
|
|
|
function woocommerce_get_customer_meta_fields() {
|
2011-12-23 21:41:07 +00:00
|
|
|
$show_fields = apply_filters('woocommerce_customer_meta_fields', array(
|
|
|
|
'billing' => array(
|
2012-10-16 09:45:33 +00:00
|
|
|
'title' => __( 'Customer Billing Address', 'woocommerce' ),
|
2011-12-23 21:41:07 +00:00
|
|
|
'fields' => array(
|
|
|
|
'billing_first_name' => array(
|
2012-10-16 09:45:33 +00:00
|
|
|
'label' => __( 'First name', 'woocommerce' ),
|
2011-12-23 21:41:07 +00:00
|
|
|
'description' => ''
|
|
|
|
),
|
|
|
|
'billing_last_name' => array(
|
2012-10-16 09:45:33 +00:00
|
|
|
'label' => __( 'Last name', 'woocommerce' ),
|
2011-12-23 21:41:07 +00:00
|
|
|
'description' => ''
|
|
|
|
),
|
2012-02-13 13:06:56 +00:00
|
|
|
'billing_company' => array(
|
2012-10-16 09:45:33 +00:00
|
|
|
'label' => __( 'Company', 'woocommerce' ),
|
2012-02-13 13:06:56 +00:00
|
|
|
'description' => ''
|
|
|
|
),
|
2011-12-23 21:41:07 +00:00
|
|
|
'billing_address_1' => array(
|
2012-10-16 09:45:33 +00:00
|
|
|
'label' => __( 'Address 1', 'woocommerce' ),
|
2011-12-23 21:41:07 +00:00
|
|
|
'description' => ''
|
|
|
|
),
|
|
|
|
'billing_address_2' => array(
|
2012-10-16 09:45:33 +00:00
|
|
|
'label' => __( 'Address 2', 'woocommerce' ),
|
2011-12-23 21:41:07 +00:00
|
|
|
'description' => ''
|
|
|
|
),
|
|
|
|
'billing_city' => array(
|
2012-10-16 09:45:33 +00:00
|
|
|
'label' => __( 'City', 'woocommerce' ),
|
2011-12-23 21:41:07 +00:00
|
|
|
'description' => ''
|
|
|
|
),
|
|
|
|
'billing_postcode' => array(
|
2012-10-16 09:45:33 +00:00
|
|
|
'label' => __( 'Postcode', 'woocommerce' ),
|
2011-12-23 21:41:07 +00:00
|
|
|
'description' => ''
|
|
|
|
),
|
|
|
|
'billing_state' => array(
|
2012-10-16 09:45:33 +00:00
|
|
|
'label' => __( 'State/County', 'woocommerce' ),
|
|
|
|
'description' => __( 'Country or state code', 'woocommerce' ),
|
2011-12-23 21:41:07 +00:00
|
|
|
),
|
|
|
|
'billing_country' => array(
|
2012-10-16 09:45:33 +00:00
|
|
|
'label' => __( 'Country', 'woocommerce' ),
|
|
|
|
'description' => __( '2 letter Country code', 'woocommerce' ),
|
2011-12-23 21:41:07 +00:00
|
|
|
),
|
|
|
|
'billing_phone' => array(
|
2012-10-16 09:45:33 +00:00
|
|
|
'label' => __( 'Telephone', 'woocommerce' ),
|
2011-12-23 21:41:07 +00:00
|
|
|
'description' => ''
|
|
|
|
),
|
|
|
|
'billing_email' => array(
|
2012-10-16 09:45:33 +00:00
|
|
|
'label' => __( 'Email', 'woocommerce' ),
|
2011-12-23 21:41:07 +00:00
|
|
|
'description' => ''
|
|
|
|
)
|
|
|
|
)
|
|
|
|
),
|
|
|
|
'shipping' => array(
|
2012-10-16 09:45:33 +00:00
|
|
|
'title' => __( 'Customer Shipping Address', 'woocommerce' ),
|
2011-12-23 21:41:07 +00:00
|
|
|
'fields' => array(
|
|
|
|
'shipping_first_name' => array(
|
2012-10-16 09:45:33 +00:00
|
|
|
'label' => __( 'First name', 'woocommerce' ),
|
2011-12-23 21:41:07 +00:00
|
|
|
'description' => ''
|
|
|
|
),
|
|
|
|
'shipping_last_name' => array(
|
2012-10-16 09:45:33 +00:00
|
|
|
'label' => __( 'Last name', 'woocommerce' ),
|
2011-12-23 21:41:07 +00:00
|
|
|
'description' => ''
|
|
|
|
),
|
2012-02-13 13:06:56 +00:00
|
|
|
'shipping_company' => array(
|
2012-10-16 09:45:33 +00:00
|
|
|
'label' => __( 'Company', 'woocommerce' ),
|
2012-02-13 13:06:56 +00:00
|
|
|
'description' => ''
|
|
|
|
),
|
2011-12-23 21:41:07 +00:00
|
|
|
'shipping_address_1' => array(
|
2012-10-16 09:45:33 +00:00
|
|
|
'label' => __( 'Address 1', 'woocommerce' ),
|
2011-12-23 21:41:07 +00:00
|
|
|
'description' => ''
|
|
|
|
),
|
|
|
|
'shipping_address_2' => array(
|
2012-10-16 09:45:33 +00:00
|
|
|
'label' => __( 'Address 2', 'woocommerce' ),
|
2011-12-23 21:41:07 +00:00
|
|
|
'description' => ''
|
|
|
|
),
|
|
|
|
'shipping_city' => array(
|
2012-10-16 09:45:33 +00:00
|
|
|
'label' => __( 'City', 'woocommerce' ),
|
2011-12-23 21:41:07 +00:00
|
|
|
'description' => ''
|
|
|
|
),
|
|
|
|
'shipping_postcode' => array(
|
2012-10-16 09:45:33 +00:00
|
|
|
'label' => __( 'Postcode', 'woocommerce' ),
|
2011-12-23 21:41:07 +00:00
|
|
|
'description' => ''
|
|
|
|
),
|
|
|
|
'shipping_state' => array(
|
2012-10-16 09:45:33 +00:00
|
|
|
'label' => __( 'State/County', 'woocommerce' ),
|
|
|
|
'description' => __( 'State/County or state code', 'woocommerce' )
|
2011-12-23 21:41:07 +00:00
|
|
|
),
|
|
|
|
'shipping_country' => array(
|
2012-10-16 09:45:33 +00:00
|
|
|
'label' => __( 'Country', 'woocommerce' ),
|
|
|
|
'description' => __( '2 letter Country code', 'woocommerce' )
|
2011-12-23 21:41:07 +00:00
|
|
|
)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
));
|
2012-01-06 14:43:03 +00:00
|
|
|
return $show_fields;
|
|
|
|
}
|
2012-08-14 12:21:34 +00:00
|
|
|
|
|
|
|
|
2012-01-06 14:43:03 +00:00
|
|
|
/**
|
2012-08-14 12:21:34 +00:00
|
|
|
* Show Address Fields on edit user pages.
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @param mixed $user User (object) being displayed
|
|
|
|
* @return void
|
2012-01-06 14:43:03 +00:00
|
|
|
*/
|
2012-08-14 12:21:34 +00:00
|
|
|
function woocommerce_customer_meta_fields( $user ) {
|
|
|
|
if ( ! current_user_can( 'manage_woocommerce' ) )
|
2012-07-31 14:04:05 +00:00
|
|
|
return;
|
2012-01-06 14:43:03 +00:00
|
|
|
|
|
|
|
$show_fields = woocommerce_get_customer_meta_fields();
|
2012-08-14 12:21:34 +00:00
|
|
|
|
2011-12-23 21:41:07 +00:00
|
|
|
foreach( $show_fields as $fieldset ) :
|
|
|
|
?>
|
|
|
|
<h3><?php echo $fieldset['title']; ?></h3>
|
|
|
|
<table class="form-table">
|
|
|
|
<?php
|
|
|
|
foreach( $fieldset['fields'] as $key => $field ) :
|
|
|
|
?>
|
|
|
|
<tr>
|
2012-10-16 14:46:21 +00:00
|
|
|
<th><label for="<?php echo esc_attr( $key ); ?>"><?php echo esc_html( $field['label'] ); ?></label></th>
|
2011-12-23 21:41:07 +00:00
|
|
|
<td>
|
2012-10-16 14:46:21 +00:00
|
|
|
<input type="text" name="<?php echo esc_attr( $key ); ?>" id="<?php echo esc_attr( $key ); ?>" value="<?php echo esc_attr( get_user_meta( $user->ID, $key, true ) ); ?>" class="regular-text" /><br/>
|
|
|
|
<span class="description"><?php echo wp_kses_post( $field['description'] ); ?></span>
|
2011-12-23 21:41:07 +00:00
|
|
|
</td>
|
|
|
|
</tr>
|
|
|
|
<?php
|
|
|
|
endforeach;
|
|
|
|
?>
|
|
|
|
</table>
|
|
|
|
<?php
|
|
|
|
endforeach;
|
2012-01-06 14:43:03 +00:00
|
|
|
}
|
|
|
|
|
2012-08-14 12:21:34 +00:00
|
|
|
add_action( 'show_user_profile', 'woocommerce_customer_meta_fields' );
|
|
|
|
add_action( 'edit_user_profile', 'woocommerce_customer_meta_fields' );
|
|
|
|
|
|
|
|
|
2012-01-06 14:43:03 +00:00
|
|
|
/**
|
|
|
|
* Save Address Fields on edit user pages
|
2012-08-14 12:21:34 +00:00
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @param mixed $user_id User ID of the user being saved
|
|
|
|
* @return void
|
2012-01-06 14:43:03 +00:00
|
|
|
*/
|
|
|
|
function woocommerce_save_customer_meta_fields( $user_id ) {
|
2012-08-14 12:21:34 +00:00
|
|
|
if ( ! current_user_can( 'manage_woocommerce' ) )
|
|
|
|
return $columns;
|
|
|
|
|
2012-01-06 14:43:03 +00:00
|
|
|
$save_fields = woocommerce_get_customer_meta_fields();
|
2012-08-14 12:21:34 +00:00
|
|
|
|
|
|
|
foreach( $save_fields as $fieldset )
|
|
|
|
foreach( $fieldset['fields'] as $key => $field )
|
|
|
|
if ( isset( $_POST[ $key ] ) )
|
2012-10-16 14:46:21 +00:00
|
|
|
update_user_meta( $user_id, $key, woocommerce_clean( $_POST[ $key ] ) );
|
2012-08-14 12:21:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
add_action( 'personal_options_update', 'woocommerce_save_customer_meta_fields' );
|
|
|
|
add_action( 'edit_user_profile_update', 'woocommerce_save_customer_meta_fields' );
|