Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[API][UI][Behat][Refactor] OriginalUnitPrice property on the OrderItem entity#13563

Merged
GSadee merged 3 commits into
Sylius:1.11from
Rafikooo:original-unit-price-on-order-item-refactor
Feb 3, 2022
Merged

[API][UI][Behat][Refactor] OriginalUnitPrice property on the OrderItem entity#13563
GSadee merged 3 commits into
Sylius:1.11from
Rafikooo:original-unit-price-on-order-item-refactor

Conversation

@Rafikooo
Copy link
Copy Markdown
Contributor

@Rafikooo Rafikooo commented Jan 28, 2022

Q A
Branch? 1.11
Bug fix? no
New feature? no
BC breaks? no
Deprecations? no
License MIT

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.

@Rafikooo Rafikooo added the Shop ShopBundle related issues and PRs. label Jan 28, 2022
@Rafikooo Rafikooo requested a review from a team as a code owner January 28, 2022 18:22
Comment thread src/Sylius/Component/Core/Calculator/ProductVariantPriceCalculatorInterface.php Outdated
Comment thread src/Sylius/Bundle/CoreBundle/Migrations/Version20220127150747.php Outdated
Comment thread src/Sylius/Component/Core/Calculator/ProductVariantPriceCalculatorInterface.php Outdated
Comment thread src/Sylius/Component/Order/Model/OrderItem.php Outdated
Comment thread src/Sylius/Bundle/CoreBundle/Migrations/Version20220127150747.php Outdated
Comment thread src/Sylius/Component/Core/Calculator/ProductVariantPriceCalculatorInterface.php Outdated
@probot-autolabeler probot-autolabeler Bot added the Maintenance CI configurations, READMEs, releases, etc. label Jan 31, 2022
Comment thread src/Sylius/Component/Order/Model/OrderItemInterface.php Outdated
Comment thread UPGRADE-1.11.md Outdated
Comment thread src/Sylius/Component/Core/OrderProcessing/OrderPricesRecalculator.php Outdated
Comment thread UPGRADE-1.8.md Outdated
Comment thread src/Sylius/Component/Order/Model/OrderItem.php Outdated
@vvasiloi
Copy link
Copy Markdown
Contributor

vvasiloi commented Jan 31, 2022

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.

@probot-autolabeler probot-autolabeler Bot added the API APIs related issues and PRs. label Feb 1, 2022
@Rafikooo Rafikooo changed the title [OrderItem] Save originalUnitPrice to the database each time an item added to the cart [API][UI][Behat][Refactor] OriginalUnitPrice property on OrderItem entity Feb 1, 2022
@Rafikooo Rafikooo changed the title [API][UI][Behat][Refactor] OriginalUnitPrice property on OrderItem entity [API][UI][Behat][Refactor] OriginalUnitPrice property on the OrderItem entity Feb 1, 2022
Comment thread src/Sylius/Bundle/ApiBundle/Serializer/OrderItemNormalizer.php Outdated
Comment thread src/Sylius/Bundle/ApiBundle/Resources/config/serialization/OrderItem.xml Outdated
Comment thread src/Sylius/Bundle/ApiBundle/Serializer/OrderItemNormalizer.php Outdated
Comment thread src/Sylius/Bundle/ApiBundle/spec/Serializer/OrderItemNormalizerSpec.php Outdated
Comment thread src/Sylius/Bundle/ShopBundle/Resources/views/Cart/Summary/_item.html.twig Outdated
Comment thread src/Sylius/Bundle/ApiBundle/Resources/config/serialization/OrderItem.xml Outdated
Comment thread src/Sylius/Bundle/ApiBundle/Serializer/OrderItemNormalizer.php Outdated
Comment thread src/Sylius/Bundle/ApiBundle/Serializer/ProductVariantNormalizer.php
Comment thread src/Sylius/Bundle/ShopBundle/Resources/views/Cart/Summary/_item.html.twig Outdated
"productName": "Mug",
"id": @integer@,
"quantity": 3,
"originalPrice": null
Copy link
Copy Markdown
Contributor Author

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

"id": @integer@,
"quantity": 3,
"unitPrice": 2000,
"originalUnitPrice": 2000,
Copy link
Copy Markdown
Contributor Author

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

@Rafikooo Rafikooo requested review from GSadee and Zales0123 February 2, 2022 14:04
Comment on lines +31 to +49
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();
}
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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;
}

@GSadee GSadee merged commit d07a74b into Sylius:1.11 Feb 3, 2022
@GSadee
Copy link
Copy Markdown
Member

GSadee commented Feb 3, 2022

Thanks, Rafał! 🎉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

API APIs related issues and PRs. Maintenance CI configurations, READMEs, releases, etc. Shop ShopBundle related issues and PRs.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants