Ste*_*unn 12 account wordpress woocommerce
无论如何,以编程方式创建客户就像使用WordPress用户一样.显然,WooCommerce用户共享一些相同的WordPress用户字段,还有其他内容需要设置,如账单/邮政地址.
以前有人有过这个吗?我在他们的网站上的WooCommerce API /功能列表中找不到任何内容.
编辑:刚刚发现这个:http://docs.woothemes.com/wc-apidocs/function-wc_create_new_customer.html
但是,我怎样才能提供其他字段详细信息(如地址).
Ana*_*hah 28
WooCommerce客户本质上是一个具有额外元数据的WordPress用户.因此,一旦创建了用户,您就可以使用update_user_meta函数向其添加元数据.导航到用户 - >所有用户,编辑其中一个用户,然后向下滚动以查看字段.
下面给出了代码,为您提供有关其工作原理的要点.
$user_id = wc_create_new_customer( $email, $username, $password );
update_user_meta( $user_id, "billing_first_name", 'God' );
update_user_meta( $user_id, "billing_last_name", 'Almighty' );
.... more fields
Run Code Online (Sandbox Code Playgroud)
以下是结算和发货字段的完整列表
开票
运输
小智 7
在尝试创建新用户之前,我会查看该用户是否已存在,如果找到则更新现有用户。get_user_by(field,value) 函数返回用户对象,因此如果用户存在,则使用他们的 id 来更新元字段,或者如果不创建新的元字段。
$email = 'test@test.test';
$address = array(
'first_name' => 'Tester',
'last_name' => 'Test',
'company' => 'Testing, LLC',
'email' => $email,
'phone' => '777-777-777-777',
'address_1' => '310 S. Main Street',
'address_2' => '',
'city' => 'Las Vegas',
'state' => 'NV',
'postcode' => '89000',
'country' => 'US'
);
$default_password = wp_generate_password();
$user = get_user_by('login', $email);
if (!$user = $user->ID) $user = wp_create_user( $email, $default_password, $email );
update_user_meta( $user, "billing_first_name", $address['first_name'] );
update_user_meta( $user, "billing_last_name", $address['last_name']);
update_user_meta( $user, "billing_company", $address['company'] );
update_user_meta( $user, "billing_email", $address['email'] );
update_user_meta( $user, "billing_address_1", $address['address_1']);
update_user_meta( $user, "billing_address_2", $address['address_2'] );
update_user_meta( $user, "billing_city", $address['city']);
update_user_meta( $user, "billing_postcode", $address['postcode'] );
update_user_meta( $user, "billing_country", 'US');
update_user_meta( $user, "billing_state", $address['state'] );
update_user_meta( $user, "billing_phone", $address['phone'] );
update_user_meta( $user, "shipping_first_name", $address['first_name'] );
update_user_meta( $user, "shipping_last_name", $address['last_name']);
update_user_meta( $user, "shipping_company", $address['company'] );
update_user_meta( $user, "shipping_address_1", $address['address_1']);
update_user_meta( $user, "shipping_address_2", $address['address_2'] );
update_user_meta( $user, "shipping_city", $address['city']);
update_user_meta( $user, "shipping_postcode", $address['postcode'] );
update_user_meta( $user, "shipping_country", 'US');
update_user_meta( $user, "shipping_state", $address['state'] );
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7553 次 |
| 最近记录: |