Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I'm completely new to woocommerce. I want to create a sign up page for shop app, so I'm trying to check if the email id entered by a customer while signing up is already exists or not, if not then create his account or else prompt the error "Email id already exists". I got the code from https://woocommerce.github.io/woocommerce-rest-api-docs/#retrieve-a-customer but it is giving me an error while retrieving customer "No route was found matching the URL and request method".

here is my signup.ts code:

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 i'm getting while checking email:

Error

like image 484
Shreyas Pednekar Avatar asked Nov 29 '25 16:11

Shreyas Pednekar


1 Answers

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/[email protected]

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

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

like image 51
Ajay Ghaghretiya Avatar answered Dec 02 '25 05:12

Ajay Ghaghretiya