😍Step by Step Study (part 1)👆

Query

Step 1: Create layout, loading, FETCH_ALL_PROUDUCTS_QUERY

src\pages\index.tsx

src\utils\gql\GQL_QUERIES.ts

Step 2: Create DisplayProducts.component.tsx

src\pages\index.tsx

src\components\Product\DisplayProducts.component.tsx

Step 3: Create Single Product

src\pages\product[slug].tsx

src\components\Product\SingleProduct.component.tsx

src\utils\gql\GQL_QUERIES.ts

file-archive
54KB

Mutation

Step 4.1: Add to Carr

Hoặc chỉ cần id là đủ mẫu như sau

Một trường hợp

— Cách mua biến thể bằng variationId ok

Kết quả:

Mua kèm theo số lượng

— Cách mua biến thể bằng variation not ok không hiểu tại sao?

Step 4.2 Get Cart

Step 4.3 giải thích cách tạo cart không cần đăng nhập

— Nếu mà chưa đăng nhập sử dụng addToCart thì khi sử dụng getCart sẽ không có 😒

— Do đó để tạo cart thì chúng ta sẽ tạo một cart localStorage

---- tạo một handleAddToCart

---- sử dụng hàm refetch() để load lại useQuery

Chú ý: Mặc định hàm useQuery nó sẽ hoạt động mỗi lần render

— prop buttonDisabled này rất quan trọng nó giúp hoạt động thêm được vào giỏ hàng khi đã được sẵn sàng không sảy ra lỗi

Toàn bộ code src\components\Product\AddToCart.component.tsx

--— Và một điều nữa khi client hoạt động nó sẽ tạo ra một key : woocommerce-session để làm việc với session

Toàn bộ code src\utils\apollo\ApolloClient.js

--— Class này giúp ta sử dụng cart trong cache : new InMemoryCache()

file-archive
59KB

Step 5.1 Update Quantyti Cart

— Update Quantyti Cart trường hợp đăng nhập

src\pages\shopping-cart.tsx

src\components\Cart\CartContents.component.tsx

src\utils\functions\functions.ts

src\utils\gql\GQL_MUTATIONS.ts

file-archive
62KB

Step 5.2 Subtotal && Link Checkout

src\components\Cart\CartContents.component.tsx

Step 6. Cách khai báo các kiểu chung global cho các dự án next TypeScrip

Đọc bài viết này https://learnreact.gitbook.io/learnreact/advanced-reactjs/cach-khai-bao-cac-kieu-chung-global-cho-cac-du-an-react-typescript-okarrow-up-right

globals.d.ts

Một mẫu query checkout

mutation-order.js

Còn đây là kết quả thanh toán ở website

Step 7 Checkout

src\utils\functions\functions.ts

datatest-graphql.txt

src\utils\gql\GQL_MUTATIONS.ts

src\utils\constants\INPUT_FIELDS.ts

file-archive
80KB

— Để giỏ hàng dược thanh toán vào Order cần thông tin sau (không cần xác thực gì)

file-archive
80KB

— setCart cả ở Layout

src\components\Layout\Layout.component.tsx

Step 8: Shop

— Step 8.1 giao diện

src\pages\shop.tsx

src\components\Product\ProductList.component.tsx

— Bổ sung thêm productCategories để shop sử dụng

— Create filteredProducts and ProductCard

src\components\Product\ProductList.component.tsx

src\components\Product\ProductCard.component.tsx

src\hooks\useProductFilters.ts

file-archive
83KB

Last updated

Was this helpful?