1pulse-woocommerce-rest-api 中文文档教程

发布于 3年前 浏览 25 项目主页 更新于 3年前

WooCommerce REST API - JavaScript Library

用于 WooCommerce REST API 的新 JavaScript 库,支持 CommonJS (CJS) 和嵌入式系统模块 (ESM)。

使用 Axios 库 发出请求,并提供 支持承诺

构建状态依赖状态npm version

Installation

npm install --save @woocommerce/woocommerce-rest-api

Getting started

按照此说明http://docs.woocommerce.com/document/woocommerce-rest-api/ .

查看可以在 http://woocommerce.github.io/woocommerce- 中操作的 WooCommerce API 端点和数据rest-api-文档/

Setup

ESM example:

import WooCommerceRestApi from "@woocommerce/woocommerce-rest-api";

const api = new WooCommerceRestApi({
  url: "http://example.com",
  consumerKey: "ck_XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
  consumerSecret: "cs_XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
  version: "wc/v3"
});

CJS example:

const WooCommerceRestApi = require("@woocommerce/woocommerce-rest-api").default;

const api = new WooCommerceRestApi({
  url: "http://example.com",
  consumerKey: "ck_XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
  consumerSecret: "cs_XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
  version: "wc/v3"
});

Options

OptionTypeRequiredDescription
urlStringyesYour Store URL, example: http://woo.dev/
consumerKeyStringyesYour API consumer key
consumerSecretStringyesYour API consumer secret
wpAPIPrefixStringnoCustom WP REST API URL prefix, used to support custom prefixes created with the rest_url_prefix filter
versionStringnoAPI version, default is v3
encodingStringnoEncoding, default is 'utf-8'
queryStringAuthBoolnoWhen true and using under HTTPS force Basic Authentication as query string, default is false
portstringnoProvide support for URLs with ports, eg: 8080
timeoutIntegernoDefine the request timeout
axiosConfigObjectnoDefine the custom Axios config, also override this library options

Methods

GET

  • .get(endpoint)
  • .get(endpoint, params)
ParamsTypeDescription
endpointStringWooCommerce API endpoint, example: customers or orders/12
paramsObjectQuery strings params, example: { per_page: 20 }

POST

  • .post(endpoint, data)
  • .post(endpoint, data, params)
ParamsTypeDescription
endpointStringWooCommerce API endpoint, example: customers or orders
dataObjectJS object to be converted into JSON and sent in the request
paramsObjectQuery strings params

PUT

  • .put(endpoint, data)
  • .put(endpoint, data, params)
ParamsTypeDescription
endpointStringWooCommerce API endpoint, example: customers/1 or orders/1234
dataObjectJS object to be converted into JSON and sent in the request
paramsObjectQuery strings params

DELETE

  • .delete(endpoint)
  • .delete(endpoint, params)
ParamsTypeDescription
endpointStringWooCommerce API endpoint, example: customers/2 or orders/12
paramsObjectQuery strings params, example: { force: true }

OPTIONS

  • .options(endpoint)
  • .options(endpoint, params)
ParamsTypeDescription
endpointStringWooCommerce API endpoint, example: customers/2 or orders/12
paramsObjectQuery strings params

Example of use

// import WooCommerceRestApi from "@woocommerce/woocommerce-rest-api";
const WooCommerceRestApi = require("@woocommerce/woocommerce-rest-api").default;

const api = new WooCommerceRestApi({
  url: "http://example.com",
  consumerKey: "ck_XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
  consumerSecret: "cs_XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
  version: "wc/v3"
});

// List products
api.get("products", {
  per_page: 20, // 20 products per page
})
  .then((response) => {
    // Successful request
    console.log("Response Status:", response.status);
    console.log("Response Headers:", response.headers);
    console.log("Response Data:", response.data);
    console.log("Total of pages:", response.headers['x-wp-totalpages']);
    console.log("Total of items:", response.headers['x-wp-total']);
  })
  .catch((error) => {
    // Invalid request, for 4xx and 5xx statuses
    console.log("Response Status:", error.response.status);
    console.log("Response Headers:", error.response.headers);
    console.log("Response Data:", error.response.data);
  })
  .finally(() => {
    // Always executed.
  });

