1.0.4 • Published 11 months ago

woojs v1.0.4

Weekly downloads
-
License
MIT
Repository
github
Last release
11 months ago

wooJS

JavaScript Library for WooCommerce REST API

Extends @woocommerce/woocommerce-rest-api with more descriptive methods.


Table of Contents


Installation

npm i woojs

Usage

const Woo = require("woojs");
const woo = new Woo(url, consumerKey, consumerSecret);

How to generate REST API keys

Constructor Parameters

Woo(url, key, secret {options})
ParameterTypeRequiredDescription
urlstringyesWooCommerce store URL
keystringyesConsumer Key
secretstringyesConsumer Secret
optionsobjectnoOptions object

Options

OptionTypeDefaultDescription
versionstring'wc/v3'WooCommerce REST API version
queryStringAuthbooleanfalseUse query string authentication instead of using the Authorization header.
timeoutnumber5000Request timeout in milliseconds.
encodingstring'utf8'Request encoding.
portstring''Request port.

Coupons

Coupon Properties

AttributeTypeDescription
idintegerUnique identifier for the object READ-ONLY
codestringCoupon code. MANDATORY
amountstringAmount of discount.
date_createddate-timeThe date the coupon was created. READ-ONLY
date_created_gmtdate-timeThe date the coupon was created, as GMT. READ-ONLY
date_modifieddate-timeThe date the coupon was last modified. READ-ONLY
date_modified_gmtdate-timeThe date the coupon was last modified, as GMT. READ-ONLY
discount_typestringDetermines the type of discount that will be applied. Options: percent, fixed_cart and fixed_product. Default is fixed_cart.
descriptionstringCoupon description.
date_expiresdate-timeCoupon expiry date.
date_expires_gmtdate-timeCoupon expiry date, as GMT.
usage_countintegerNumber of times the coupon has been used already. READ-ONLY
individual_usebooleanIf true, the coupon can only be used individually. Other applied coupons will be removed from the cart. Default is false.
product_idsarrayList of product IDs the coupon can be used on.
excluded_product_idsarrayList of product IDs the coupon cannot be used on.
usage_limitintegerHow many times the coupon can be used in total.
usage_limit_per_userintegerHow many times the coupon can be used per customer.
limit_usage_to_x_itemsintegerMaximum number of items in the cart the coupon can be applied to.
free_shippingbooleanIf true and if the free shipping method requires a coupon, this coupon will enable free shipping. Default is false.
product_categoriesarrayList of category IDs the coupon can be used on.
excluded_product_categoriesarrayList of category IDs the coupon cannot be used on.
exclude_sale_itemsbooleanIf true, the coupon will not apply to items that have sale prices. Default is false.
minimum_amountstringMinimum order amount that needs to be in the cart before coupon applies.
maximum_amountstringMaximum order amount allowed when using the coupon.
email_restrictionsarrayList of email addresses that can use this coupon.
used_byarrayList of user IDs who have used the coupon. READ-ONLY
meta_dataarrayMeta data. See Coupon - Meta data properties

Coupon - Meta Data Properties

AttributeTypeDescription
idintegerMeta ID.
keystringMeta key.
valuestringMeta value.

Coupon Methods

Create Coupon

.couponCreate() or .couponAdd()

(method) Woo.couponCreate(data: object): object

(method) Woo.couponAdd(data: object): object

Creates a new coupon.

Example:

woo
  .couponCreate(data)
  .then((response) => {
    console.log(response);
  })
  .catch((error) => {
    console.log(error);
  });

data props

Retrieve Coupon

.couponRetrieve() or .couponGet()

(method) Woo.couponRetrieve(id: number): object

(method) Woo.couponGet(id: number): object

Retrieve and view a specific coupon by ID.

Example:

woo
  .couponRetrieve(id)
  .then((response) => {
    console.log(response);
  })
  .catch((error) => {
    console.log(error);
  });
woo
  .couponGet(id)
  .then((response) => {
    console.log(response);
  })
  .catch((error) => {
    console.log(error);
  });

List Coupons

.couponList() or .couponGetAll()

(method) Woo.couponList(params?: object): object

(method) Woo.couponGetAll(params?: object): object

List all coupons.

Example:

woo
  .couponList()
  .then((response) => {
    console.log(response);
  })
  .catch((error) => {
    console.log(error);
  });
