Ionic 3和Woocommerce - 使用woocommerce客户API检索/创建客户?

问题描述 投票:3回答:3

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

这是我的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));
     });
}

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

Error

wordpress ionic-framework woocommerce ionic3 woocommerce-rest-api
3个回答
3
投票

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

您尝试这条路线/wp-json/wc/v2/customers/<id>,并且您将id作为客户电子邮件ID传递。正确?

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

像这样的/wp-json/wc/v2/customers/1

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

/wp-json/wc/v2/[email protected]

此路线返回分配了此电子邮件ID [email protected]的客户的数据。

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));
     });
}

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


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


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

   });
© www.soinside.com 2019 - 2024. All rights reserved.