New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[API][UI][Behat][Refactor] OriginalUnitPrice property on the OrderItem entity #13563
[API][UI][Behat][Refactor] OriginalUnitPrice property on the OrderItem entity #13563
Conversation
src/Sylius/Component/Core/Calculator/ProductVariantPriceCalculatorInterface.php
Outdated
Show resolved
Hide resolved
src/Sylius/Bundle/CoreBundle/Migrations/Version20220127150747.php
Outdated
Show resolved
Hide resolved
src/Sylius/Component/Core/Calculator/ProductVariantPriceCalculatorInterface.php
Outdated
Show resolved
Hide resolved
src/Sylius/Bundle/CoreBundle/Migrations/Version20220127150747.php
Outdated
Show resolved
Hide resolved
src/Sylius/Component/Core/Calculator/ProductVariantPriceCalculatorInterface.php
Outdated
Show resolved
Hide resolved
src/Sylius/Component/Core/OrderProcessing/OrderPricesRecalculator.php
Outdated
Show resolved
Hide resolved
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. |
src/Sylius/Bundle/ApiBundle/Resources/config/serialization/OrderItem.xml
Outdated
Show resolved
Hide resolved
src/Sylius/Bundle/ApiBundle/spec/Serializer/OrderItemNormalizerSpec.php
Outdated
Show resolved
Hide resolved
src/Sylius/Bundle/ShopBundle/Resources/views/Cart/Summary/_item.html.twig
Outdated
Show resolved
Hide resolved
src/Sylius/Bundle/ApiBundle/Resources/config/serialization/OrderItem.xml
Outdated
Show resolved
Hide resolved
src/Sylius/Bundle/ApiBundle/Serializer/ProductVariantNormalizer.php
Outdated
Show resolved
Hide resolved
src/Sylius/Bundle/ShopBundle/Resources/views/Cart/Summary/_item.html.twig
Outdated
Show resolved
Hide resolved
@@ -37,8 +37,7 @@ | |||
"variant": "\/api\/v2\/shop\/product-variants\/MUG_BLUE", | |||
"productName": "Mug", | |||
"id": @integer@, | |||
"quantity": 3, | |||
"originalPrice": null |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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
@@ -57,6 +57,7 @@ | |||
"id": @integer@, | |||
"quantity": 3, | |||
"unitPrice": 2000, | |||
"originalUnitPrice": 2000, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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; | |
} |
src/Sylius/Bundle/ShopBundle/Twig/OrderItemOriginalPriceToDisplayExtension.php
Show resolved
Hide resolved
…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
originalUnitPrice
of this order item.Using saved
originalUnitPrice
instead of using the prices from ChannelPricing while processing cart each time.