woo
  .couponGetAll()
  .then((response) => {
    console.log(response);
  })
  .catch((error) => {
    console.log(error);
  });
List Coupons parameters:
ParameterTypeDescription
contextstringScope under which the request is made; determines fields present in response. Options: view and edit. Default is view.
pageintegerCurrent page of the collection. Default is 1.
per_pageintegerMaximum number of items to be returned in result set. Default is 10.
searchstringLimit results to those matching a string.
afterstringLimit response to resources published after a given ISO8601 compliant date.
beforestringLimit response to resources published before a given ISO8601 compliant date.
modified_afterstringLimit response to resources modified after a given ISO8601 compliant date.
modified_beforestringLimit response to resources modified before a given ISO8601 compliant date.
dates_are_gmtbooleanWhether to consider GMT post dates when limiting response by published or modified date.
excludearrayEnsure result set excludes specific IDs.
includearrayLimit result set to specific ids.
offsetintegerOffset the result set by a specific number of items.
orderstringOrder sort attribute ascending or descending. Options: asc and desc. Default is asc.
orderbystringSort collection by object attribute. Options: date, id, include, title, slug. Default is date.
codestringLimit result set to coupons with a specific code.

Update Coupon

.couponUpdate() or .couponEdit()

(method) Woo.couponUpdate(id: number, data: object): object

(method) Woo.couponEdit(id: number, data: object): object

Make changes to a coupon.

Example:

woo
  .couponUpdate(id, data)
  .then((response) => {
    console.log(response);
  })
  .catch((error) => {
    console.log(error);
  });

data props

Delete Coupon

.couponDelete() or .couponRemove()

(method) Woo.couponDelete(id: number, params?: object): object

(method) Woo.couponRemove(id: number, params?: object): object

Delete a coupon.

Example:

woo
  .couponDelete(id, { force: true }})
  .then((response) => {
    console.log(response);
  })
  .catch((error) => {
    console.log(error);
  });
Delete Coupon parameters:
ParameterTypeDescription
forcebooleanUse true whether to permanently delete the coupon, Default is false.

Batch Update Coupons

Note: By default it's limited to up to 100 objects to be created, updated or deleted.

.couponBatchUpdate() or .couponBatch()

(method) Woo.couponBatchUpdate(data: object): object

(method) Woo.couponBatch(data: object): object

Update multiple coupons.

Example:

const data = {
  create: [
    {
      code: "20off",
      discount_type: "percent",
      amount: "20",
      individual_use: true,
      exclude_sale_items: true,
      minimum_amount: "100.00"
    },
    {
      code: "30off",
      discount_type: "percent",
      amount: "30",
      individual_use: true,
      exclude_sale_items: true,
      minimum_amount: "100.00"
    }
  ],
  update: [
    {
      id: 719,
      minimum_amount: "50.00"
    }
  ],
  delete: [
    720
  ]
};

woo.couponBatchUpdate(, data)
  .then((response) => {
    console.log(response);
  })
  .catch((error) => {
    console.log(error);
  });

Customers

Customer properties

PropertyTypeDescription
idintegerUnique identifier for the resource. READ-ONLY
date_createddate-timeThe date the customer was created, in the site's timezone. READ-ONLY
date_created_gmtdate-timeThe date the customer was created, as GMT. READ-ONLY
date_modifieddate-timeThe date the customer was last modified, in the site's timezone. READ-ONLY
date_modified_gmtdate-timeThe date the customer was last modified, as GMT. READ-ONLY
emailstringCustomer email address. MANDATORY
first_namestringCustomer first name.
last_namestringCustomer last name.
rolestringCustomer role. READ-ONLY
usernamestringCustomer login name.
passwordstringCustomer password. WRITE-ONLY
billingobjectList of billing address data. See Customer - Billing Properties
shippingobjectList of shipping address data. See Customer - Shipping Properties
is_paying_customerbooleanShows if the customer is a paying customer. READ-ONLY
avatar_urlstringAvatar URL. READ-ONLY
meta_dataarrayMeta data. See Customer - Meta data properties

Customer - Billing Properties

AttributeTypeDescription
first_namestringFirst name.
last_namestringLast name.
companystringCompany name.
address_1stringAddress line 1.
address_2stringAddress line 2.
citystringCity name.
statestringISO code or name of the state, province or district.
postcodestringPostal code.
countrystringISO code of the country.
emailstringEmail address.
phonestringPhone number.

Customer - Shipping Properties

