From 443200141cd67815b43fb4762e087d9fc519f3b6 Mon Sep 17 00:00:00 2001 From: RodrigoTadeuF Date: Fri, 27 Sep 2024 15:19:00 -0300 Subject: [PATCH 1/4] Adding the country and postalCode to orderForm --- react/client.ts | 13 +++++++++++++ react/hooks/useShippingOptions.ts | 15 +++++++++++++-- react/utils/cookie.ts | 10 ++++++++++ 3 files changed, 36 insertions(+), 2 deletions(-) diff --git a/react/client.ts b/react/client.ts index 1a18fab..05596fe 100644 --- a/react/client.ts +++ b/react/client.ts @@ -30,3 +30,16 @@ export const getPickups = ( fetch( `/api/checkout/pub/pickup-points?an=${account}&countryCode=${countryCode}&postalCode=${zipCode}` ).then((res) => res.json()) + +export const updateOrderForm = ( + country: string, + zipCode: string, + orderFormId: string +) => + fetch(`/api/checkout/pub/orderForm/${orderFormId}/attachments/shippingData`, { + method: 'POST', + body: `{"selectedAddresses": [{ "postalCode": ${zipCode}, "country": ${country} ]}`, + headers: { + 'Content-Type': 'application/json', + }, + }).then((res) => res.json()) diff --git a/react/hooks/useShippingOptions.ts b/react/hooks/useShippingOptions.ts index 1af2797..a52a4e6 100644 --- a/react/hooks/useShippingOptions.ts +++ b/react/hooks/useShippingOptions.ts @@ -4,9 +4,14 @@ import { useRuntime, useSSR } from 'vtex.render-runtime' import { useIntl } from 'react-intl' import { usePixel } from 'vtex.pixel-manager' -import { getCountryCode, getZipCode } from '../utils/cookie' +import { getCountryCode, getOrderFormId, getZipCode } from '../utils/cookie' import messages from '../messages' -import { getAddress, getPickups, updateSession } from '../client' +import { + getAddress, + getPickups, + updateOrderForm, + updateSession, +} from '../client' declare let window: any @@ -104,6 +109,12 @@ const useShippingOptions = () => { setIsLoading(true) + const orderFormId = getOrderFormId() + + if (orderFormId) { + await updateOrderForm(inputZipCode, orderFormId, countryCode) + } + const { geoCoordinates: coordinates } = await getAddress( countryCode, inputZipCode, diff --git a/react/utils/cookie.ts b/react/utils/cookie.ts index 9640cb3..be1b139 100644 --- a/react/utils/cookie.ts +++ b/react/utils/cookie.ts @@ -50,3 +50,13 @@ export function getCountryCode() { return countryCode } + +export function getOrderFormId() { + const orderForm = localStorage.getItem('orderform') + + if (!orderForm) { + return + } + + return JSON.parse(orderForm || '{}').id +} From f11520c1dff9a1ad34fed598ef421dcfaa8bfbe6 Mon Sep 17 00:00:00 2001 From: RodrigoTadeuF Date: Fri, 27 Sep 2024 15:32:47 -0300 Subject: [PATCH 2/4] Changelog --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 592f2d7..32da326 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Added +- orderForm integration from PLP to cart + + ## [0.1.2] - 2024-09-12 ## [0.1.1] - 2024-09-12 From e7ba06e77726db37bba0e4d0a3b15eda961213f8 Mon Sep 17 00:00:00 2001 From: RodrigoTadeuF Date: Mon, 30 Sep 2024 07:28:33 -0300 Subject: [PATCH 3/4] Fixing the order --- react/hooks/useShippingOptions.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/react/hooks/useShippingOptions.ts b/react/hooks/useShippingOptions.ts index a52a4e6..a39c6e1 100644 --- a/react/hooks/useShippingOptions.ts +++ b/react/hooks/useShippingOptions.ts @@ -112,7 +112,7 @@ const useShippingOptions = () => { const orderFormId = getOrderFormId() if (orderFormId) { - await updateOrderForm(inputZipCode, orderFormId, countryCode) + await updateOrderForm(countryCode, inputZipCode, orderFormId) } const { geoCoordinates: coordinates } = await getAddress( From 8afccc06c30a7bbf343a5c485fb06969562cb504 Mon Sep 17 00:00:00 2001 From: RodrigoTadeuF Date: Tue, 1 Oct 2024 09:27:11 -0300 Subject: [PATCH 4/4] Fixing the orderform call --- react/client.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/react/client.ts b/react/client.ts index 05596fe..3a903ac 100644 --- a/react/client.ts +++ b/react/client.ts @@ -38,7 +38,7 @@ export const updateOrderForm = ( ) => fetch(`/api/checkout/pub/orderForm/${orderFormId}/attachments/shippingData`, { method: 'POST', - body: `{"selectedAddresses": [{ "postalCode": ${zipCode}, "country": ${country} ]}`, + body: `{"selectedAddresses": [{ "postalCode": ${zipCode}, "country": "${country}" }]}`, headers: { 'Content-Type': 'application/json', },