// Create a product
api.post("products", {
  name: "Premium Quality", // See more in https://woocommerce.github.io/woocommerce-rest-api-docs/#product-properties
  type: "simple",
  regular_price: "21.99",
})
  .then((response) => {
    // Successful request
    console.log("Response Status:", response.status);
    console.log("Response Headers:", response.headers);
    console.log("Response Data:", response.data);
  })
  .catch((error) => {
    // Invalid request, for 4xx and 5xx statuses
    console.log("Response Status:", error.response.status);
    console.log("Response Headers:", error.response.headers);
    console.log("Response Data:", error.response.data);
  })
  .finally(() => {
    // Always executed.
  });

// Edit a product
api.put("products/1", {
  sale_price: "11.99", // See more in https://woocommerce.github.io/woocommerce-rest-api-docs/#product-properties
})
  .then((response) => {
    // Successful request
    console.log("Response Status:", response.status);
    console.log("Response Headers:", response.headers);
    console.log("Response Data:", response.data);
  })
  .catch((error) => {
    // Invalid request, for 4xx and 5xx statuses
    console.log("Response Status:", error.response.status);
    console.log("Response Headers:", error.response.headers);
    console.log("Response Data:", error.response.data);
  })
  .finally(() => {
    // Always executed.
  });

// Delete a product
api.delete("products/1", {
  force: true, // Forces to delete instead of move to the Trash
})
  .then((response) => {
    // Successful request
    console.log("Response Status:", response.status);
    console.log("Response Headers:", response.headers);
    console.log("Response Data:", response.data);
  })
  .catch((error) => {
    // Invalid request, for 4xx and 5xx statuses
    console.log("Response Status:", error.response.status);
    console.log("Response Headers:", error.response.headers);
    console.log("Response Data:", error.response.data);
  })
  .finally(() => {
    // Always executed.
  });

Changelog

查看更新日志了解详情

WooCommerce REST API - JavaScript Library

New JavaScript library for WooCommerce REST API, supports CommonJS (CJS) and Embedded System Module (ESM).

Requests are made with Axios library with support to promises.

build statusdependency statusnpm version

Installation

npm install --save @woocommerce/woocommerce-rest-api

Getting started

Generate API credentials (Consumer Key & Consumer Secret) following this instructions http://docs.woocommerce.com/document/woocommerce-rest-api/ .

Check out the WooCommerce API endpoints and data that can be manipulated in http://woocommerce.github.io/woocommerce-rest-api-docs/.

Setup

ESM example:

import WooCommerceRestApi from "@woocommerce/woocommerce-rest-api";

const api = new WooCommerceRestApi({
  url: "http://example.com",
  consumerKey: "ck_XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
  consumerSecret: "cs_XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
  version: "wc/v3"
});

CJS example:

const WooCommerceRestApi = require("@woocommerce/woocommerce-rest-api").default;

const api = new WooCommerceRestApi({
  url: "http://example.com",
  consumerKey: "ck_XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
  consumerSecret: "cs_XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
  version: "wc/v3"
});

Options

OptionTypeRequiredDescription
urlStringyesYour Store URL, example: http://woo.dev/
consumerKeyStringyesYour API consumer key
consumerSecretStringyesYour API consumer secret
wpAPIPrefixStringnoCustom WP REST API URL prefix, used to support custom prefixes created with the rest_url_prefix filter
versionStringnoAPI version, default is v3
encodingStringnoEncoding, default is 'utf-8'
queryStringAuthBoolnoWhen true and using under HTTPS force Basic Authentication as query string, default is false
portstringnoProvide support for URLs with ports, eg: 8080
timeoutIntegernoDefine the request timeout
axiosConfigObjectnoDefine the custom Axios config, also override this library options

Methods

GET

  • .get(endpoint)
  • .get(endpoint, params)