AttributeTypeDescription
first_namestringFirst name.
last_namestringLast name.
companystringCompany name.
address_1stringAddress line 1.
address_2stringAddress line 2.
citystringCity name.
statestringISO code or name of the state, province or district.
postcodestringPostal code.
countrystringISO code of the country.

Customer - Meta data properties

AttributeTypeDescription
idnumberMeta ID. READ-ONLY
keystringMeta key.
valuestringMeta value.

Customer methods

Create Customer

.customerCreate() or .customerAdd()

(method) Woo.customerCreate(data: object): object

(method) Woo.customerAdd(data: object): object

Create a customer.

Example:

woo
  .customerCreate(data)
  .then((response) => {
    console.log(response);
  })
  .catch((error) => {
    console.log(error);
  });

Retrieve Customer

.customerRetrieve() or .customerGet()

(method) Woo.customerRetrieve(id: number): object

(method) Woo.customerGet(id: number): object

Retrieve and view a specific customer by ID.

Example:

woo
  .customerRetrieve(1)
  .then((response) => {
    console.log(response);
  })
  .catch((error) => {
    console.log(error);
  });

List Customers

.customerList() or .customerGetAll()

(method) Woo.customerList(params?: object): object

(method) Woo.customerGetAll(params?: object): object

View all the customers.

Example:

woo
  .customerList()
  .then((response) => {
    console.log(response);
  })
  .catch((error) => {
    console.log(error);
  });
List Customers parameters
ParameterTypeDescription
contextstringScope under which the request is made; determines fields present in response. Options: view and edit. Default is view.
pageintegerCurrent page of the collection. Default is 1.
per_pageintegerMaximum number of items to be returned in result set. Default is 10.
searchstringLimit results to those matching a string.
excludearrayEnsure result set excludes specific IDs.
includearrayLimit result set to specific ids.
offsetintegerOffset the result set by a specific number of items.
orderstringOrder sort attribute ascending or descending. Options: asc and desc. Default is asc.
orderbystringSort collection by object attribute. Options: id, include, name and registered_date. Default is name.
emailstringLimit result set to resources with a specific email.
rolestringLimit result set to resources with a specific role. Options: all, administrator, editor, author, contributor, subscriber, customer and shop_manager. Default is customer.

Update Customer

.customerUpdate() or .customerEdit()

(method) Woo.customerUpdate(id: number, data: object): object

(method) Woo.customerEdit(id: number, data: object): object

Update a customer.

Example:

woo
  .customerUpdate(1, data)
  .then((response) => {
    console.log(response);
  })
  .catch((error) => {
    console.log(error);
  });

data options

Delete Customer

.customerDelete() or .customerRemove()

(method) Woo.customerDelete(id: number, params?: object): object

(method) Woo.customerRemove(id: number, params?: object): object

Delete a customer.

Example:

woo
  .customerDelete(1)
  .then((response) => {
    console.log(response);
  })
  .catch((error) => {
    console.log(error);
  });
Delete Customer parameters
ParameterTypeDescription
forcebooleanRequired to be true, as resource does not support trashing.
reassignintegerUser ID to reassign posts to.

Batch Update Customers

Note: By default it's limited to up to 100 objects to be created, updated or deleted.

.customerBatch() or .customerBatchUpdate()

(method) Woo.customerBatch(data: object): object

(method) Woo.customerBatchUpdate(data: object): object

Update multiple customers in a single request.

Example:

const data = {
  create: [
    {
      email: "john.doe2@example.com",
      first_name: "John",
      last_name: "Doe",
      username: "john.doe2",
      billing: {
        first_name: "John",
        last_name: "Doe",
        company: "",
        address_1: "969 Market",
        address_2: "",
        city: "San Francisco",
        state: "CA",
        postcode: "94103",
        country: "US",
        email: "john.doe@example.com",
        phone: "(555) 555-5555",
      },
      shipping: {
        first_name: "John",
        last_name: "Doe",
        company: "",
        address_1: "969 Market",
        address_2: "",
        city: "San Francisco",
        state: "CA",
        postcode: "94103",
        country: "US",
      },
    },
    {
      email: "joao.silva2@example.com",
      first_name: "João",
      last_name: "Silva",
      username: "joao.silva2",
      billing: {
        first_name: "João",
        last_name: "Silva",
        company: "",
        address_1: "Av. Brasil, 432",
        address_2: "",
        city: "Rio de Janeiro",
        state: "RJ",
        postcode: "12345-000",
        country: "BR",
        email: "joao.silva@example.com",
        phone: "(55) 5555-5555",
      },
      shipping: {
        first_name: "João",
        last_name: "Silva",
        company: "",
        address_1: "Av. Brasil, 432",
        address_2: "",
        city: "Rio de Janeiro",
        state: "RJ",
        postcode: "12345-000",
        country: "BR",
      },
    },
  ],
  update: [
    {
      id: 26,
      billing: {
        phone: "(11) 1111-1111",
      },
    },
  ],
  delete: [11],
};

