|
1 | 1 | # Magento 2 Connector |
2 | 2 |
|
3 | | -[Adobe Magento 2](https://devdocs.magento.com/guides/v2.3/graphql/) connector for [React Storefront](https://github.com/storefront-foundation/react-storefront). |
| 3 | +The [React Storefront](https://github.com/storefront-foundation/react-storefront) headless ecommerce connector for [Adobe Magento 2](https://devdocs.magento.com/guides/v2.3/graphql/). |
4 | 4 |
|
5 | | -## Requirements |
| 5 | +This guide covers how to get up and running with the Magento 2 Connector. For information on connectors in general and how to write your own connector refer to the [React Storefront Connectors](https://docs.reactstorefront.io/guides/connectors) documentation. |
6 | 6 |
|
7 | | -`M2_CONFIG_HOST` Node.js Environment Variable defining your origin site based on Magento 2. |
| 7 | +## Requirements |
8 | 8 |
|
9 | | -See `.env.sample` file as an example of adding env variable via [dotenv](https://www.npmjs.com/package/dotenv). You can also check [this guide](https://www.twilio.com/blog/working-with-environment-variables-in-node-js-html) to get more info about Node.js Environment Variables. |
| 9 | +You will need a Magento 2 (sometimes referred to as "M2") backend to try out the connector. |
10 | 10 |
|
11 | | -## Usage |
| 11 | +## Running Locally |
12 | 12 |
|
13 | | -Initialize config environment variables by using |
| 13 | +Create a new React Storefront app using version 8.14.0 or later: |
14 | 14 |
|
15 | | -```js |
16 | | -require('dotenv').config() |
17 | 15 | ``` |
18 | | - |
19 | | -in one of your server files (for example `server.js` in NextJS). |
20 | | - |
21 | | -Then, you can simply import normalizer and fetch helper in your backend handlers. |
22 | | - |
23 | | -### Product data: |
24 | | - |
25 | | -```js |
26 | | -import { fetchProduct, normalizeProduct } from 'react-storefront-magento2-connector/product'; |
27 | | -... |
28 | | - |
29 | | -const rawProduct = await fetchProduct(productId); |
30 | | -// normalize it for React Storefront https://docs.reactstorefront.io/guides/product |
31 | | -const product = normalizeProduct(rawProduct, productId); |
| 16 | +npm create react-storefront@^8.014.0 my-m2-app |
32 | 17 | ``` |
33 | 18 |
|
34 | | -### Product reviews: |
| 19 | +Next `cd` into your created application and install the Magento2 connector: |
35 | 20 |
|
36 | | -```js |
37 | | -import fetchProductReviews from 'react-storefront-magento2-connector/product/reviews/fetchProductReviews'; |
38 | | -... |
39 | | -// Magento API returns HTML as result for reviews, so no need to normalize the data |
40 | | -const data = await fetchProductReviews(productId); |
41 | | -... |
42 | 21 | ``` |
43 | | - |
44 | | -### CMS slots: |
45 | | - |
46 | | -```js |
47 | | - import { fetchCmsBlocks, normalizeCmsBlocks } from 'react-storefront-magento2-connector/cms/blocks'; |
48 | | - ... |
49 | | - // identifiers is a string with CMS block id |
50 | | - const rawData = await fetchCmsBlocks({ identifiers }); |
51 | | - const data = normalizeCmsBlocks(rawData); |
52 | | - ... |
| 22 | +cd my-m2-app |
| 23 | +npm install react-storefront-magento2-connector |
53 | 24 | ``` |
54 | 25 |
|
55 | | -### Search |
| 26 | +Next configure the `M2_CONFIG_HOST` environment variable in `.env` file to point to your Magento2 backend. See `.env.sample` file as an example of adding env variable via [dotenv](https://www.npmjs.com/package/dotenv). You can also check [this guide](https://www.twilio.com/blog/working-with-environment-variables-in-node-js-html) to get more info about Node.js Environment Variables. For example, your `.env` file may look like: |
56 | 27 |
|
57 | | -```js |
58 | | - import { fetchSearch, normalizeSearch } from 'react-storefront-magento2-connector/search'; |
59 | | - ... |
60 | | -// where "search" is a search term |
61 | | - const rawData = await fetchSearch({ search }); |
62 | | -// normalize it to follow React Storefront data contract https://docs.reactstorefront.io/guides/search |
63 | | - const data = normalizeSearch(rawData); |
64 | | - ... |
65 | 28 | ``` |
66 | | - |
67 | | -### Subcategory |
68 | | - |
69 | | -#### Subcategory data |
70 | | - |
71 | | -```js |
72 | | - import { fetchSubcategory, normalizeSubcategory } from 'react-storefront-magento2-connector/subcategory'; |
73 | | - ... |
74 | | - const rawData = await fetchSubcategory({ categoryId }); |
75 | | -// normalize data to follow React Storefront data contract https://docs.reactstorefront.io/guides/subcategory |
76 | | - const { id, name } = normalizeSubcategory(rawData); |
| 29 | +LEGACY_BACKEND_DOMAIN=www.my-magento-site.com |
| 30 | +LEGACY_BACKEND_HOST_HEADER=www.my-magento-site.com |
| 31 | +M2_CONFIG_HOST=http://www.my-magento-site.com |
77 | 32 | ``` |
78 | 33 |
|
79 | | -#### Subcategory id by URL key |
80 | | - |
81 | | -```js |
82 | | - import { fetchSubcategoryId, normalizeSubcategoryId } from 'react-storefront-magento2-connector/subcategory/id'; |
83 | | - ... |
84 | | - const rawData = await fetchSubcategoryId({ urlKey }); |
85 | | - const id = normalizeSubcategoryId(rawData); |
86 | | - ... |
87 | | -``` |
| 34 | +Finally set the connector in your `next.config.js` file. By default this file is set to use the `react-storefront/mock-connector` as shown below: |
88 | 35 |
|
89 | | -#### Nested subcategories by URL key |
90 | | - |
91 | | -```js |
92 | | - import { |
93 | | - fetchSubcategorySubCategories, |
94 | | - normalizeSubcategorySubCategories, |
95 | | - } from 'react-storefront-magento2-connector/subcategory/sub-categories'; |
96 | | - ... |
97 | | - const rawData = await fetchSubcategorySubCategories({ urlKey }); |
98 | | - const data = normalizeSubcategorySubCategories(rawData); |
99 | | - ... |
100 | 36 | ``` |
| 37 | +module.exports = withReactStorefront({ |
101 | 38 |
|
102 | | -### Menu |
| 39 | + // ... Some code |
| 40 | + |
| 41 | + connector: 'react-storefront/mock-connector', |
103 | 42 |
|
104 | | -```js |
105 | | - import { fetchMenu, normalizeMenu } from 'react-storefront-magento2-connector/menu'; |
106 | | - ... |
107 | | - const rawData = await fetchMenu({}); |
108 | | - const data = normalizeMenu(rawData); |
109 | | - ... |
| 43 | + // ... More code |
110 | 44 | ``` |
111 | 45 |
|
112 | | -### Helpers: |
| 46 | +Change this line to use the `react-storefront-magento2-connector` as shown below: |
113 | 47 |
|
114 | | -Catch / properly handle error message from GraphQL response |
| 48 | +``` |
| 49 | +module.exports = withReactStorefront({ |
115 | 50 |
|
116 | | -```js |
117 | | -import getError from 'react-storefront-magento2-connector/helpers/getError'; |
118 | | -... |
119 | | -const rawData = await fetchProduct(productId); |
| 51 | + // ... Some code |
| 52 | + |
| 53 | + connector: 'react-storefront-magento2-connector', |
120 | 54 |
|
121 | | -const error = getError(rawData); |
122 | | -if (error) { |
123 | | - return { |
124 | | - error, |
125 | | - }; |
126 | | -} |
| 55 | + // ... More code |
127 | 56 | ``` |
128 | 57 |
|
129 | | -### Cart |
130 | | - |
131 | | -[Add to cart](examples/next/cart/common/addToCart.js) |
| 58 | +Now you can run your project locally, |
132 | 59 |
|
133 | | -[Merge carts](examples/next/cart/common/mergeCarts.js) |
| 60 | +``` |
| 61 | +npm start |
| 62 | +``` |
134 | 63 |
|
135 | | -[Create customer](examples/next/cart/customer/createCustomer.js) |
| 64 | +And then visit http://127.0.0.1:3000 in your browser. |
136 | 65 |
|
137 | | -[Generate token](examples/next/cart/customer/generateToken.js) |
| 66 | +## Deploying to the Moovweb XDN |
138 | 67 |
|
139 | | -[Revoke token](examples/next/cart/customer/revokeToken.js) |
| 68 | +The front-end React Storefront can be hosted anywhere that supports Node and Express but it works great on the [Moovweb XDN](https://www.moovweb.com/). You can try the XDN for free by signing up [here](https://moovweb.app/signup?redirectTo=/). Once you have an account you can deploy it by running `xdn deploy`: |
140 | 69 |
|
141 | | -[Guest cart](examples/next/cart/guest/cart.js) |
| 70 | +``` |
| 71 | +xdn deploy |
| 72 | +``` |
142 | 73 |
|
143 | | -[Obtain session / create empty cart](examples/next/cart/guest/obtainSession.js) |
| 74 | +Refer to the [XDN deployment guide](https://developer.moovweb.com/guides/deploying) for more information. |
144 | 75 |
|
145 | | -#### Get cart data |
146 | 76 |
|
147 | | -```js |
148 | | - import { fetchCart, normalizeCart } from 'react-storefront-magento2-connector/cart/customer/cart'; |
149 | | - ... |
150 | | - const rawData = await fetchCart(token); |
151 | | - const data = normalizeCart(rawData); |
152 | | - ... |
153 | | -``` |
154 | 77 |
|
155 | | -## Examples |
156 | 78 |
|
157 | | -- See `/examples/react/` for [ReactJS](https://reactjs.org/) stuff examples |
158 | | -- See `/examples/rsf/` for [React Storefront](https://github.com/storefront-foundation/react-storefront) handlers examples |
159 | | -- See `/examples/next/` for [NextJS](https://nextjs.org/) handlers examples (they are also can be used in RSF v7+) |
0 commit comments