ParamsTypeDescription
endpointStringWooCommerce API endpoint, example: customers or orders/12
paramsObjectQuery strings params, example: { per_page: 20 }

POST

  • .post(endpoint, data)
  • .post(endpoint, data, params)
ParamsTypeDescription
endpointStringWooCommerce API endpoint, example: customers or orders
dataObjectJS object to be converted into JSON and sent in the request
paramsObjectQuery strings params

PUT

  • .put(endpoint, data)
  • .put(endpoint, data, params)
ParamsTypeDescription
endpointStringWooCommerce API endpoint, example: customers/1 or orders/1234
dataObjectJS object to be converted into JSON and sent in the request
paramsObjectQuery strings params

DELETE

  • .delete(endpoint)
  • .delete(endpoint, params)
ParamsTypeDescription
endpointStringWooCommerce API endpoint, example: customers/2 or orders/12
paramsObjectQuery strings params, example: { force: true }

OPTIONS

  • .options(endpoint)
  • .options(endpoint, params)
ParamsTypeDescription
endpointStringWooCommerce API endpoint, example: customers/2 or orders/12
paramsObjectQuery strings params

Example of use

// import WooCommerceRestApi from "@woocommerce/woocommerce-rest-api";
const WooCommerceRestApi = require("@woocommerce/woocommerce-rest-api").default;

const api = new WooCommerceRestApi({
  url: "http://example.com",
  consumerKey: "ck_XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
  consumerSecret: "cs_XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
  version: "wc/v3"
});

// List products
api.get("products", {
  per_page: 20, // 20 products per page
})
  .then((response) => {
    // Successful request
    console.log("Response Status:", response.status);
    console.log("Response Headers:", response.headers);
    console.log("Response Data:", response.data);
    console.log("Total of pages:", response.headers['x-wp-totalpages']);
    console.log("Total of items:", response.headers['x-wp-total']);
  })
  .catch((error) => {
    // Invalid request, for 4xx and 5xx statuses
    console.log("Response Status:", error.response.status);
    console.log("Response Headers:", error.response.headers);
    console.log("Response Data:", error.response.data);
  })
  .finally(() => {
    // Always executed.
  });

// Create a product
api.post("products", {
  name: "Premium Quality", // See more in https://woocommerce.github.io/woocommerce-rest-api-docs/#product-properties
  type: "simple",
  regular_price: "21.99",
})
  .then((response) => {
    // Successful request
    console.log("Response Status:", response.status);
    console.log("Response Headers:", response.headers);
    console.log("Response Data:", response.data);
  })
  .catch((error) => {
    // Invalid request, for 4xx and 5xx statuses
    console.log("Response Status:", error.response.status);
    console.log("Response Headers:", error.response.headers);
    console.log("Response Data:", error.response.data);
  })
  .finally(() => {
    // Always executed.
  });

// Edit a product
api.put("products/1", {
  sale_price: "11.99", // See more in https://woocommerce.github.io/woocommerce-rest-api-docs/#product-properties
})
  .then((response) => {
    // Successful request
    console.log("Response Status:", response.status);
    console.log("Response Headers:", response.headers);
    console.log("Response Data:", response.data);
  })
  .catch((error) => {
    // Invalid request, for 4xx and 5xx statuses
    console.log("Response Status:", error.response.status);
    console.log("Response Headers:", error.response.headers);
    console.log("Response Data:", error.response.data);
  })
  .finally(() => {
    // Always executed.
  });

// Delete a product
api.delete("products/1", {
  force: true, // Forces to delete instead of move to the Trash
})
  .then((response) => {
    // Successful request
    console.log("Response Status:", response.status);
    console.log("Response Headers:", response.headers);
    console.log("Response Data:", response.data);
  })
  .catch((error) => {
    // Invalid request, for 4xx and 5xx statuses
    console.log("Response Status:", error.response.status);
    console.log("Response Headers:", error.response.headers);
    console.log("Response Data:", error.response.data);
  })
  .finally(() => {
    // Always executed.
  });

Changelog

See changelog for details

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文