File tree 4 files changed +84
-19
lines changed
4 files changed +84
-19
lines changed Original file line number Diff line number Diff line change
1
+ import { isEmpty } from 'lodash' ;
2
+
3
+ import axios from 'axios' ;
4
+ import { ADD_TO_CART_ENDPOINT } from '../../utils/constants/endpoints' ;
5
+
6
+ const AddToCart = ( { product } ) => {
7
+
8
+ if ( isEmpty ( product ) ) {
9
+ return null ;
10
+ }
11
+
12
+ const addToCart = ( productId , qty = 1 ) => {
13
+ axios . post ( ADD_TO_CART_ENDPOINT , {
14
+ product_id : productId ,
15
+ quantity : qty ,
16
+ } ,
17
+ {
18
+ withCredentials : true ,
19
+ headers : {
20
+ 'X-Headless-CMS' : true ,
21
+ } ,
22
+ } )
23
+ . then ( ( res ) => {
24
+ console . log ( 'card added' , res ) ;
25
+ } )
26
+ . catch ( err => {
27
+ console . log ( 'err' , err ) ;
28
+ } ) ;
29
+ } ;
30
+
31
+
32
+ return (
33
+ < button className = "bg-white hover:bg-gray-100 text-gray-800 font-semibold py-2 px-4 border border-gray-400 rounded shadow" onClick = { ( ) => addToCart ( product ?. id ) } > Add to cart</ button >
34
+ ) ;
35
+ }
36
+
37
+ export default AddToCart
Original file line number Diff line number Diff line change 1
1
import { isArray , isEmpty } from 'lodash' ;
2
- import Link from 'next/link' ;
3
- import Image from '../image' ;
4
- import { sanitize } from '../../utils/miscellaneous' ;
2
+ import Product from './product' ;
5
3
6
4
const Products = ( { products } ) => {
7
5
8
6
if ( isEmpty ( products ) || ! isArray ( products ) ) {
9
7
return null ;
10
8
}
11
9
10
+ console . log ( 'products' , products ) ;
11
+
12
12
return (
13
13
< div className = "flex flex-wrap -mx-2 overflow-hidden" >
14
14
15
15
{ products . length ? products . map ( product => {
16
- const img = product ?. images ?. [ 0 ] ?? { } ;
17
16
return (
18
- < div key = { product ?. id } className = "my-2 px-2 w-full overflow-hidden sm:w-1/2 md:w-1/3 xl:w-1/4" >
19
- < Link href = { product ?. permalink ?? '/' } >
20
- < a >
21
- < Image
22
- sourceUrl = { img ?. src ?? '' }
23
- altText = { img ?. alt ?? '' }
24
- title = { product ?. name ?? '' }
25
- width = "380"
26
- height = "380"
27
- />
28
- < h3 className = "font-bold uppercase" > { product ?. name ?? '' } </ h3 >
29
- < div dangerouslySetInnerHTML = { { __html : sanitize ( product ?. price_html ?? '' ) } } />
30
- </ a >
31
- </ Link >
32
- </ div >
17
+ < Product key = { product ?. id } product = { product } />
33
18
)
34
19
} ) : null }
35
20
Original file line number Diff line number Diff line change
1
+ import Link from 'next/link' ;
2
+ import Image from '../image' ;
3
+ import { sanitize } from '../../utils/miscellaneous' ;
4
+ import AddToCart from '../cart/add-to-cart' ;
5
+ import { isEmpty } from 'lodash' ;
6
+
7
+ const Product = ( { product } ) => {
8
+
9
+ if ( isEmpty ( product ) ) {
10
+ return null ;
11
+ }
12
+
13
+ const img = product ?. images ?. [ 0 ] ?? { } ;
14
+ const productType = product ?. type ?? '' ;
15
+
16
+ return (
17
+ < div className = "my-2 px-2 w-full overflow-hidden sm:w-1/2 md:w-1/3 xl:w-1/4" >
18
+ < Link href = { product ?. permalink ?? '/' } >
19
+ < a >
20
+ < Image
21
+ sourceUrl = { img ?. src ?? '' }
22
+ altText = { img ?. alt ?? '' }
23
+ title = { product ?. name ?? '' }
24
+ width = "380"
25
+ height = "380"
26
+ />
27
+ < h3 className = "font-bold uppercase my-2" > { product ?. name ?? '' } </ h3 >
28
+ < div className = "mb-4" dangerouslySetInnerHTML = { { __html : sanitize ( product ?. price_html ?? '' ) } } />
29
+ </ a >
30
+ </ Link >
31
+
32
+ { 'simple' === productType ? < AddToCart product = { product } /> : null }
33
+ </ div >
34
+ )
35
+ }
36
+
37
+ export default Product ;
Original file line number Diff line number Diff line change 1
1
export const HEADER_FOOTER_ENDPOINT = `${ process . env . NEXT_PUBLIC_WORDPRESS_SITE_URL } /wp-json/rae/v1/header-footer?header_location_id=hcms-menu-header&footer_location_id=hcms-menu-footer` ;
2
2
export const GET_PRODUCTS_ENDPOINT = `${ process . env . NEXT_PUBLIC_SITE_URL } /api/get-products` ;
3
+
4
+ /**
5
+ * Cart
6
+ * @type {string }
7
+ */
8
+ export const ADD_TO_CART_ENDPOINT = `${ process . env . NEXT_PUBLIC_WORDPRESS_SITE_URL } /wp-json/rae/v1/cart/items` ;
You can’t perform that action at this time.
0 commit comments