WC_Customer是WooCommerce中操作用户的一个类,源代码在woocommerce/includes/class-wc-customer.php
中。本文简单介绍这个类所包含的方法和属性。
目录
方法
$customer->get_id();
$customer->get_avatar_url();
// 税费计算地址,返回国家、省、市和邮编,根据后台的设置不同,税收地址可以是配送地址、账单地址或店铺地址
$customer->get_taxable_address();
//获取该用户的所有可下载产品
$customer->get_downloadable_products();
//是否免税
$customer->is_vat_exempt();
$customer->get_calculated_shipping();
$customer->get_last_order();
$customer->get_order_count();
$customer->get_total_spent();
$customer->has_calculated_shipping();
$customer->has_shipping_address();
$customer->set_is_vat_exempt( $is_vat_exempt );
$customer->set_calculated_shipping( $calculated = true );
$customer->set_password( $password );
//保存数据到数据库
$customer->save();
Getters
$customer->get_username();
$customer->get_email();
$customer->get_first_name();
$customer->get_last_name();
$customer->get_display_name();
$customer->get_role();
$customer->get_date_created();
$customer->get_date_modified();
$customer->get_billing(); // 账单详细地址
$customer->get_billing_first_name();
$customer->get_billing_last_name();
$customer->get_billing_company();
$customer->get_billing_address();
$customer->get_billing_address_1();
$customer->get_billing_address_2();
$customer->get_billing_city();
$customer->get_billing_state();
$customer->get_billing_postcode();
$customer->get_billing_country();
$customer->get_billing_email();
$customer->get_billing_phone();
$customer->get_shipping(); //配送详细地址
$customer->get_shipping_first_name();
$customer->get_shipping_last_name();
$customer->get_shipping_company();
$customer->get_shipping_address();
$customer->get_shipping_address_1();
$customer->get_shipping_address_2();
$customer->get_shipping_city();
$customer->get_shipping_state();
$customer->get_shipping_postcode();
$customer->get_shipping_country();
$customer->get_shipping_phone();
$customer->get_is_paying_customer();
Setters
$customer->set_username( $username );
$customer->set_email( $value );
$customer->set_first_name( $first_name );
$customer->set_last_name( $last_name );
$customer->set_display_name( $display_name );
$customer->set_role( $role );
$customer->set_date_created( $date );
$customer->set_date_modified( $date );
$customer->set_billing_address_to_base(); //账单地址的国家和省的信息使用店铺地址
$customer->set_billing_location( $country, $state = '', $postcode = '', $city = '' );
$customer->set_billing_first_name( $value );
$customer->set_billing_last_name( $value );
$customer->set_billing_company( $value );
$customer->set_billing_address( $value );
$customer->set_billing_address_1( $value );
$customer->set_billing_address_2( $value );
$customer->set_billing_city( $value );
$customer->set_billing_state( $value );
$customer->set_billing_postcode( $value );
$customer->set_billing_country( $value );
$customer->set_billing_email( $value );
$customer->set_billing_phone( $value );
$customer->set_shipping_address_to_base();
$customer->set_shipping_location( $country, $state = '', $postcode = '', $city = '' );
$customer->set_shipping_first_name( $value );
$customer->set_shipping_last_name( $value );
$customer->set_shipping_company( $value );
$customer->set_shipping_address( $value );
$customer->set_shipping_address_1( $value );
$customer->set_shipping_address_2( $value );
$customer->set_shipping_city( $value );
$customer->set_shipping_state( $value );
$customer->set_shipping_postcode( $value );
$customer->set_shipping_country( $value );
$customer->set_shipping_phone( $value );
$customer->set_is_paying_customer( $is_paying_customer );
WC_Customer代码实例
初始化一个类的实例,包含的数据是当前登录用户的信息。
$customer = new WC_Customer(get_current_user_id());
修改用户信息,并保存到数据库
$customer = new WC_Customer(get_current_user_id());
$customer->set_email('myemail@email.com');
$customer->save();
WooCommerce Customer Functions
除了使用WC_Customer类来操作用户,WooCommerce还提供了一些封装好的函数,使用更方便,源代码位于woocommerce/includes/wc-user-functions.php
文件中。
某些方法是基于WC_Customers方法的封装函数,例如:
wc_get_customer_total_spent( $user_id );
wc_get_customer_order_count( $user_id );
wc_get_customer_last_order( $customer_id );
还有一些比较有用的函数,简单列举一下。
wc_create_new_customer( $email, $username = ”, $password = ”, $args = array() )
创建一个用户,$args接受的参数可以参考wp_insert_user()函数,例如:
$userdata = array(
'user_nicename' => 'mingzhang',
'user_url' => 'http://myblog.me',
'display_name' => '我的名字',
'nickname' => '昵称',
'first_name' => '晓明',
'last_name' => '张',
'description' => '这个人很懒,什么也没写',
'rich_editing' => true,
'syntax_highlighting' => true,
'comment_shortcuts' => true,
'show_admin_bar_front' => false,
'role' => 'customer',
);
$user_id = wc_create_new_customer( 'mingzhang@domain.com', 'mingzhang','mypassword', $userdata );
if( is_wp_error($user_id) ){
$error_code = array_key_first( $user_id->errors );
$error_message = $user_id->errors[$error_code][0];
echo '<pre> ', var_dump( $error_code,$error_message ) ,'</pre>';
} else {
echo '用户成功创建';
}
wc_create_new_customer_username( $email, $new_user_args = array(), $suffix = ” )
返回一个唯一的用户名。
wc_create_new_customer_username('whatever@email.com', array('first_name' => 'Lily', 'last_name' => 'chen'), '.Sola' );
// 返回:"lily.chen.Sola"
wc_set_customer_auth_cookie( $customer_id )
将当前站点的登录用户设为$customer_id指定的用户,跟这个用户自己登录没区别。
function login_customer_by_id(){
wc_set_customer_auth_cookie(5);
}
add_action( 'init', 'login_customer_by_id' );
wc_update_new_customer_past_orders( $customer_id )
根据$customer_id对应的用户的email,查询过往订单,并与当前用户关联。如果一个用户曾经用guest身份下单,后来用下单的邮箱注册了一个账户,但账户里却没有过往订单,本函数可解决这个问题。
WooCommerce自带链接过往订单的功能,位于WooCommerce->报表->客户->客户列表
下。Automatically Link WooCommerce Orders at Customer Registration这篇文章详细介绍了使用方法。
wc_customer_bought_product( $customer_email, $user_id, $product_id )
根据用户email或ID查询用户是否购买过某个产品,例如
wc_customer_bought_product( 'jiedaodizhi@gmail.com', false, 265 );
wc_current_user_has_role( $role )
检查当前登录用户是否有具有某用户角色,例如:
wc_current_user_has_role('subscriber')
针对所有用户的版本为:wc_user_has_role( $user, $role )
。
邻居来串串门
欢迎欢迎!