Skip to content

Commit b73462b

Browse files
committed
[custom price] fix bug
1 parent df984ad commit b73462b

File tree

3 files changed

+23
-13
lines changed

3 files changed

+23
-13
lines changed

src/Objects/Cart.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -90,12 +90,13 @@ public function getTotal(Customer $customer): string
9090
foreach ($this->items as $productId => $_items) {
9191
foreach ($_items as $item) {
9292
$product = $item->getProduct();
93-
$prices[$productId] = $item->getPricePerItem()
94-
?? $prices[$productId]
95-
?? $product->getAmountProduct($customer)
96-
;
93+
$pricePerItem = $item->getPricePerItem();
94+
if ($pricePerItem === null) {
95+
$prices[$productId] ??= $product->getAmountProduct($customer);
96+
$pricePerItem = $prices[$productId];
97+
}
9798

98-
$price = $this->math->mul($this->getQuantity($product), $prices[$productId]);
99+
$price = $this->math->mul(count($item), $pricePerItem);
99100
$result = $this->math->add($result, $price);
100101
}
101102
}

src/Traits/CartPay.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -126,14 +126,17 @@ public function payCart(CartInterface $cart, bool $force = false): array
126126
foreach ($cart->getBasketDto()->items() as $item) {
127127
foreach ($item->getItems() as $product) {
128128
$productId = $product::class.':'.$castService->getModel($product)->getKey();
129-
$prices[$productId] = $item->getPricePerItem()
130-
?? $prices[$productId]
131-
?? $product->getAmountProduct($this);
129+
$pricePerItem = $item->getPricePerItem();
130+
if ($pricePerItem === null) {
131+
$prices[$productId] ??= $product->getAmountProduct($this);
132+
$pricePerItem = $prices[$productId];
133+
}
134+
132135
$transfers[] = $prepareService->transferLazy(
133136
$this,
134137
$product,
135138
Transfer::STATUS_PAID,
136-
$prices[$productId],
139+
$pricePerItem,
137140
$assistantService->getMeta($basketDto, $product)
138141
);
139142
}

tests/Units/Domain/ProductTest.php

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -269,19 +269,25 @@ public function testPayCustomPrice(): void
269269
*/
270270
$buyer = BuyerFactory::new()->create();
271271
$product = ItemFactory::new()->create([
272-
'quantity' => 1,
272+
'quantity' => 2,
273273
'price' => 5_000,
274274
]);
275275

276-
$buyer->deposit(1_000);
277-
self::assertSame(1_000, $buyer->balanceInt);
276+
self::assertSame(0, $buyer->balanceInt);
277+
278+
$buyer->deposit(6_000 + (int) $buyer->wallet->getKey());
279+
self::assertSame(6_000 + (int) $buyer->wallet->getKey(), $buyer->balanceInt);
278280

279281
$cart = app(Cart::class)
280282
->withItem($product, pricePerItem: 1_000)
283+
->withItem($product)
281284
;
282285

286+
self::assertSame(6000 + (int) $buyer->wallet->getKey(), (int) $cart->getTotal($buyer));
287+
283288
$transfers = $buyer->payCart($cart);
284-
self::assertCount(1, $transfers);
289+
self::assertSame(0, $buyer->balanceInt);
290+
self::assertCount(2, $transfers);
285291
}
286292

287293
/**

0 commit comments

Comments
 (0)