Skip to content

Commit a2c7be0

Browse files
authored
Merge pull request #3 from imranhsayed/feature/get-products-api
Get Products API
2 parents d101467 + 98efefd commit a2c7be0

File tree

6 files changed

+101
-7
lines changed

6 files changed

+101
-7
lines changed

.env-example

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,4 @@
11
NEXT_PUBLIC_WORDPRESS_SITE_URL=https://example.com
2+
3+
WC_CONSUMER_KEY=ck_xx
4+
WC_CONSUMER_SECRET=cs_xx

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ npm run dev
5454
1. (Required) Create a `.env` file taking reference from `.env-example` and update your WordPressSite URL.
5555
- `NEXT_PUBLIC_WORDPRESS_URL=https://example.com`
5656

57+
2. Add your `WC_CONSUMER_KEY` and `WC_CONSUMER_SECRET` to the `.env` by following [WooCommerce > Settings > Advanced > REST API](https://woocommerce.github.io/woocommerce-rest-api-docs/#authentication)
58+
5759
2. In your WordPress Dashboard, Go to Settings > General > Site Address (URL) ( Set this to Frontend URL e.g. http://localhost:3000 during development )
5860
3. Create the Header and Footer Menus In WordPress Dashboard and set them to HCMS Header menu and HCMS Footer Menu respectively.
5961

package-lock.json

Lines changed: 61 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@
66
"dev": "next dev",
77
"build": "next build",
88
"start": "next start",
9-
"svg": "svgr -d src/components/icons src/components/icons/svgs"
9+
"svg": "svgr -d src/components/icons src/components/icons/svgs"
1010
},
1111
"dependencies": {
1212
"@svgr/cli": "^5.5.0",
13+
"@woocommerce/woocommerce-rest-api": "^1.0.1",
1314
"axios": "^0.21.1",
1415
"dompurify": "^2.3.1",
1516
"lodash": "^4.17.21",

pages/api/get-products.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
const WooCommerceRestApi = require("@woocommerce/woocommerce-rest-api").default;
2+
3+
const api = new WooCommerceRestApi({
4+
url: process.env.NEXT_PUBLIC_WORDPRESS_SITE_URL,
5+
consumerKey: process.env.WC_CONSUMER_KEY,
6+
consumerSecret: process.env.WC_CONSUMER_SECRET,
7+
version: "wc/v3"
8+
});
9+
10+
export default async function handler(req, res) {
11+
const responseData = {
12+
success: false,
13+
products: []
14+
}
15+
16+
try {
17+
const { data } = await api.get(
18+
'products',
19+
{
20+
per_page: 50
21+
}
22+
);
23+
24+
responseData.success = true;
25+
responseData.products = data;
26+
27+
res.json( responseData );
28+
29+
} catch ( error ) {
30+
responseData.error = error.message;
31+
res.status( 500 ).json( responseData );
32+
}
33+
}

pages/api/hello.js

Lines changed: 0 additions & 6 deletions
This file was deleted.

0 commit comments

Comments
 (0)