Skip to content

Commit dd65d19

Browse files
committed
Make sure cart item price includes discount; Add discount, price_excl_tax and price_incl_tax
1 parent c116778 commit dd65d19

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

DataLayer/Mapper/CartItemDataMapper.php

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,16 @@ public function mapByCartItem(CartItem $cartItem): array
5858
$cartItemData = [];
5959
}
6060

61+
$price = $this->getPrice($cartItem);
6162
return array_merge($cartItemData, [
6263
'item_sku' => $cartItem->getSku(),
6364
'item_name' => $cartItem->getName(),
6465
'order_item_id' => $cartItem->getItemId(),
6566
'quantity' => (float) $cartItem->getQty(),
66-
'price' => $this->getPrice($cartItem)
67+
'price_excl_tax' => $cartItem->getRowTotal() / $cartItem->getQty(),
68+
'price_incl_tax' => $cartItem->getRowTotalInclTax() / $cartItem->getQty(),
69+
'price' => $price,
70+
'discount' => $this->getPriceDiscount($cartItem, $price)
6771
]);
6872
}
6973

@@ -82,14 +86,19 @@ private function getPrice(CartItem $cartItem): float
8286
switch ($displayType) {
8387
case Config::DISPLAY_TYPE_EXCLUDING_TAX:
8488
case Config::DISPLAY_TYPE_BOTH:
85-
$price = $cartItem->getConvertedPrice();
89+
$price = ($cartItem->getRowTotal() - $cartItem->getDiscountAmount()) / $cartItem->getQty();
8690
break;
8791
case Config::DISPLAY_TYPE_INCLUDING_TAX:
8892
default:
89-
$price = $cartItem->getPriceInclTax();
93+
$price = $cartItem->getRowTotalInclTax() / $cartItem->getQty();
9094
break;
9195
}
9296

9397
return $this->priceFormatter->format((float)$price);
9498
}
99+
100+
private function getPriceDiscount(CartItem $cartItem, float $price): float
101+
{
102+
return $cartItem->getDiscountAmount() / $cartItem->getQty();
103+
}
95104
}

0 commit comments

Comments
 (0)