离子3& Woocommerce - 使用woocommerce客户api检索/创建客户?

时间:2018-03-14 06:54:09

标签: wordpress ionic-framework woocommerce ionic3 woocommerce-rest-api

我对woocommerce完全陌生。我想为商店应用创建一个注册页面,所以我试图检查客户在注册时输入的电子邮件ID是否已经存在,如果没有,则创建他的帐户或者提示错误& #34;电子邮件ID已存在"。我从https://woocommerce.github.io/woocommerce-rest-api-docs/#retrieve-a-customer获得了代码,但是在检索客户时发现了一个错误"找不到与URL和请求方法匹配的路由"。

这是我的signup.ts代码:

testVal

我在查看电子邮件时遇到错误:

Error

3 个答案:

答案 0 :(得分:3)

我认为你对WooCommerce REST API Route有点困惑。

您尝试此路由/wp-json/wc/v2/customers/1并且您将ID作为客户电子邮件ID传递。正确的吗?

这不是您可以将ID作为客户电子邮件ID传递的位置。对于id,您必须传递客户ID。

像这样/wp-json/wc/v2/customers?email=ajay@xyz.com

但是,如果您尝试通过电子邮件ID获取客户详细信息,则可以使用此路线。

ajay@xyz.com

此路线返回分配了此电子邮件ID 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)); }); } 的客户的数据。

push = false

我已经修改了您的请求中的路线,您可以查看并在此告诉我您遇到任何问题。

答案 1 :(得分:1)

试试这个

  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

答案 2 :(得分:0)

从此删除多余的参数

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_*************************************",

   });