Ionic 3 和 Woocommerce - retrieve/create 客户使用 woocommerce 客户 api?
Ionic 3 & Woocommerce - retrieve/create customer using woocommerce customer api?
我对 woocommerce 完全陌生。我想为商店应用程序创建一个注册页面,所以我试图检查客户在注册时输入的电子邮件 ID 是否已经存在,如果不存在则创建他的帐户,否则会提示错误 "Email id already exists".我从 https://woocommerce.github.io/woocommerce-rest-api-docs/#retrieve-a-customer 获得代码,但在检索客户 "No route was found matching the URL and request method".
时出现错误
这是我的 signup.ts 代码:
import * as WC from 'woocommerce-api';
WooCommerce: any;
newUser: any = {};
constructor()
{
this.WooCommerce = WC({
url: "http://localhost:1432/wordpress/",
consumerKey: "ck_*************************************",
consumerSecret: "cs_*************************************",
wpAPI: true, // Enable the WP REST API integration
queryStringAuth: true,
verifySsl: true,
version: 'wc/v2' // WooCommerce WP REST API version
});
this.newUser.billing_address = {};
this.newUser.shipping_address = {};
}
checkEmail()
{
let validEmail = false;
let reg = /^(([^<>()\[\]\.,;:\s@\"]+(\.[^<>()\[\]\.,;:\s@\"]+)*)|(\".+\"))@(([^<>()[\]\.,;:\s@\"]+\.)+[^<>()[\]\.,;:\s@\"]{2,})$/i;
if(reg.test(this.newUser.email))
{
this.WooCommerce.getAsync('customers/email/'+this.newUser.email)
.then((data) => {
let res = (JSON.parse(data.body));
console.log("data", res);
if(res.errors)
{
validEmail = true;
this.toastCtrl.create({
message: "Congratulations. Email is good to go!",
duration: 2000
}).present();
}
else
{
validEmail = false;
this.toastCtrl.create({
message: "Email already registered, please check.",
showCloseButton: true
}).present();
}
console.log(validEmail);
})
}
else
{
validEmail = false;
this.toastCtrl.create({
message: "Invalid Email. Please check.",
showCloseButton: true
}).present();
console.log(validEmail);
}
}
signup()
{
let customerData = {
customer : {}
}
customerData.customer = {
"email": this.newUser.email,
"first_name": this.newUser.first_name,
"last_name": this.newUser.last_name,
"username": this.newUser.username,
"password": this.newUser.password,
"billing_address": {
"first_name": this.newUser.first_name,
"last_name": this.newUser.last_name,
"company": "",
"address_1": this.newUser.billing_address.address_1,
"address_2": this.newUser.billing_address.address_2,
"city": this.newUser.billing_address.city,
"state": this.newUser.billing_address.state,
"postcode": this.newUser.billing_address.postcode,
"country": this.newUser.billing_address.country,
"email": this.newUser.email,
"phone": this.newUser.billing_address.phone,
},
"shipping_address": {
"first_name": this.newUser.first_name,
"last_name": this.newUser.last_name,
"company": "",
"address_1": this.newUser.shipping_address.address_1,
"address_2": this.newUser.shipping_address.address_2,
"city": this.newUser.shipping_address.city,
"state": this.newUser.shipping_address.state,
"postcode": this.newUser.shipping_address.postcode,
"country": this.newUser.shipping_address.country
}
}
if(this.billing_shipping_same)
{
this.newUser.shipping_address = this.newUser.shipping_address;
}
this.WooCommerce.postAsync('customers',customerData).then((data) =>{
console.log(JSON.parse(data.body));
});
}
我在检查电子邮件时遇到错误:
试试这个
WooCommerceResult:any=[];
WooCommerce.getAsync('customers/email'+this.newUser.email).then((result) => {
console.log(result.toJSON().body);
this.WooCommerceResult=result.toJSON().body;
//return Promise.resolve(JSON.parse(result.toJSON().body));
// return JSON.parse(result.toJSON().body);
});
在视图中绑定 WooCommerceResult
我认为您对 WooCommerce REST API 路线有点困惑。
您正在尝试此路线 /wp-json/wc/v2/customers/<id>
并且您将 ID 作为客户电子邮件 ID 传递。正确吗?
这不是您可以将 ID 作为客户电子邮件 ID 传递的地方。对于 ID,您必须传递客户 ID。
像这样/wp-json/wc/v2/customers/1
但是如果您试图通过电子邮件 ID 获取客户详细信息,那么您可以使用此途径。
/wp-json/wc/v2/customers?email=ajay@xyz.com
此路由 returns 分配有此电子邮件 ID ajay@xyz.com
的客户的数据。
import * as WC from 'woocommerce-api';
WooCommerce: any;
newUser: any = {};
constructor()
{
this.WooCommerce = WC({
url: "http://localhost:1432/wordpress/",
consumerKey: "ck_*************************************",
consumerSecret: "cs_*************************************",
wpAPI: true, // Enable the WP REST API integration
queryStringAuth: true,
verifySsl: true,
version: 'wc/v2' // WooCommerce WP REST API version
});
this.newUser.billing_address = {};
this.newUser.shipping_address = {};
}
checkEmail()
{
let validEmail = false;
let reg = /^(([^<>()\[\]\.,;:\s@\"]+(\.[^<>()\[\]\.,;:\s@\"]+)*)|(\".+\"))@(([^<>()[\]\.,;:\s@\"]+\.)+[^<>()[\]\.,;:\s@\"]{2,})$/i;
if(reg.test(this.newUser.email))
{
this.WooCommerce.getAsync('customers?email='+this.newUser.email)
.then((data) => {
let res = (JSON.parse(data.body));
console.log("data", res);
if(res.errors)
{
validEmail = true;
this.toastCtrl.create({
message: "Congratulations. Email is good to go!",
duration: 2000
}).present();
}
else
{
validEmail = false;
this.toastCtrl.create({
message: "Email already registered, please check.",
showCloseButton: true
}).present();
}
console.log(validEmail);
})
}
else
{
validEmail = false;
this.toastCtrl.create({
message: "Invalid Email. Please check.",
showCloseButton: true
}).present();
console.log(validEmail);
}
}
signup()
{
let customerData = {
customer : {}
}
customerData.customer = {
"email": this.newUser.email,
"first_name": this.newUser.first_name,
"last_name": this.newUser.last_name,
"username": this.newUser.username,
"password": this.newUser.password,
"billing_address": {
"first_name": this.newUser.first_name,
"last_name": this.newUser.last_name,
"company": "",
"address_1": this.newUser.billing_address.address_1,
"address_2": this.newUser.billing_address.address_2,
"city": this.newUser.billing_address.city,
"state": this.newUser.billing_address.state,
"postcode": this.newUser.billing_address.postcode,
"country": this.newUser.billing_address.country,
"email": this.newUser.email,
"phone": this.newUser.billing_address.phone,
},
"shipping_address": {
"first_name": this.newUser.first_name,
"last_name": this.newUser.last_name,
"company": "",
"address_1": this.newUser.shipping_address.address_1,
"address_2": this.newUser.shipping_address.address_2,
"city": this.newUser.shipping_address.city,
"state": this.newUser.shipping_address.state,
"postcode": this.newUser.shipping_address.postcode,
"country": this.newUser.shipping_address.country
}
}
if(this.billing_shipping_same)
{
this.newUser.shipping_address = this.newUser.shipping_address;
}
this.WooCommerce.postAsync('customers',customerData).then((data) =>{
console.log(JSON.parse(data.body));
});
}
我已经修改了您请求中的路线,如果您遇到任何问题,您可以在这里查看并告诉我。
从此删除额外的参数
this.WooCommerce = WC({
url: "http://localhost:1432/wordpress/",
consumerKey: "ck_*************************************",
consumerSecret: "cs_*************************************",
wpAPI: true, // Enable the WP REST API integration
queryStringAuth: true,
verifySsl: true,
version: 'wc/v2' // WooCommerce WP REST API version
});
到::
this.WooCommerce = WC({
url: "http://localhost:1432/wordpress/",
consumerKey: "ck_*************************************",
consumerSecret: "cs_*************************************",
});
我对 woocommerce 完全陌生。我想为商店应用程序创建一个注册页面,所以我试图检查客户在注册时输入的电子邮件 ID 是否已经存在,如果不存在则创建他的帐户,否则会提示错误 "Email id already exists".我从 https://woocommerce.github.io/woocommerce-rest-api-docs/#retrieve-a-customer 获得代码,但在检索客户 "No route was found matching the URL and request method".
时出现错误这是我的 signup.ts 代码:
import * as WC from 'woocommerce-api';
WooCommerce: any;
newUser: any = {};
constructor()
{
this.WooCommerce = WC({
url: "http://localhost:1432/wordpress/",
consumerKey: "ck_*************************************",
consumerSecret: "cs_*************************************",
wpAPI: true, // Enable the WP REST API integration
queryStringAuth: true,
verifySsl: true,
version: 'wc/v2' // WooCommerce WP REST API version
});
this.newUser.billing_address = {};
this.newUser.shipping_address = {};
}
checkEmail()
{
let validEmail = false;
let reg = /^(([^<>()\[\]\.,;:\s@\"]+(\.[^<>()\[\]\.,;:\s@\"]+)*)|(\".+\"))@(([^<>()[\]\.,;:\s@\"]+\.)+[^<>()[\]\.,;:\s@\"]{2,})$/i;
if(reg.test(this.newUser.email))
{
this.WooCommerce.getAsync('customers/email/'+this.newUser.email)
.then((data) => {
let res = (JSON.parse(data.body));
console.log("data", res);
if(res.errors)
{
validEmail = true;
this.toastCtrl.create({
message: "Congratulations. Email is good to go!",
duration: 2000
}).present();
}
else
{
validEmail = false;
this.toastCtrl.create({
message: "Email already registered, please check.",
showCloseButton: true
}).present();
}
console.log(validEmail);
})
}
else
{
validEmail = false;
this.toastCtrl.create({
message: "Invalid Email. Please check.",
showCloseButton: true
}).present();
console.log(validEmail);
}
}
signup()
{
let customerData = {
customer : {}
}
customerData.customer = {
"email": this.newUser.email,
"first_name": this.newUser.first_name,
"last_name": this.newUser.last_name,
"username": this.newUser.username,
"password": this.newUser.password,
"billing_address": {
"first_name": this.newUser.first_name,
"last_name": this.newUser.last_name,
"company": "",
"address_1": this.newUser.billing_address.address_1,
"address_2": this.newUser.billing_address.address_2,
"city": this.newUser.billing_address.city,
"state": this.newUser.billing_address.state,
"postcode": this.newUser.billing_address.postcode,
"country": this.newUser.billing_address.country,
"email": this.newUser.email,
"phone": this.newUser.billing_address.phone,
},
"shipping_address": {
"first_name": this.newUser.first_name,
"last_name": this.newUser.last_name,
"company": "",
"address_1": this.newUser.shipping_address.address_1,
"address_2": this.newUser.shipping_address.address_2,
"city": this.newUser.shipping_address.city,
"state": this.newUser.shipping_address.state,
"postcode": this.newUser.shipping_address.postcode,
"country": this.newUser.shipping_address.country
}
}
if(this.billing_shipping_same)
{
this.newUser.shipping_address = this.newUser.shipping_address;
}
this.WooCommerce.postAsync('customers',customerData).then((data) =>{
console.log(JSON.parse(data.body));
});
}
我在检查电子邮件时遇到错误:
试试这个
WooCommerceResult:any=[];
WooCommerce.getAsync('customers/email'+this.newUser.email).then((result) => {
console.log(result.toJSON().body);
this.WooCommerceResult=result.toJSON().body;
//return Promise.resolve(JSON.parse(result.toJSON().body));
// return JSON.parse(result.toJSON().body);
});
在视图中绑定 WooCommerceResult
我认为您对 WooCommerce REST API 路线有点困惑。
您正在尝试此路线 /wp-json/wc/v2/customers/<id>
并且您将 ID 作为客户电子邮件 ID 传递。正确吗?
这不是您可以将 ID 作为客户电子邮件 ID 传递的地方。对于 ID,您必须传递客户 ID。
像这样/wp-json/wc/v2/customers/1
但是如果您试图通过电子邮件 ID 获取客户详细信息,那么您可以使用此途径。
/wp-json/wc/v2/customers?email=ajay@xyz.com
此路由 returns 分配有此电子邮件 ID ajay@xyz.com
的客户的数据。
import * as WC from 'woocommerce-api';
WooCommerce: any;
newUser: any = {};
constructor()
{
this.WooCommerce = WC({
url: "http://localhost:1432/wordpress/",
consumerKey: "ck_*************************************",
consumerSecret: "cs_*************************************",
wpAPI: true, // Enable the WP REST API integration
queryStringAuth: true,
verifySsl: true,
version: 'wc/v2' // WooCommerce WP REST API version
});
this.newUser.billing_address = {};
this.newUser.shipping_address = {};
}
checkEmail()
{
let validEmail = false;
let reg = /^(([^<>()\[\]\.,;:\s@\"]+(\.[^<>()\[\]\.,;:\s@\"]+)*)|(\".+\"))@(([^<>()[\]\.,;:\s@\"]+\.)+[^<>()[\]\.,;:\s@\"]{2,})$/i;
if(reg.test(this.newUser.email))
{
this.WooCommerce.getAsync('customers?email='+this.newUser.email)
.then((data) => {
let res = (JSON.parse(data.body));
console.log("data", res);
if(res.errors)
{
validEmail = true;
this.toastCtrl.create({
message: "Congratulations. Email is good to go!",
duration: 2000
}).present();
}
else
{
validEmail = false;
this.toastCtrl.create({
message: "Email already registered, please check.",
showCloseButton: true
}).present();
}
console.log(validEmail);
})
}
else
{
validEmail = false;
this.toastCtrl.create({
message: "Invalid Email. Please check.",
showCloseButton: true
}).present();
console.log(validEmail);
}
}
signup()
{
let customerData = {
customer : {}
}
customerData.customer = {
"email": this.newUser.email,
"first_name": this.newUser.first_name,
"last_name": this.newUser.last_name,
"username": this.newUser.username,
"password": this.newUser.password,
"billing_address": {
"first_name": this.newUser.first_name,
"last_name": this.newUser.last_name,
"company": "",
"address_1": this.newUser.billing_address.address_1,
"address_2": this.newUser.billing_address.address_2,
"city": this.newUser.billing_address.city,
"state": this.newUser.billing_address.state,
"postcode": this.newUser.billing_address.postcode,
"country": this.newUser.billing_address.country,
"email": this.newUser.email,
"phone": this.newUser.billing_address.phone,
},
"shipping_address": {
"first_name": this.newUser.first_name,
"last_name": this.newUser.last_name,
"company": "",
"address_1": this.newUser.shipping_address.address_1,
"address_2": this.newUser.shipping_address.address_2,
"city": this.newUser.shipping_address.city,
"state": this.newUser.shipping_address.state,
"postcode": this.newUser.shipping_address.postcode,
"country": this.newUser.shipping_address.country
}
}
if(this.billing_shipping_same)
{
this.newUser.shipping_address = this.newUser.shipping_address;
}
this.WooCommerce.postAsync('customers',customerData).then((data) =>{
console.log(JSON.parse(data.body));
});
}
我已经修改了您请求中的路线,如果您遇到任何问题,您可以在这里查看并告诉我。
从此删除额外的参数
this.WooCommerce = WC({
url: "http://localhost:1432/wordpress/",
consumerKey: "ck_*************************************",
consumerSecret: "cs_*************************************",
wpAPI: true, // Enable the WP REST API integration
queryStringAuth: true,
verifySsl: true,
version: 'wc/v2' // WooCommerce WP REST API version
});
到::
this.WooCommerce = WC({
url: "http://localhost:1432/wordpress/",
consumerKey: "ck_*************************************",
consumerSecret: "cs_*************************************",
});