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

Set amount to the subtotal when the discount amount exceeds the subtotal #200

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 24 additions & 3 deletions src/PricingManager/Action/CartDiscount.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,22 @@ class CartDiscount implements DiscountInterface, CartActionInterface

protected float $percent = 0;

protected bool $onlyDiscountCart = false;

public function executeOnCart(EnvironmentInterface $environment): ActionInterface
{
$priceCalculator = $environment->getCart()->getPriceCalculator();

$subTotal = $priceCalculator->getSubTotal()->getAmount();

$amount = Decimal::create($this->amount);
if ($amount->isZero()) {
$amount = $priceCalculator->getSubTotal()->getAmount()->toPercentage($this->getPercent());
//round to 2 digits for further calculations to avoid rounding issues at later point

if ($this->onlyDiscountCart && $subTotal->sub($amount)->isNegative()) {
// prevent discounted amount to be higher than the subtotal
$amount = $subTotal;
} elseif ($amount->isZero()) {
$amount = $subTotal->toPercentage($this->getPercent());
// round to 2 digits for further calculations to avoid rounding issues at later point
$amount = Decimal::fromDecimal($amount->withScale(2));
}

Expand Down Expand Up @@ -66,6 +74,7 @@ public function toJSON(): string
'type' => 'CartDiscount',
'amount' => $this->getAmount(),
'percent' => $this->getPercent(),
'onlyDiscountCart' => $this->onlyDiscountCart(),
]);
}

Expand All @@ -87,6 +96,8 @@ public function fromJSON(string $string): ActionInterface
$this->setPercent($json->percent);
}

$this->setOnlyDiscountCart($json->onlyDiscountCart ?? false);

return $this;
}

Expand All @@ -109,4 +120,14 @@ public function getPercent(): float
{
return $this->percent;
}

public function setOnlyDiscountCart(bool $onlyDiscountCart): void
{
$this->onlyDiscountCart = $onlyDiscountCart;
}

public function onlyDiscountCart(): bool
{
return $this->onlyDiscountCart;
}
}
7 changes: 7 additions & 0 deletions src/Resources/public/js/pricing/config/item.js
Original file line number Diff line number Diff line change
Expand Up @@ -1346,6 +1346,13 @@ pimcore.bundle.EcommerceFramework.pricing.actions = {
value: data.percent,
maxValue: 100,
minValue: 0
}, {
xtype: "checkbox",
width: 100,
labelWidth: 120,
fieldLabel: t("bundle_ecommerce_pricing_config_action_cart_discount_only_discount_cart"),
name: "onlyDiscountCart",
value: data.onlyDiscountCart,
}
]
});
Expand Down
1 change: 1 addition & 0 deletions src/Resources/translations/admin.en.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ bundle_ecommerce_pricing_config_action_gift: Gift
bundle_ecommerce_pricing_config_action_cart_discount: Cart discount
bundle_ecommerce_pricing_config_action_cart_discount_percent: Discount in %
bundle_ecommerce_pricing_config_action_cart_discount_amount: Absolute Discount
bundle_ecommerce_pricing_config_action_cart_discount_only_discount_cart: Only discount cart
bundle_ecommerce_pricing_config_action_product_discount: Product discount
bundle_ecommerce_pricing_config_action_product_discount_percent: Discount in %
bundle_ecommerce_pricing_config_action_product_discount_amount: Absolute Discount
Expand Down
Loading