@@ -93,21 +93,8 @@ export default class Cart extends Component {
93
93
const data = Object . assign ( { } , lineItem , this . options . viewData ) ;
94
94
const fullPrice = data . variant . priceV2 . amount * data . quantity ;
95
95
const formattedPrice = formatMoney ( fullPrice , this . moneyFormat ) ;
96
- const discountAllocations = data . discountAllocations ;
97
-
98
- const { discounts, totalDiscount} = discountAllocations . reduce ( ( discountAcc , discount ) => {
99
- const targetSelection = discount . discountApplication . targetSelection ;
100
- if ( LINE_ITEM_TARGET_SELECTIONS . indexOf ( targetSelection ) > - 1 ) {
101
- const discountAmount = discount . allocatedAmount . amount ;
102
- const discountDisplayText = discount . discountApplication . title || discount . discountApplication . code ;
103
- discountAcc . totalDiscount += discountAmount ;
104
- discountAcc . discounts . push ( { discount : `${ discountDisplayText } (-${ formatMoney ( discountAmount , this . moneyFormat ) } )` } ) ;
105
- }
106
- return discountAcc ;
107
- } , {
108
- discounts : [ ] ,
109
- totalDiscount : 0 ,
110
- } ) ;
96
+
97
+ const { discounts, totalDiscount} = this . discountsForLineItem ( lineItem ) ;
111
98
data . discounts = discounts . length > 0 ? discounts : null ;
112
99
data . formattedFullPrice = totalDiscount > 0 ? formattedPrice : null ;
113
100
data . formattedActualPrice = formatMoney ( fullPrice - totalDiscount , this . moneyFormat ) ;
@@ -408,8 +395,15 @@ export default class Cart extends Component {
408
395
this . toggles . forEach ( ( toggle ) => toggle . view . render ( ) ) ;
409
396
if ( quantity > 0 ) {
410
397
this . view . render ( ) ;
398
+ const updatedLineItem = this . model . lineItems . find ( ( item ) => item . id === id ) ;
399
+ this . updateSummaryText ( `${ this . options . text . itemTotalAccessibilityLabel } ${ this . formattedLineItemTotal ( updatedLineItem ) } ` ) ;
411
400
} else {
412
401
this . view . animateRemoveNode ( id ) ;
402
+ if ( this . model . lineItems . length > 0 ) {
403
+ this . updateSummaryText ( this . options . text . itemRemovedAccessibilityLabel ) ;
404
+ } else {
405
+ this . updateSummaryText ( `${ this . options . text . itemRemovedAccessibilityLabel } ${ this . options . text . empty } ` , true ) ;
406
+ }
413
407
}
414
408
return checkout ;
415
409
} ) ;
@@ -437,6 +431,7 @@ export default class Cart extends Component {
437
431
if ( ! openCart ) {
438
432
this . setFocus ( ) ;
439
433
}
434
+ this . updateSummaryText ( this . options . text . itemAddedAccessibilityLabel ) ;
440
435
return checkout ;
441
436
} ) ;
442
437
} else {
@@ -454,6 +449,7 @@ export default class Cart extends Component {
454
449
if ( ! openCart ) {
455
450
this . setFocus ( ) ;
456
451
}
452
+ this . updateSummaryText ( this . options . text . itemAddedAccessibilityLabel ) ;
457
453
return checkout ;
458
454
} ) ;
459
455
}
@@ -495,4 +491,43 @@ export default class Cart extends Component {
495
491
this . view . setFocus ( ) ;
496
492
} , 0 ) ;
497
493
}
494
+
495
+ updateSummaryText ( lineItemText , hideSubtotal ) {
496
+ const summaryText = hideSubtotal ? lineItemText : `${ lineItemText } ${ this . options . text . subtotalAccessibilityLabel } ${ this . formattedTotal } ` ;
497
+
498
+ const summaryNode = this . view . document . querySelector ( this . selectors . cart . hiddenSummary ) ;
499
+ summaryNode . textContent = summaryText ;
500
+
501
+ setTimeout ( ( ) => {
502
+ summaryNode . textContent = '' ;
503
+ } , 1000 ) ;
504
+ }
505
+
506
+ formattedLineItemTotal ( lineItem ) {
507
+ const fullPrice = lineItem . variant . priceV2 . amount * lineItem . quantity ;
508
+ if ( ! this . options . contents . discounts ) {
509
+ return formatMoney ( fullPrice , this . moneyFormat ) ;
510
+ }
511
+
512
+ const { totalDiscount} = this . discountsForLineItem ( lineItem ) ;
513
+ return formatMoney ( fullPrice - totalDiscount , this . moneyFormat ) ;
514
+ }
515
+
516
+ discountsForLineItem ( lineItem ) {
517
+ const discountAllocations = lineItem . discountAllocations ;
518
+
519
+ return discountAllocations . reduce ( ( discountAcc , discount ) => {
520
+ const targetSelection = discount . discountApplication . targetSelection ;
521
+ if ( LINE_ITEM_TARGET_SELECTIONS . indexOf ( targetSelection ) > - 1 ) {
522
+ const discountAmount = discount . allocatedAmount . amount ;
523
+ const discountDisplayText = discount . discountApplication . title || discount . discountApplication . code ;
524
+ discountAcc . totalDiscount += discountAmount ;
525
+ discountAcc . discounts . push ( { discount : `${ discountDisplayText } (-${ formatMoney ( discountAmount , this . moneyFormat ) } )` } ) ;
526
+ }
527
+ return discountAcc ;
528
+ } , {
529
+ discounts : [ ] ,
530
+ totalDiscount : 0 ,
531
+ } ) ;
532
+ }
498
533
}
0 commit comments