Skip to content

Commit 9d576b5

Browse files
[6.x] Fix error with {{ sc:cart:raw_tax_total_split }} tag (#1037)
* Add failing test * Refactor `raw_tax_total_split` tag to avoid getting data post-augmentation
1 parent a988310 commit 9d576b5

File tree

2 files changed

+15
-9
lines changed

2 files changed

+15
-9
lines changed

src/Tags/CartTags.php

+9-9
Original file line numberDiff line numberDiff line change
@@ -160,16 +160,16 @@ public function taxTotalSplit(): Collection
160160

161161
public function rawTaxTotalSplit(): Collection
162162
{
163-
return collect($this->items())
164-
->groupBy(function ($item) {
165-
return $item['tax']['rate'];
166-
})
167-
->map(function ($group, $groupRate) {
163+
if (! $this->hasCart()) {
164+
return collect();
165+
}
166+
167+
return $this->getCart()->lineItems()
168+
->groupBy(fn ($lineItem) => $lineItem->tax()['rate'])
169+
->map(function ($group, $rate) {
168170
return [
169-
'rate' => $groupRate,
170-
'amount' => $group->sum(function ($item) {
171-
return $item['tax']['amount'];
172-
}),
171+
'rate' => $rate,
172+
'amount' => $group->sum(fn ($lineItem) => $lineItem->tax()['amount']),
173173
];
174174
})
175175
->values();

tests/Tags/CartTagTest.php

+6
Original file line numberDiff line numberDiff line change
@@ -429,6 +429,12 @@
429429
$taxReduced = $cartProduct3->tax()['amount'];
430430
$taxReducedFormatted = Currency::parse($taxReduced, Site::default());
431431

432+
// Expected tag output format = '19:1234|7:5678'
433+
$renderedTag = (string) tag('{{ sc:cart:rawTaxTotalSplit }}{{ rate }}:{{ amount }}|{{ /sc:cart:rawTaxTotalSplit }}');
434+
435+
expect($renderedTag)->toContain($taxRateDefault->rate().':'.$taxDefault);
436+
expect($renderedTag)->toContain($taxRateReduced->rate().':'.$taxReduced);
437+
432438
// Expected tag output format = '7:£12.34|19:£56.78'
433439
$renderedTag = (string) tag('{{ sc:cart:taxTotalSplit }}{{ rate }}:{{ amount }}|{{ /sc:cart:taxTotalSplit }}');
434440

0 commit comments

Comments
 (0)