woo
  .customerBatch(data)
  .then((response) => {
    console.log(response);
  })
  .catch((error) => {
    console.log(error);
  });

data options

Retrieve Customer Downloads

.customerDownloads()

(method) Woo.customerDownloads(id: number): object

Retrieve customer downloads permissions.

Example:

woo
  .customerDownloads(1)
  .then((response) => {
    console.log(response);
  })
  .catch((error) => {
    console.log(error);
  });

Orders

Order properties

AttributeTypeDescription
idintegerUnique identifier for the resource. READ-ONLY
parent_idintegerOrder parent ID.
numberstringOrder number. READ-ONLY
order_keystringOrder key. READ-ONLY
created_viastringShows where the order was created. READ-ONLY
versionstringVersion of WooCommerce which last updated the order. READ-ONLY
statusstringOrder status. Options: pending, processing, on-hold, completed, cancelled, refunded, failed and trash. Default is pending.
currencystringCurrency the order was created with, in ISO format. Options: AED, AFN, ALL, AMD, ANG, AOA, ARS, AUD, AWG, AZN, BAM, BBD, BDT, BGN, BHD, BIF, BMD, BND, BOB, BRL, BSD, BTC, BTN, BWP, BYR, BZD, CAD, CDF, CHF, CLP, CNY, COP, CRC, CUC, CUP, CVE, CZK, DJF, DKK, DOP, DZD, EGP, ERN, ETB, EUR, FJD, FKP, GBP, GEL, GGP, GHS, GIP, GMD, GNF, GTQ, GYD, HKD, HNL, HRK, HTG, HUF, IDR, ILS, IMP, INR, IQD, IRR, IRT, ISK, JEP, JMD, JOD, JPY, KES, KGS, KHR, KMF, KPW, KRW, KWD, KYD, KZT, LAK, LBP, LKR, LRD, LSL, LYD, MAD, MDL, MGA, MKD, MMK, MNT, MOP, MRO, MUR, MVR, MWK, MXN, MYR, MZN, NAD, NGN, NIO, NOK, NPR, NZD, OMR, PAB, PEN, PGK, PHP, PKR, PLN, PRB, PYG, QAR, RON, RSD, RUB, RWF, SAR, SBD, SCR, SDG, SEK, SGD, SHP, SLL, SOS, SRD, SSP, STD, SYP, SZL, THB, TJS, TMT, TND, TOP, TRY, TTD, TWD, TZS, UAH, UGX, USD, UYU, UZS, VEF, VND, VUV, WST, XAF, XCD, XOF, XPF, YER, ZAR and ZMW. Default is USD.
date_createddate-timeThe date the order was created, in the site's timezone. READ-ONLY
date_created_gmtdate-timeThe date the order was created, as GMT. READ-ONLY
date_modifieddate-timeThe date the order was last modified, in the site's timezone. READ-ONLY
date_modified_gmtdate-timeThe date the order was last modified, as GMT. READ-ONLY
discount_totalstringOrder discount total. READ-ONLY
discount_taxstringOrder discount tax. READ-ONLY
shipping_totalstringOrder shipping total. READ-ONLY
shipping_taxstringOrder shipping tax. READ-ONLY
cart_taxstringOrder cart tax. READ-ONLY
totalstringOrder total. READ-ONLY
total_taxstringOrder total tax. READ-ONLY
prices_include_taxbooleanAre prices inclusive of tax. READ-ONLY
customer_idintegerUser ID who owns the order. 0 for guests. Default is 0.
customer_ip_addressstringCustomer IP address. READ-ONLY
customer_user_agentstringCustomer user agent. READ-ONLY
1.0.4

11 months ago

1.0.3

1 year ago

1.0.2

1 year ago

1.0.1

1 year ago

1.0.0

1 year ago