[API][UI][Behat][Refactor] OriginalUnitPrice property on the OrderItem entity#13563
Conversation
|
WDYT about adding a method on the Order model to get the items total based on original unit price? Edit: it would also require additional methods on the order item to calculate it's total first, plus a property to store the result and methods to recalculate the total. Just like with the current units and items total. |
| "productName": "Mug", | ||
| "id": @integer@, | ||
| "quantity": 3, | ||
| "originalPrice": null |
There was a problem hiding this comment.
TBH I'm not sure about that field. I checked the master demo API endpoint
api/v2/shop/account/orders/{tokenValue}/payments/{paymentId}
and it seems that that field should not be included, but I had one and it broke the tests
| "id": @integer@, | ||
| "quantity": 3, | ||
| "unitPrice": 2000, | ||
| "originalUnitPrice": 2000, |
There was a problem hiding this comment.
After consultation with @GSadee originalUnitPrice can be included in that response
| if ($this->originalUnitPriceIsGreaterThanUnitPriceAndDiscountedUnitPrice($item)) { | ||
| return $item->getOriginalUnitPrice(); | ||
| } elseif($this->originalUnitPriceIsNullAndUnitPriceIsGreaterThanDiscountedUnitPrice($item)) { | ||
| return $item->getUnitPrice(); | ||
| } | ||
|
|
||
| return null; | ||
| } | ||
|
|
||
| private function originalUnitPriceIsGreaterThanUnitPriceAndDiscountedUnitPrice(OrderItem $item): bool | ||
| { | ||
| return ($item->getOriginalUnitPrice() !== null && $item->getOriginalUnitPrice() > $item->getUnitPrice()) || | ||
| ($item->getOriginalUnitPrice() !== null && $item->getOriginalUnitPrice() > $item->getDiscountedUnitPrice()); | ||
| } | ||
|
|
||
| private function originalUnitPriceIsNullAndUnitPriceIsGreaterThanDiscountedUnitPrice(OrderItem $item): bool | ||
| { | ||
| return $item->getOriginalUnitPrice() === null && $item->getUnitPrice() > $item->getDiscountedUnitPrice(); | ||
| } |
There was a problem hiding this comment.
| if ($this->originalUnitPriceIsGreaterThanUnitPriceAndDiscountedUnitPrice($item)) { | |
| return $item->getOriginalUnitPrice(); | |
| } elseif($this->originalUnitPriceIsNullAndUnitPriceIsGreaterThanDiscountedUnitPrice($item)) { | |
| return $item->getUnitPrice(); | |
| } | |
| return null; | |
| } | |
| private function originalUnitPriceIsGreaterThanUnitPriceAndDiscountedUnitPrice(OrderItem $item): bool | |
| { | |
| return ($item->getOriginalUnitPrice() !== null && $item->getOriginalUnitPrice() > $item->getUnitPrice()) || | |
| ($item->getOriginalUnitPrice() !== null && $item->getOriginalUnitPrice() > $item->getDiscountedUnitPrice()); | |
| } | |
| private function originalUnitPriceIsNullAndUnitPriceIsGreaterThanDiscountedUnitPrice(OrderItem $item): bool | |
| { | |
| return $item->getOriginalUnitPrice() === null && $item->getUnitPrice() > $item->getDiscountedUnitPrice(); | |
| } | |
| if ( | |
| $item->getOriginalUnitPrice() !== null && | |
| ($item->getOriginalUnitPrice() > $item->getUnitPrice()) || $item->getOriginalUnitPrice() > $item->getDiscountedUnitPrice()) | |
| ) { | |
| return $item->getOriginalUnitPrice(); | |
| } | |
| if ($item->getOriginalUnitPrice() === null && $item->getUnitPrice() > $item->getDiscountedUnitPrice()) { | |
| return $item->getUnitPrice(); | |
| } | |
| return null; | |
| } |
…is added to the cart
…nalPrice' to 'originalUnitPrice'
|
Thanks, Rafał! 🎉 |
To increase immutability, at the moment of adding an item to the cart, we should save the original price to the
originalUnitPriceof this order item.Using saved
originalUnitPriceinstead of using the prices from ChannelPricing while processing cart each time.