Skip to content
This repository was archived by the owner on Feb 23, 2024. It is now read-only.

Commit 9a9fd51

Browse files
authored
Support for multiple fee rows in the cart (#3678)
* Inject the routes controller * Cart totals need to be calculated on cart routes or fees will be missing * Add fees to schema and response * Add fees to useStoreCart * Fix styling of multiple fee rows * Fix test shape
1 parent d35d2c8 commit 9a9fd51

File tree

17 files changed

+199
-60
lines changed

17 files changed

+199
-60
lines changed

assets/js/base/components/cart-checkout/totals/fees/index.js

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,36 +6,41 @@ import { DISPLAY_CART_PRICES_INCLUDING_TAX } from '@woocommerce/block-settings';
66
import PropTypes from 'prop-types';
77
import { TotalsItem } from '@woocommerce/blocks-checkout';
88

9-
const TotalsFees = ( { currency, values } ) => {
10-
const { total_fees: totalFees, total_fees_tax: totalFeesTax } = values;
11-
const feesValue = parseInt( totalFees, 10 );
9+
const TotalsFees = ( { currency, cartFees } ) => {
10+
return (
11+
<>
12+
{ cartFees.map( ( { id, name, totals } ) => {
13+
const feesValue = parseInt( totals.total, 10 );
1214

13-
if ( ! feesValue ) {
14-
return null;
15-
}
15+
if ( ! feesValue ) {
16+
return null;
17+
}
1618

17-
const feesTaxValue = parseInt( totalFeesTax, 10 );
19+
const feesTaxValue = parseInt( totals.total_tax, 10 );
1820

19-
return (
20-
<TotalsItem
21-
className="wc-block-components-totals-fees"
22-
currency={ currency }
23-
label={ __( 'Fees', 'woo-gutenberg-products-block' ) }
24-
value={
25-
DISPLAY_CART_PRICES_INCLUDING_TAX
26-
? feesValue + feesTaxValue
27-
: feesValue
28-
}
29-
/>
21+
return (
22+
<TotalsItem
23+
key={ id }
24+
className="wc-block-components-totals-fees"
25+
currency={ currency }
26+
label={
27+
name || __( 'Fee', 'woo-gutenberg-products-block' )
28+
}
29+
value={
30+
DISPLAY_CART_PRICES_INCLUDING_TAX
31+
? feesValue + feesTaxValue
32+
: feesValue
33+
}
34+
/>
35+
);
36+
} ) }
37+
</>
3038
);
3139
};
3240

3341
TotalsFees.propTypes = {
3442
currency: PropTypes.object.isRequired,
35-
values: PropTypes.shape( {
36-
total_fees: PropTypes.string,
37-
total_fees_tax: PropTypes.string,
38-
} ).isRequired,
43+
cartFees: PropTypes.array.isRequired,
3944
};
4045

4146
export default TotalsFees;

assets/js/base/components/cart-checkout/totals/fees/stories/index.js

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,22 @@ export default {
1717

1818
export const Default = () => {
1919
const currency = currencyKnob();
20-
const totalFees = text( 'Total fees', '1000' );
21-
const totalFeesTax = text( 'Total fees tax', '200' );
20+
const totalFees = text( 'Total fee', '1000' );
21+
const totalFeesTax = text( 'Total fee tax', '200' );
2222

2323
return (
2424
<TotalsFees
2525
currency={ currency }
26-
values={ {
27-
total_fees: totalFees,
28-
total_fees_tax: totalFeesTax,
29-
} }
26+
cartFees={ [
27+
{
28+
id: 'fee',
29+
name: 'Fee',
30+
totals: {
31+
total: totalFees,
32+
total_tax: totalFeesTax,
33+
},
34+
},
35+
] }
3036
/>
3137
);
3238
};

assets/js/base/hooks/cart/test/use-store-cart.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ describe( 'useStoreCart', () => {
3737
cartIsLoading: false,
3838
cartItemErrors: [],
3939
cartErrors: [],
40+
cartFees: [],
4041
billingAddress: {
4142
first_name: '',
4243
last_name: '',
@@ -98,6 +99,7 @@ describe( 'useStoreCart', () => {
9899
cartTotals: mockCartTotals,
99100
cartIsLoading: mockCartIsLoading,
100101
cartErrors: mockCartErrors,
102+
cartFees: [],
101103
billingAddress: {},
102104
shippingAddress: mockShippingAddress,
103105
shippingRates: [],

assets/js/base/hooks/cart/use-store-cart.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ const decodeAddress = ( address ) =>
3737
export const defaultCartData = {
3838
cartCoupons: [],
3939
cartItems: [],
40+
cartFees: [],
4041
cartItemsCount: 0,
4142
cartItemsWeight: 0,
4243
cartNeedsPayment: true,
@@ -80,6 +81,7 @@ export const useStoreCart = ( options = { shouldSelect: true } ) => {
8081
return {
8182
cartCoupons: previewCart.coupons,
8283
cartItems: previewCart.items,
84+
cartFees: previewCart.fees,
8385
cartItemsCount: previewCart.items_count,
8486
cartItemsWeight: previewCart.items_weight,
8587
cartNeedsPayment: previewCart.needs_payment,
@@ -117,6 +119,7 @@ export const useStoreCart = ( options = { shouldSelect: true } ) => {
117119
return {
118120
cartCoupons: cartData.coupons,
119121
cartItems: cartData.items || [],
122+
cartFees: cartData.fees || [],
120123
cartItemsCount: cartData.itemsCount,
121124
cartItemsWeight: cartData.itemsWeight,
122125
cartNeedsPayment: cartData.needsPayment,

assets/js/blocks/cart-checkout/cart/full-cart/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ const Cart = ( { attributes } ) => {
6565

6666
const {
6767
cartItems,
68+
cartFees,
6869
cartTotals,
6970
cartIsLoading,
7071
cartItemsCount,
@@ -114,7 +115,7 @@ const Cart = ( { attributes } ) => {
114115
{ __( 'Cart totals', 'woo-gutenberg-products-block' ) }
115116
</Title>
116117
<Subtotal currency={ totalsCurrency } values={ cartTotals } />
117-
<TotalsFees currency={ totalsCurrency } values={ cartTotals } />
118+
<TotalsFees currency={ totalsCurrency } cartFees={ cartFees } />
118119
<TotalsDiscount
119120
cartCoupons={ appliedCoupons }
120121
currency={ totalsCurrency }

assets/js/blocks/cart-checkout/checkout/block.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ const Checkout = ( { attributes, scrollToTop } ) => {
6565
cartItems,
6666
cartTotals,
6767
cartCoupons,
68+
cartFees,
6869
cartNeedsPayment,
6970
} = useStoreCart();
7071
const {
@@ -157,6 +158,7 @@ const Checkout = ( { attributes, scrollToTop } ) => {
157158
cartCoupons={ cartCoupons }
158159
cartItems={ cartItems }
159160
cartTotals={ cartTotals }
161+
cartFees={ cartFees }
160162
/>
161163
</Sidebar>
162164
</SidebarLayout>

assets/js/blocks/cart-checkout/checkout/sidebar/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import { useStoreCartCoupons } from '@woocommerce/base-hooks';
2323
const CheckoutSidebar = ( {
2424
cartCoupons = [],
2525
cartItems = [],
26+
cartFees = [],
2627
cartTotals = {},
2728
} ) => {
2829
const {
@@ -39,7 +40,7 @@ const CheckoutSidebar = ( {
3940
<>
4041
<OrderSummary cartItems={ cartItems } />
4142
<Subtotal currency={ totalsCurrency } values={ cartTotals } />
42-
<TotalsFees currency={ totalsCurrency } values={ cartTotals } />
43+
<TotalsFees currency={ totalsCurrency } cartFees={ cartFees } />
4344
<TotalsDiscount
4445
cartCoupons={ cartCoupons }
4546
currency={ totalsCurrency }

assets/js/previews/cart.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ export const previewCart = {
156156
},
157157
},
158158
],
159+
fees: [],
159160
items_count: 3,
160161
items_weight: 0,
161162
needs_payment: true,

assets/js/type-defs/hooks.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
* to the cart.
1313
* @property {Array} cartItems An array of items in the
1414
* cart.
15+
* @property {Array} cartFees An array of fees in the
16+
* cart.
1517
* @property {number} cartItemsCount The number of items in the
1618
* cart.
1719
* @property {number} cartItemsWeight The weight of all items in

packages/checkout/totals/item/style.scss

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
flex-wrap: wrap;
44
padding: em($gap-small) 0;
55
width: 100%;
6+
7+
&:not(:first-of-type):not(:last-of-type) {
8+
@include with-translucent-border(1px 0 0);
9+
}
610
}
711

812
.wc-block-components-totals-item__label {

0 commit comments

Comments
 (0)