🫢Một ví dụ dùng wc/v3 để kết nối WooCommerceRestApi
https://woocommerce.github.io/woocommerce-rest-api-docs
Example 1: Sử dụng wc/v3
package.json
{
"name": "myapp",
"version": "1.0.0",
"description": "",
"main": "app.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"dev": "node app.js"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"@woocommerce/woocommerce-rest-api": "^1.0.1",
"express": "^4.21.2"
}
}
app.js
var express = require('express');
const wooConfig = require('./wooConfig');
const WooCommerceRestApi = require("@woocommerce/woocommerce-rest-api").default;
const WooCommerce = new WooCommerceRestApi({
url: wooConfig.siteUrl,
consumerKey: wooConfig.consumerKey,
consumerSecret: wooConfig.consumerSecret,
version: 'wc/v3',
queryStringAuth: true
});
var app = express();
app.get('/', function (req, res) {
WooCommerce.get("products")
.then((response) => {
console.log(response.data);
})
.catch((error) => {
console.log(error);
});
res.send('Hello World!');
});
app.listen(3000, function () {
console.log('Example app listening on port 3000!');
});
wooConfig.js
// const wooConfig = {
// siteUrl: 'https://wpclidemo.dev',
// consumerKey: 'ck_a4d07c9f2ac8cb1e02779810a35644591dc4a7b6',
// consumerSecret: 'cs_89ba76c650eac0aff8baf1623dc4074147c2e2b8',
// };
// module.exports = wooConfig;
const wooConfig = {
siteUrl: 'https://dev-lionel1.pantheonsite.io',
consumerKey: 'ck_a07b018d4bc7fca36953f4d151ad1a3db40e5355',
consumerSecret: 'cs_fd29f1ab86335a85fec504bd6671fa68e9690875',
};
module.exports = wooConfig;
[
{
id: 82,
name: 'Product',
slug: 'product-4',
permalink: 'https://dev-lionel1.pantheonsite.io/product/product-4/',
date_created: '2025-02-08T11:44:40',
date_created_gmt: '2025-02-08T11:44:40',
date_modified: '2025-02-08T11:44:40',
date_modified_gmt: '2025-02-08T11:44:40',
type: 'simple',
status: 'publish',
featured: false,
catalog_visibility: 'visible',
description: '',
short_description: '',
sku: '',
price: '',
regular_price: '',
sale_price: '',
date_on_sale_from: null,
date_on_sale_from_gmt: null,
date_on_sale_to: null,
date_on_sale_to_gmt: null,
on_sale: false,
purchasable: false,
total_sales: 0,
virtual: false,
downloadable: false,
downloads: [],
download_limit: -1,
download_expiry: -1,
external_url: '',
button_text: '',
tax_status: 'taxable',
tax_class: '',
manage_stock: false,
stock_quantity: null,
backorders: 'no',
backorders_allowed: false,
backordered: false,
low_stock_amount: null,
sold_individually: false,
weight: '',
dimensions: { length: '', width: '', height: '' },
shipping_required: true,
shipping_taxable: true,
shipping_class: '',
shipping_class_id: 0,
reviews_allowed: true,
average_rating: '0.00',
rating_count: 0,
upsell_ids: [],
cross_sell_ids: [],
parent_id: 0,
purchase_note: '',
categories: [ [Object] ],
tags: [],
images: [],
attributes: [],
default_attributes: [],
variations: [],
grouped_products: [],
menu_order: 0,
price_html: '',
related_ids: [ 80, 81, 79, 83 ],
meta_data: [],
stock_status: 'instock',
has_options: false,
post_password: '',
global_unique_id: '',
gutenberg_blocks: [],
brands: [],
_links: { self: [Array], collection: [Array] }
},
{
id: 83,
name: 'Product',
slug: 'product-5',
permalink: 'https://dev-lionel1.pantheonsite.io/product/product-5/',
date_created: '2025-02-08T11:44:40',
date_created_gmt: '2025-02-08T11:44:40',
date_modified: '2025-02-08T11:44:40',
date_modified_gmt: '2025-02-08T11:44:40',
type: 'simple',
status: 'publish',
featured: false,
catalog_visibility: 'visible',
description: '',
short_description: '',
sku: '',
price: '',
regular_price: '',
sale_price: '',
date_on_sale_from: null,
date_on_sale_from_gmt: null,
date_on_sale_to: null,
date_on_sale_to_gmt: null,
on_sale: false,
purchasable: false,
total_sales: 0,
virtual: false,
downloadable: false,
downloads: [],
download_limit: -1,
download_expiry: -1,
external_url: '',
button_text: '',
tax_status: 'taxable',
tax_class: '',
manage_stock: false,
stock_quantity: null,
backorders: 'no',
backorders_allowed: false,
backordered: false,
low_stock_amount: null,
sold_individually: false,
weight: '',
dimensions: { length: '', width: '', height: '' },
shipping_required: true,
shipping_taxable: true,
shipping_class: '',
shipping_class_id: 0,
reviews_allowed: true,
average_rating: '0.00',
rating_count: 0,
upsell_ids: [],
cross_sell_ids: [],
parent_id: 0,
purchase_note: '',
categories: [ [Object] ],
tags: [],
images: [],
attributes: [],
default_attributes: [],
variations: [],
grouped_products: [],
menu_order: 0,
price_html: '',
related_ids: [ 81, 79, 82, 80 ],
meta_data: [],
stock_status: 'instock',
has_options: false,
post_password: '',
global_unique_id: '',
gutenberg_blocks: [],
brands: [],
_links: { self: [Array], collection: [Array] }
},
{
id: 81,
name: 'Product',
slug: 'product-3',
permalink: 'https://dev-lionel1.pantheonsite.io/product/product-3/',
date_created: '2025-02-08T11:44:39',
date_created_gmt: '2025-02-08T11:44:39',
date_modified: '2025-02-08T11:44:39',
date_modified_gmt: '2025-02-08T11:44:39',
type: 'simple',
status: 'publish',
featured: false,
catalog_visibility: 'visible',
description: '',
short_description: '',
sku: '',
price: '',
regular_price: '',
sale_price: '',
date_on_sale_from: null,
date_on_sale_from_gmt: null,
date_on_sale_to: null,
date_on_sale_to_gmt: null,
on_sale: false,
purchasable: false,
total_sales: 0,
virtual: false,
downloadable: false,
downloads: [],
download_limit: -1,
download_expiry: -1,
external_url: '',
button_text: '',
tax_status: 'taxable',
tax_class: 'zero-rate',
manage_stock: false,
stock_quantity: null,
backorders: 'no',
backorders_allowed: false,
backordered: false,
low_stock_amount: null,
sold_individually: false,
weight: '',
dimensions: { length: '', width: '', height: '' },
shipping_required: true,
shipping_taxable: true,
shipping_class: '',
shipping_class_id: 0,
reviews_allowed: true,
average_rating: '0.00',
rating_count: 0,
upsell_ids: [],
cross_sell_ids: [],
parent_id: 0,
purchase_note: '',
categories: [ [Object] ],
tags: [],
images: [],
attributes: [],
default_attributes: [],
variations: [],
grouped_products: [],
menu_order: 0,
price_html: '',
related_ids: [ 80, 83, 82, 79 ],
meta_data: [],
stock_status: 'instock',
has_options: false,
post_password: '',
global_unique_id: '',
gutenberg_blocks: [],
brands: [],
_links: { self: [Array], collection: [Array] }
},
{
id: 79,
name: 'Product',
slug: 'product',
permalink: 'https://dev-lionel1.pantheonsite.io/product/product/',
date_created: '2025-02-08T11:44:39',
date_created_gmt: '2025-02-08T11:44:39',
date_modified: '2025-02-08T11:44:39',
date_modified_gmt: '2025-02-08T11:44:39',
type: 'simple',
status: 'publish',
featured: false,
catalog_visibility: 'visible',
description: '',
short_description: '',
sku: '',
price: '',
regular_price: '',
sale_price: '',
date_on_sale_from: null,
date_on_sale_from_gmt: null,
date_on_sale_to: null,
date_on_sale_to_gmt: null,
on_sale: false,
purchasable: false,
total_sales: 0,
virtual: false,
downloadable: false,
downloads: [],
download_limit: -1,
download_expiry: -1,
external_url: '',
button_text: '',
tax_status: 'taxable',
tax_class: '',
manage_stock: false,
stock_quantity: null,
backorders: 'no',
backorders_allowed: false,
backordered: false,
low_stock_amount: null,
sold_individually: false,
weight: '',
dimensions: { length: '', width: '', height: '' },
shipping_required: true,
shipping_taxable: true,
shipping_class: '',
shipping_class_id: 0,
reviews_allowed: true,
average_rating: '0.00',
rating_count: 0,
upsell_ids: [],
cross_sell_ids: [],
parent_id: 0,
purchase_note: '',
categories: [ [Object] ],
tags: [],
images: [],
attributes: [],
default_attributes: [],
variations: [],
grouped_products: [],
menu_order: 0,
price_html: '',
related_ids: [ 80, 82, 83, 81 ],
meta_data: [],
stock_status: 'instock',
has_options: false,
post_password: '',
global_unique_id: '',
gutenberg_blocks: [],
brands: [],
_links: { self: [Array], collection: [Array] }
},
{
id: 80,
name: 'Product',
slug: 'product-2',
permalink: 'https://dev-lionel1.pantheonsite.io/product/product-2/',
date_created: '2025-02-08T11:44:39',
date_created_gmt: '2025-02-08T11:44:39',
date_modified: '2025-02-08T11:44:39',
date_modified_gmt: '2025-02-08T11:44:39',
type: 'simple',
status: 'publish',
featured: false,
catalog_visibility: 'visible',
description: '',
short_description: '',
sku: '',
price: '',
regular_price: '',
sale_price: '',
date_on_sale_from: null,
date_on_sale_from_gmt: null,
date_on_sale_to: null,
date_on_sale_to_gmt: null,
on_sale: false,
purchasable: false,
total_sales: 0,
virtual: false,
downloadable: false,
downloads: [],
download_limit: -1,
download_expiry: -1,
external_url: '',
button_text: '',
tax_status: 'taxable',
tax_class: 'reduced-rate',
manage_stock: false,
stock_quantity: null,
backorders: 'no',
backorders_allowed: false,
backordered: false,
low_stock_amount: null,
sold_individually: false,
weight: '',
dimensions: { length: '', width: '', height: '' },
shipping_required: true,
shipping_taxable: true,
shipping_class: '',
shipping_class_id: 0,
reviews_allowed: true,
average_rating: '0.00',
rating_count: 0,
upsell_ids: [],
cross_sell_ids: [],
parent_id: 0,
purchase_note: '',
categories: [ [Object] ],
tags: [],
images: [],
attributes: [],
default_attributes: [],
variations: [],
grouped_products: [],
menu_order: 0,
price_html: '',
related_ids: [ 79, 82, 81, 83 ],
meta_data: [],
stock_status: 'instock',
has_options: false,
post_password: '',
global_unique_id: '',
gutenberg_blocks: [],
brands: [],
_links: { self: [Array], collection: [Array] }
},
{
id: 77,
name: 'Hat',
slug: 'hat',
permalink: 'https://dev-lionel1.pantheonsite.io/product/hat/',
date_created: '2025-02-08T11:42:46',
date_created_gmt: '2025-02-08T11:42:46',
date_modified: '2025-02-08T11:42:46',
date_modified_gmt: '2025-02-08T11:42:46',
type: 'simple',
status: 'publish',
featured: false,
catalog_visibility: 'visible',
description: '<p>Pellentesque habitant morbi tristique senectus et netus et
malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultric
ies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. A
enean ultricies mi vitae est. Mauris placerat eleifend leo.</p>\n' +
'<p>Photography by @cottonbro.</p>\n',
short_description: '<p>This is a simple product.</p>\n',
sku: 'woo-fashion-hat',
price: '12',
regular_price: '12',
sale_price: '',
date_on_sale_from: null,
date_on_sale_from_gmt: null,
date_on_sale_to: null,
date_on_sale_to_gmt: null,
on_sale: false,
purchasable: true,
total_sales: 0,
virtual: false,
downloadable: false,
downloads: [],
download_limit: 0,
download_expiry: 0,
external_url: '',
button_text: '',
tax_status: 'taxable',
tax_class: '',
manage_stock: false,
stock_quantity: null,
backorders: 'no',
backorders_allowed: false,
backordered: false,
low_stock_amount: null,
sold_individually: false,
weight: '',
dimensions: { length: '', width: '', height: '' },
shipping_required: true,
shipping_taxable: true,
shipping_class: '',
shipping_class_id: 0,
reviews_allowed: true,
average_rating: '0.00',
rating_count: 0,
upsell_ids: [],
cross_sell_ids: [],
parent_id: 0,
purchase_note: '',
categories: [ [Object] ],
tags: [],
images: [ [Object] ],
attributes: [],
default_attributes: [],
variations: [],
grouped_products: [],
menu_order: 0,
price_html: '<span class="woocommerce-Price-amount amount"><bdi>12 <spa
n class="woocommerce-Price-currencySymbol">₫</span></bdi></span>',
related_ids: [ 69, 67 ],
meta_data: [ [Object] ],
stock_status: 'instock',
has_options: false,
post_password: '',
global_unique_id: '',
gutenberg_blocks: [ [Object] ],
brands: [],
_links: { self: [Array], collection: [Array] }
},
{
id: 75,
name: 'Shirt - Green',
slug: 'shirt-green',
permalink: 'https://dev-lionel1.pantheonsite.io/product/shirt-green/',
date_created: '2025-02-08T11:42:45',
date_created_gmt: '2025-02-08T11:42:45',
date_modified: '2025-02-08T11:42:45',
date_modified_gmt: '2025-02-08T11:42:45',
type: 'simple',
status: 'publish',
featured: false,
catalog_visibility: 'visible',
description: '<p>Pellentesque habitant morbi tristique senectus et netus et
malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultric
ies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. A
enean ultricies mi vitae est. Mauris placerat eleifend leo.</p>\n' +
'<p>Photography by @cottonbro.</p>\n',
short_description: '<p>This is a simple product.</p>\n',
sku: 'woo-fashion-shirt-green',
price: '20',
regular_price: '25',
sale_price: '20',
date_on_sale_from: null,
date_on_sale_from_gmt: null,
date_on_sale_to: null,
date_on_sale_to_gmt: null,
on_sale: true,
purchasable: true,
total_sales: 0,
virtual: false,
downloadable: false,
downloads: [],
download_limit: 0,
download_expiry: 0,
external_url: '',
button_text: '',
tax_status: 'taxable',
tax_class: '',
manage_stock: false,
stock_quantity: null,
backorders: 'no',
backorders_allowed: false,
backordered: false,
low_stock_amount: null,
sold_individually: false,
weight: '',
dimensions: { length: '', width: '', height: '' },
shipping_required: true,
shipping_taxable: true,
shipping_class: '',
shipping_class_id: 0,
reviews_allowed: true,
average_rating: '0.00',
rating_count: 0,
upsell_ids: [],
cross_sell_ids: [],
parent_id: 0,
purchase_note: '',
categories: [ [Object] ],
tags: [],
images: [ [Object] ],
attributes: [],
default_attributes: [],
variations: [],
grouped_products: [],
menu_order: 0,
price_html: '<del aria-hidden="true"><span class="woocommerce-Price-amount a
mount"><bdi>25 <span class="woocommerce-Price-currencySymbol">₫</span
></bdi></span></del> <span class="screen-reader-text">Original price was: 25&nbs
p;₫.</span><ins aria-hidden="true"><span class="woocommerce-Price-amount a
mount"><bdi>20 <span class="woocommerce-Price-currencySymbol">₫</span
></bdi></span></ins><span class="screen-reader-text">Current price is: 20 &
#8363;.</span>',
related_ids: [ 71, 63, 65 ],
meta_data: [ [Object] ],
stock_status: 'instock',
has_options: false,
post_password: '',
global_unique_id: '',
gutenberg_blocks: [ [Object] ],
brands: [],
_links: { self: [Array], collection: [Array] }
},
{
id: 73,
name: 'Jacket',
slug: 'jacket',
permalink: 'https://dev-lionel1.pantheonsite.io/product/jacket/',
date_created: '2025-02-08T11:42:44',
date_created_gmt: '2025-02-08T11:42:44',
date_modified: '2025-02-08T11:42:44',
date_modified_gmt: '2025-02-08T11:42:44',
type: 'simple',
status: 'publish',
featured: false,
catalog_visibility: 'visible',
description: '<p>Pellentesque habitant morbi tristique senectus et netus et
malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultric
ies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. A
enean ultricies mi vitae est. Mauris placerat eleifend leo.</p>\n' +
'<p>Photography by @cottonbro.</p>\n',
short_description: '<p>This is a simple product.</p>\n',
sku: 'woo-fashion-jacket',
price: '25',
regular_price: '25',
sale_price: '',
date_on_sale_from: null,
date_on_sale_from_gmt: null,
date_on_sale_to: null,
date_on_sale_to_gmt: null,
on_sale: false,
purchasable: true,
total_sales: 0,
virtual: false,
downloadable: false,
downloads: [],
download_limit: 0,
download_expiry: 0,
external_url: '',
button_text: '',
tax_status: 'taxable',
tax_class: '',
manage_stock: false,
stock_quantity: null,
backorders: 'no',
backorders_allowed: false,
backordered: false,
low_stock_amount: null,
sold_individually: false,
weight: '',
dimensions: { length: '', width: '', height: '' },
shipping_required: true,
shipping_taxable: true,
shipping_class: '',
shipping_class_id: 0,
reviews_allowed: true,
average_rating: '0.00',
rating_count: 0,
upsell_ids: [],
cross_sell_ids: [],
parent_id: 0,
purchase_note: '',
categories: [ [Object] ],
tags: [],
images: [ [Object] ],
attributes: [],
default_attributes: [],
variations: [],
grouped_products: [],
menu_order: 0,
price_html: '<span class="woocommerce-Price-amount amount"><bdi>25 <spa
n class="woocommerce-Price-currencySymbol">₫</span></bdi></span>',
related_ids: [],
meta_data: [ [Object] ],
stock_status: 'instock',
has_options: false,
post_password: '',
global_unique_id: '',
gutenberg_blocks: [ [Object] ],
brands: [],
_links: { self: [Array], collection: [Array] }
},
{
id: 71,
name: 'Shirt - Cream',
slug: 'shirt-cream',
permalink: 'https://dev-lionel1.pantheonsite.io/product/shirt-cream/',
date_created: '2025-02-08T11:42:43',
date_created_gmt: '2025-02-08T11:42:43',
date_modified: '2025-02-08T11:42:43',
date_modified_gmt: '2025-02-08T11:42:43',
type: 'simple',
status: 'publish',
featured: false,
catalog_visibility: 'visible',
description: '<p>Pellentesque habitant morbi tristique senectus et netus et
malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultric
ies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. A
enean ultricies mi vitae est. Mauris placerat eleifend leo.</p>\n' +
'<p>Photography by @cottonbro.</p>\n',
short_description: '<p>This is a simple product.</p>\n',
sku: 'woo-fashion-shirt-cream',
price: '25',
regular_price: '25',
sale_price: '',
date_on_sale_from: null,
date_on_sale_from_gmt: null,
date_on_sale_to: null,
date_on_sale_to_gmt: null,
on_sale: false,
purchasable: true,
total_sales: 0,
virtual: false,
downloadable: false,
downloads: [],
download_limit: 0,
download_expiry: 0,
external_url: '',
button_text: '',
tax_status: 'taxable',
tax_class: '',
manage_stock: true,
stock_quantity: 5,
backorders: 'no',
backorders_allowed: false,
backordered: false,
low_stock_amount: null,
sold_individually: false,
weight: '',
dimensions: { length: '', width: '', height: '' },
shipping_required: true,
shipping_taxable: true,
shipping_class: '',
shipping_class_id: 0,
reviews_allowed: true,
average_rating: '0.00',
rating_count: 0,
upsell_ids: [],
cross_sell_ids: [],
parent_id: 0,
purchase_note: '',
categories: [ [Object] ],
tags: [],
images: [ [Object] ],
attributes: [],
default_attributes: [],
variations: [],
grouped_products: [],
menu_order: 0,
price_html: '<span class="woocommerce-Price-amount amount"><bdi>25 <spa
n class="woocommerce-Price-currencySymbol">₫</span></bdi></span>',
related_ids: [ 63, 65, 75 ],
meta_data: [ [Object] ],
stock_status: 'instock',
has_options: false,
post_password: '',
global_unique_id: '',
gutenberg_blocks: [ [Object] ],
brands: [],
_links: { self: [Array], collection: [Array] }
},
{
id: 69,
name: 'Shoes',
slug: 'shoes',
permalink: 'https://dev-lionel1.pantheonsite.io/product/shoes/',
date_created: '2025-02-08T11:42:42',
date_created_gmt: '2025-02-08T11:42:42',
date_modified: '2025-02-08T11:42:42',
date_modified_gmt: '2025-02-08T11:42:42',
type: 'simple',
status: 'publish',
featured: false,
catalog_visibility: 'visible',
description: '<p>Pellentesque habitant morbi tristique senectus et netus et
malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultric
ies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. A
enean ultricies mi vitae est. Mauris placerat eleifend leo.</p>\n' +
'<p>Photography by @cottonbro.</p>\n',
short_description: '<p>This is a simple product.</p>\n',
sku: 'woo-fashion-shoes',
price: '18',
regular_price: '18',
sale_price: '',
date_on_sale_from: null,
date_on_sale_from_gmt: null,
date_on_sale_to: null,
date_on_sale_to_gmt: null,
on_sale: false,
purchasable: true,
total_sales: 0,
virtual: false,
downloadable: false,
downloads: [],
download_limit: 0,
download_expiry: 0,
external_url: '',
button_text: '',
tax_status: 'taxable',
tax_class: '',
manage_stock: true,
stock_quantity: 5,
backorders: 'no',
backorders_allowed: false,
backordered: false,
low_stock_amount: null,
sold_individually: false,
weight: '',
dimensions: { length: '', width: '', height: '' },
shipping_required: true,
shipping_taxable: true,
shipping_class: '',
shipping_class_id: 0,
reviews_allowed: true,
average_rating: '0.00',
rating_count: 0,
upsell_ids: [],
cross_sell_ids: [],
parent_id: 0,
purchase_note: '',
categories: [ [Object] ],
tags: [],
images: [ [Object] ],
attributes: [],
default_attributes: [],
variations: [],
grouped_products: [],
menu_order: 0,
price_html: '<span class="woocommerce-Price-amount amount"><bdi>18 <span class="woocommerce-Price-currencySymbol">₫</span></bdi></span>',
related_ids: [ 77, 67 ],
meta_data: [ [Object] ],
stock_status: 'instock',
has_options: false,
post_password: '',
global_unique_id: '',
gutenberg_blocks: [ [Object] ],
brands: [],
_links: { self: [Array], collection: [Array] }
}
]
PreviousA React WooCommerce Project Example With REST APINextSử dụng Nextjs kết nối store woocomerce, blog, category post, category product (ok)
Last updated
Was this helpful?