Skip to content

Commit 904f98d

Browse files
author
Arjen Miedema
committed
Fetch applied taxes and subtotal incl and excl VAT
To get a total overview of the totals in the checkout, the applied taxes are now also fetched from Magento using GraphQL. Also the subtotal is now loaded excluding and including taxes, so the developer in the frontend can decide if the subtotal should be shown including or excluding VAT.
1 parent ba18393 commit 904f98d

File tree

4 files changed

+51
-10
lines changed

4 files changed

+51
-10
lines changed

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

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,21 +42,33 @@ function modifyCartItemsData(cartItems) {
4242

4343
function modifyCartPricesData(cartPrices) {
4444
const grandTotal = _get(cartPrices, 'grand_total', {});
45-
const subTotal = _get(cartPrices, 'subtotal_including_tax', {});
45+
const subTotalIncl = _get(cartPrices, 'subtotal_including_tax', {});
46+
const subTotalExcl = _get(cartPrices, 'subtotal_excluding_tax', {});
4647
const discountPrices = _get(cartPrices, 'discounts', []) || [];
4748
const discounts = discountPrices.map((discount) => ({
4849
label: discount.label,
4950
price: formatPrice(-discount.amount.value, true),
5051
amount: discount.amount.value,
5152
}));
53+
const appliedTaxAmounts = _get(cartPrices, 'applied_taxes', []) || [];
54+
const appliedTaxes = appliedTaxAmounts.map((tax) => ({
55+
label: tax.label,
56+
price: formatPrice(tax.amount.value),
57+
amount: tax.amount.value,
58+
}));
5259
const grandTotalAmount = _get(grandTotal, 'value');
53-
const subTotalAmount = _get(subTotal, 'value');
60+
const subTotalInclAmount = _get(subTotalIncl, 'value');
61+
const subTotalExclAmount = _get(subTotalExcl, 'value');
5462

5563
return {
5664
discounts,
5765
hasDiscounts: !_isArrayEmpty(discountPrices),
58-
subTotal: formatPrice(subTotalAmount),
59-
subTotalAmount,
66+
appliedTaxes,
67+
hasAppliedTaxes: !_isArrayEmpty(appliedTaxAmounts),
68+
subTotalIncl: formatPrice(subTotalInclAmount),
69+
subTotalInclAmount,
70+
subTotalExcl: formatPrice(subTotalExclAmount),
71+
subTotalExclAmount,
6072
grandTotal: formatPrice(grandTotalAmount),
6173
grandTotalAmount,
6274
};

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,21 @@ prices {
44
value
55
currency
66
}
7+
subtotal_excluding_tax {
8+
value
9+
currency
10+
}
711
subtotal_including_tax {
812
value
913
currency
1014
}
15+
applied_taxes {
16+
label
17+
amount {
18+
currency
19+
value
20+
}
21+
}
1122
discounts {
1223
label
1324
amount {

src/reactapp/src/components/totals/Totals.jsx

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,13 @@ import useTotalsCartContext from './hooks/useTotalsCartContext';
77

88
function Totals() {
99
const {
10-
subTotal,
10+
subTotalIncl,
11+
subTotalExcl,
1112
discounts,
13+
appliedTaxes,
1214
grandTotal,
1315
hasSubTotal,
16+
hasAppliedTaxes,
1417
hasDiscounts,
1518
hasShippingRate,
1619
shippingMethodRate,
@@ -25,7 +28,7 @@ function Totals() {
2528
{hasSubTotal && (
2629
<div className="flex justify-between">
2730
<div>{__('Cart Subtotal')}</div>
28-
<div>{subTotal}</div>
31+
<div>{subTotalIncl}</div>
2932
</div>
3033
)}
3134

@@ -35,6 +38,15 @@ function Totals() {
3538
<div>{shippingMethodRate}</div>
3639
</div>
3740
)}
41+
{hasAppliedTaxes &&
42+
appliedTaxes.map((appliedTax) => (
43+
<div key={appliedTax.label} className="flex justify-between">
44+
<div>
45+
{__('Tax')} {__(appliedTax.label)}
46+
</div>
47+
<div>{appliedTax.price}</div>
48+
</div>
49+
))}
3850
{hasDiscounts &&
3951
discounts.map((discount) => (
4052
<div key={discount.label} className="flex justify-between">

src/reactapp/src/components/totals/hooks/useTotalsCartContext.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,27 @@ export default function useTotalsCartContext() {
1010
const prices = _get(cart, 'prices', {}) || {};
1111
const { price: shippingMethodRate, amount: shippingAmount } = shippingMethod;
1212
const {
13-
subTotal,
13+
subTotalIncl,
14+
subTotalExcl,
1415
grandTotal,
16+
appliedTaxes,
17+
hasAppliedTaxes,
1518
discounts,
1619
hasDiscounts,
17-
subTotalAmount,
20+
subTotalInclAmount,
1821
grandTotalAmount,
1922
} = prices;
2023

2124
return {
22-
subTotal,
25+
subTotalIncl,
26+
subTotalExcl,
2327
grandTotal,
2428
discounts,
29+
appliedTaxes,
2530
shippingMethodRate,
2631
hasDiscounts,
27-
hasSubTotal: !!subTotalAmount,
32+
hasAppliedTaxes,
33+
hasSubTotal: !!subTotalInclAmount,
2834
hasGrandTotal: !!grandTotalAmount,
2935
hasShippingRate: !!shippingAmount,
3036
};

0 commit comments

Comments
 (0)