Skip to content
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

[CartPromotion][CatalogPromotion] Receiving discount only on non discounted products #13349

Merged

Conversation

GSadee
Copy link
Member

@GSadee GSadee commented Dec 1, 2021

Q A
Branch? master
Bug fix? no
New feature? yes
BC breaks? no
Deprecations? no
Related tickets based on #13328
License MIT

@GSadee GSadee added Feature New feature proposals. Behat Issues and PRs aimed at improving Behat usage. Shop ShopBundle related issues and PRs. API APIs related issues and PRs. labels Dec 1, 2021
@GSadee GSadee requested a review from a team as a code owner December 1, 2021 14:27
@probot-autolabeler probot-autolabeler bot added the Admin AdminBundle related issues and PRs. label Dec 1, 2021
@GSadee GSadee force-pushed the cart-promotions-vs-catalog-promotions branch 3 times, most recently from e74c157 to f3ee1bb Compare December 5, 2021 20:59
@SirDomin SirDomin force-pushed the cart-promotions-vs-catalog-promotions branch from 640d16e to 9e6efd5 Compare December 8, 2021 02:47
@SirDomin SirDomin changed the title [WIP][CartPromotion][CatalogPromotion] Receiving discount only on non discounted products [CartPromotion][CatalogPromotion] Receiving discount only on non discounted products Dec 8, 2021
@SirDomin SirDomin force-pushed the cart-promotions-vs-catalog-promotions branch from 9e6efd5 to 4227f91 Compare December 9, 2021 08:27
@SirDomin SirDomin force-pushed the cart-promotions-vs-catalog-promotions branch from 4227f91 to 9ce4cb8 Compare December 9, 2021 08:47
@@ -27,7 +27,7 @@ public function __construct(ProportionalIntegerDistributorInterface $proportiona
$this->proportionalIntegerDistributor = $proportionalIntegerDistributor;
}

public function distribute(array $orderItems, int $amount, ChannelInterface $channel): array
public function distribute(array $orderItems, int $amount, ChannelInterface $channel, bool $appliesOnDiscounted): array
Copy link
Contributor

Choose a reason for hiding this comment

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

is it BC break? we shouldn't add an argument without default value to a public function.

Copy link
Member

Choose a reason for hiding this comment

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

That would be a BC Break, but this code is only on master branch which means it is not used by anyone 💃 We're safe 🚀

@@ -460,4 +460,18 @@ public function setCustomerIp(?string $customerIp): void
{
$this->customerIp = $customerIp;
}

public function getNonDiscountedItemsTotal(): int
{
Copy link
Contributor

Choose a reason for hiding this comment

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

I am not sure but can we use here array_reduce instead of foreach?

/**
* @Then /^the product "([^"]+)" should have total price ("[^"]+") in the cart$/
*/
public function theProductShouldHaveTotalPriceInTheCart(string $productName, int $totalPrice): void
Copy link
Member

Choose a reason for hiding this comment

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

If we use a transformer, we could just call $product->getName() in line 426 and remove exception from the end of the function

@@ -27,7 +27,7 @@ public function __construct(ProportionalIntegerDistributorInterface $proportiona
$this->proportionalIntegerDistributor = $proportionalIntegerDistributor;
}

public function distribute(array $orderItems, int $amount, ChannelInterface $channel): array
public function distribute(array $orderItems, int $amount, ChannelInterface $channel, bool $appliesOnDiscounted): array
Copy link
Member

Choose a reason for hiding this comment

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

That would be a BC Break, but this code is only on master branch which means it is not used by anyone 💃 We're safe 🚀

private function getTotalPrice(OrderItemInterface $orderItem, bool $appliesOnDiscounted, ChannelInterface $channel): int
{
$variant = $orderItem->getVariant();
if ($appliesOnDiscounted === false && !empty($variant->getAppliedPromotionsForChannel($channel))) {
Copy link
Member

Choose a reason for hiding this comment

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

!$appilesOnDiscounted?

Copy link
Member

Choose a reason for hiding this comment

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

Also, maybe we could have hasAppliedPromotionsForChannel($channel): bool on the variant? It would seem more natural this way

$distributedAmounts[] = 0;
} else {
$distributedAmounts[] = (int) round(($element * $amount) / $total, 0, \PHP_ROUND_HALF_DOWN);
}
Copy link
Member

Choose a reason for hiding this comment

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

I would extract it to the private function or make it shorter:

$distributedAmounts[] = ($element === 0) ? 0 : (int) round(($element * $amount) / $total, 0, \PHP_ROUND_HALF_DOWN);

}
}

if(array_sum($distributedAmounts) === 0) {
Copy link
Member

Choose a reason for hiding this comment

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

Missing space after if 😱

Comment on lines 71 to 90
if ($this->minimumPriceDistributor !== null) {
$splitPromotion = $this->minimumPriceDistributor->distribute($subject->getItems()->toArray(), $promotionAmount, $subject->getChannel());
$splitPromotion = $this->minimumPriceDistributor->distribute($subject->getItems()->toArray(), $promotionAmount, $subject->getChannel(), $promotion->getAppliesToDiscounted());
} else {
$itemsTotal = [];
foreach ($subject->getItems() as $orderItem) {
if ($promotion->getAppliesToDiscounted()) {
$itemsTotal[] = $orderItem->getTotal();

continue;
}

$variant = $orderItem->getVariant();
if (!empty($variant->getAppliedPromotionsForChannel($subject->getChannel()))) {
$itemsTotal[] = 0;

continue;
}

$itemsTotal[] = $orderItem->getTotal();
}
Copy link
Member

Choose a reason for hiding this comment

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

My soul hurts, as this part of code is identical to this one 😢

@@ -107,4 +111,23 @@ private function calculate(int $unitTotal, ?int $minimumPrice, int $promotionAmo

return $promotionAmount;
}

private function canPromotionBeApplied(OrderItemUnitInterface $unit, PromotionInterface $promotion): bool
Copy link
Member

Choose a reason for hiding this comment

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

Shouldn't we make it protected? It's a solid part of logic, that would not be easily replaceable (as is the private function in the extendable class)

Scenario: Distributing discount proportionally between different products when one has minimum price specified and promotion does not apply on discounted products
Given this promotion does not apply on discounted products
And it gives "$27" discount to every order
And there is a catalog promotion "Fixed T-Shirt sale" that reduces price by fixed "$2.50" in the "United States" channel and applies on "PHP Mug" product
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
And there is a catalog promotion "Fixed T-Shirt sale" that reduces price by fixed "$2.50" in the "United States" channel and applies on "PHP Mug" product
And there is a catalog promotion "Fixed Mug sale" that reduces price by fixed "$2.50" in the "United States" channel and applies on "PHP Mug" product

@AdamKasp AdamKasp merged commit 3ff3cf7 into Sylius:master Dec 9, 2021
@AdamKasp
Copy link
Contributor

AdamKasp commented Dec 9, 2021

Thanks, Grzegorz! 🥇

@GSadee GSadee deleted the cart-promotions-vs-catalog-promotions branch December 10, 2021 06:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Admin AdminBundle related issues and PRs. API APIs related issues and PRs. Behat Issues and PRs aimed at improving Behat usage. Feature New feature proposals. Shop ShopBundle related issues and PRs.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants