Skip to content

Commit da3543f

Browse files
committed
fix: coupon code invalid error message
- rework coupon code component to handle GraphQL errors M2-559
1 parent ae5f1de commit da3543f

File tree

7 files changed

+47
-33
lines changed

7 files changed

+47
-33
lines changed

packages/api-client/src/types/API.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ export interface MagentoApiMethods {
203203
applyCouponToCart(
204204
input: ApplyCouponToCartInput,
205205
customQuery?: CustomQuery
206-
): Promise<FetchResult<ApplyCouponToCartMutation>>;
206+
): Promise<ApolloQueryResult<ApplyCouponToCartMutation>>;
207207

208208
availableStores(customQuery?: CustomQuery): Promise<ApolloQueryResult<AvailableStoresQuery>>;
209209

@@ -355,7 +355,7 @@ export interface MagentoApiMethods {
355355
removeCouponFromCart(
356356
input: RemoveCouponFromCartInput,
357357
customQuery?: CustomQuery
358-
): Promise<FetchResult<RemoveCouponFromCartMutation>>;
358+
): Promise<ApolloQueryResult<RemoveCouponFromCartMutation>>;
359359

360360
removeItemFromCart(
361361
input: RemoveItemFromCartInput,

packages/theme/components/CouponCode.vue

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
:disabled="isCouponCodeApplied"
1515
:label="$t('Enter promo code')"
1616
class="sf-input--filled coupon-code__input"
17-
:error-message="$t('An error occurred')"
17+
:error-message="$t(applyCouponMsg.message)"
1818
:valid="!hasAnyError"
1919
/>
2020
<SfButton
@@ -52,9 +52,8 @@ export default defineComponent({
5252
5353
const couponCodeAppliedToCart = computed(() => cartGetters.getAppliedCoupon(cart.value)?.code);
5454
const isCouponCodeApplied = computed(() => couponCodeAppliedToCart.value !== undefined);
55-
56-
const hasAnyError = computed(() => Object.values(error.value).some((value) => value instanceof Error));
57-
55+
const hasAnyError = computed(() => Object.values(error.value).some((value) => value !== null));
56+
const applyCouponMsg = computed<Error>(() => error.value.applyCoupon || error.value.removeCoupon || { message: '', name: 'apply-coupon' });
5857
const applyOrRemoveCoupon = async () => {
5958
const operationPromise = isCouponCodeApplied.value
6059
? removeCoupon({})
@@ -67,8 +66,9 @@ export default defineComponent({
6766
couponCodeUserInput,
6867
couponCodeAppliedToCart,
6968
isCouponCodeApplied,
70-
hasAnyError,
69+
applyCouponMsg,
7170
applyOrRemoveCoupon,
71+
hasAnyError,
7272
};
7373
},
7474
});

packages/theme/lang/de.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ export default {
1010
"All Orders": "Alle Bestellungen",
1111
"Allow order notifications": "Bestellbenachrichtigungen zulassen",
1212
"Apply": "Übernehmen",
13+
"Applied Coupon": "Angewendeter Gutschein",
1314
"Attention!": "Attention!",
1415
"Back to home": "Zurück Zur Startseite",
1516
"Back to homepage": "Zurück zur Homepage",
@@ -131,6 +132,7 @@ export default {
131132
"Read reviews": "Bewertungen lesen",
132133
"Register": "Registrieren",
133134
"Register today": "Melde dich noch heute an",
135+
"Remove": "Löschen",
134136
"Remove Address": "Adresse entfernen",
135137
"Remove from Wishlist": "Von Wunschliste entfernen",
136138
"Repeat Password": "Wiederhole das Passwort",
@@ -266,5 +268,6 @@ export default {
266268
"Show less": "Zeige weniger",
267269
"Yes": "Ja",
268270
"No": "Nein",
269-
"Apply filters": "Filter anwenden"
271+
"Apply filters": "Filter anwenden",
272+
"The coupon code isn't valid. Verify the code and try again.": "Der Gutscheincode ist ungültig. Überprüfen Sie den Code und versuchen Sie es erneut."
270273
};

packages/theme/lang/en.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ export default {
1010
"All Orders": "All Orders",
1111
"Allow order notifications": "Allow order notifications",
1212
"Apply": "Apply",
13+
"Applied Coupon": "Applied Coupon",
1314
"Attention!": "Attention!",
1415
"Back to home": "Back to home",
1516
"Back to homepage": "Back to homepage",
@@ -127,6 +128,7 @@ export default {
127128
"Read reviews": "Read reviews",
128129
"Register": "Register",
129130
"Register today": "Register today",
131+
"Remove": "Remove",
130132
"Remove Address": "Remove Address",
131133
"Remove from Wishlist": "Remove from Wishlist",
132134
"Repeat Password": "Repeat Password",
@@ -264,5 +266,6 @@ export default {
264266
"Show less": "Show less",
265267
"Yes": "Yes",
266268
"No": "No",
267-
"Apply filters": "Apply filters"
269+
"Apply filters": "Apply filters",
270+
"The coupon code isn't valid. Verify the code and try again.": "The coupon code isn't valid. Verify the code and try again.",
268271
};

packages/theme/modules/checkout/composables/useCart/commands/applyCouponCommand.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,17 @@ export const applyCouponCommand = {
1313
currentCart,
1414
});
1515

16-
const { data } = await context.$magento.api.applyCouponToCart({
16+
const { data, errors } = await context.$magento.api.applyCouponToCart({
1717
cart_id: currentCart.id,
1818
coupon_code: couponCode,
1919
}, customQuery);
2020

21-
Logger.debug('[Result]:', { data });
21+
Logger.debug('[Result]:', { data, errors });
2222

2323
return {
24-
updatedCart: data.applyCouponToCart.cart as unknown as Cart,
24+
updatedCart: data.applyCouponToCart?.cart as unknown as Cart,
2525
updatedCoupon: { code: couponCode },
26+
errors,
2627
};
2728
},
2829
};

packages/theme/modules/checkout/composables/useCart/commands/removeCouponCommand.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,16 @@ export const removeCouponCommand = {
66
execute: async (context: VsfContext, { currentCart, customQuery = { removeCouponFromCart: 'removeCouponFromCart' } }) => {
77
Logger.debug('[Magento]: Remove coupon from cart', { currentCart });
88

9-
const { data } = await context.$magento.api.removeCouponFromCart({
9+
const { data, errors } = await context.$magento.api.removeCouponFromCart({
1010
cart_id: currentCart.id,
1111
}, customQuery);
1212

1313
Logger.debug('[Result]:', { data });
1414

1515
return {
16-
updatedCart: data.removeCouponFromCart.cart as unknown as Cart,
16+
updatedCart: data.removeCouponFromCart?.cart as unknown as Cart,
1717
updatedCoupon: { code: '' },
18+
errors,
1819
};
1920
},
2021
};

packages/theme/modules/checkout/composables/useCart/index.ts

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -197,21 +197,35 @@ PRODUCT
197197
}
198198
};
199199

200+
const handleCoupon = async (couponCode = null, customQuery = null): Promise<void> => {
201+
const variables = {
202+
currentCart: cart.value,
203+
customQuery,
204+
couponCode,
205+
};
206+
207+
const { updatedCart, errors } = couponCode
208+
? await applyCouponCommand.execute(context, variables)
209+
: await removeCouponCommand.execute(context, variables);
210+
211+
if (errors) {
212+
throw errors[0];
213+
}
214+
215+
if (updatedCart) {
216+
customerStore.$patch((state) => {
217+
state.cart = updatedCart;
218+
});
219+
}
220+
};
221+
200222
const applyCoupon = async ({ couponCode, customQuery }): Promise<void> => {
201223
Logger.debug('useCart.applyCoupon');
202224

203225
try {
204226
loading.value = true;
205-
const { updatedCart } = await applyCouponCommand.execute(context, {
206-
currentCart: cart.value,
207-
couponCode,
208-
customQuery,
209-
});
210-
227+
await handleCoupon(couponCode, customQuery);
211228
error.value.applyCoupon = null;
212-
customerStore.$patch((state) => {
213-
state.cart = updatedCart;
214-
});
215229
} catch (err) {
216230
error.value.applyCoupon = err;
217231
Logger.error('useCart/applyCoupon', err);
@@ -225,16 +239,8 @@ PRODUCT
225239

226240
try {
227241
loading.value = true;
228-
const { updatedCart } = await removeCouponCommand.execute(context, {
229-
currentCart: cart.value,
230-
customQuery,
231-
});
232-
233-
error.value.removeCoupon = null;
234-
customerStore.$patch((state) => {
235-
state.cart = updatedCart;
236-
});
237-
loading.value = false;
242+
await handleCoupon(null, customQuery);
243+
error.value.applyCoupon = null;
238244
} catch (err) {
239245
error.value.removeCoupon = err;
240246
Logger.error('useCart/removeCoupon', err);

0 commit comments

Comments
 (0)