Ionic 3 & Woocommerce - retrieve/create customer using woocommerce customer api? Ionic 3 & Woocommerce - retrieve/create customer using woocommerce customer api? wordpress wordpress

Ionic 3 & Woocommerce - retrieve/create customer using woocommerce customer api?


I think you are the little bit confused with the WooCommerce REST API Route.

You trying this route /wp-json/wc/v2/customers/<id> and you are passing the id as customer email id. Correct?

This is not which where you can pass the id as customer email id. for id you have to pass the customer id.

Something like this /wp-json/wc/v2/customers/1

But if you are trying to get customer details by the email id then you can use this route.

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

This route returns the data of the customer who are assigned with this email 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));     });}

I have modified the route in your request you can check and let me know here i you are facing any issue.


try this

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

Bind WooCommerceResult in a view


remove extra parameters from this

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

to::

this.WooCommerce = WC({      url: "http://localhost:1432/wordpress/",      consumerKey: "ck_*************************************",      consumerSecret: "cs_*************************************",   });