Skip to content

Commit 84b5342

Browse files
Merge pull request #249 from hyva-themes/63/discount-codes-form
Apply discount code form
2 parents 5003338 + 708a578 commit 84b5342

32 files changed

+371
-10
lines changed

src/reactapp/package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/reactapp/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "hyva-react-checkout",
3-
"version": "1.0.2",
3+
"version": "1.0.3",
44
"description": "Highly Customizable Checkout for Magento 2, Built with React.",
55
"repository": "local",
66
"keywords": [
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import sendRequest from '../../../sendRequest';
2+
import modifier from '../../fetchGuestCart/modifier';
3+
import { APPLY_COUPON_CODE_MUTATION } from './mutation';
4+
import LocalStorage from '../../../../utils/localStorage';
5+
6+
export default async function applyCouponCode(dispatch, couponCode) {
7+
const variables = { couponCode, cartId: LocalStorage.getCartId() };
8+
9+
return modifier(
10+
await sendRequest(dispatch, {
11+
query: APPLY_COUPON_CODE_MUTATION,
12+
variables,
13+
}),
14+
'applyCouponToCart.cart'
15+
);
16+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { CART_DATA_FRAGMENT } from '../../utility/query/cartQueryInfo';
2+
3+
export const APPLY_COUPON_CODE_MUTATION = `
4+
mutation applyCouponToCartMutation(
5+
$cartId: String!
6+
$couponCode: String!
7+
) {
8+
applyCouponToCart(
9+
input: {
10+
cart_id: $cartId
11+
coupon_code: $couponCode
12+
}
13+
) {
14+
cart {
15+
${CART_DATA_FRAGMENT}
16+
}
17+
}
18+
}
19+
`;
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import applyCouponCodeToCart from './applyCouponCode/applyCouponCode';
2+
import removeCouponCodeFromCart from './removeCouponCode/removeCouponCode';
3+
4+
export { applyCouponCodeToCart, removeCouponCodeFromCart };
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { CART_DATA_FRAGMENT } from '../../utility/query/cartQueryInfo';
2+
3+
export const REMOVE_COUPON_CODE_MUTATION = `
4+
mutation removeCouponFromCartMutation(
5+
$cartId: String!
6+
) {
7+
removeCouponFromCart(
8+
input: {
9+
cart_id: $cartId
10+
}
11+
) {
12+
cart {
13+
${CART_DATA_FRAGMENT}
14+
}
15+
}
16+
}
17+
`;
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import sendRequest from '../../../sendRequest';
2+
import modifier from '../../fetchGuestCart/modifier';
3+
import { REMOVE_COUPON_CODE_MUTATION } from './mutation';
4+
import LocalStorage from '../../../../utils/localStorage';
5+
6+
export default async function removeCouponCode(dispatch, couponCode) {
7+
const variables = { couponCode, cartId: LocalStorage.getCartId() };
8+
9+
return modifier(
10+
await sendRequest(dispatch, {
11+
query: REMOVE_COUPON_CODE_MUTATION,
12+
variables,
13+
}),
14+
'removeCouponFromCart.cart'
15+
);
16+
}

src/reactapp/src/api/cart/fetchGuestCart/modifier.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,11 +77,13 @@ export default function fetchGuestCartModifier(result, dataMethod) {
7777
const cartPrices = _get(cartData, 'prices', {});
7878
const paymentMethods = _get(cartData, 'available_payment_methods', []);
7979
const selectedPaymentMethod = _get(cartData, 'selected_payment_method', {});
80+
const appliedCoupon = _get(cartData, 'applied_coupons[0].code') || '';
8081

8182
return {
8283
id: cartData.id,
8384
email: cartData.email,
8485
isVirtualCart: cartData.is_virtual,
86+
appliedCoupon,
8587
items: modifyCartItemsData(cartItems),
8688
billing_address: modifyBillingAddressData(billingAddress),
8789
shipping_address: modifyShippingAddressList(shippingAddresses),

src/reactapp/src/api/cart/fetchGuestCart/query.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ export const CART_QUERY_PART = `
99
id
1010
email
1111
is_virtual
12+
applied_coupons {
13+
code
14+
}
1215
${cartItemsInfo}
1316
${cartPriceInfo}
1417
${cartBillingAddrInfo}

src/reactapp/src/api/cart/utility/query/cartQueryInfo.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ export const CART_DATA_FRAGMENT = `
88
id
99
email
1010
is_virtual
11+
applied_coupons {
12+
code
13+
}
1114
${cartItemsInfo}
1215
${cartBillingAddrInfo}
1316
${cartShippingAddrInfo}

0 commit comments

Comments
 (0)