Skip to content

Commit

Permalink
Make not discounting price modifications optional
Browse files Browse the repository at this point in the history
  • Loading branch information
jan888adams committed Oct 9, 2024
1 parent f40dc6c commit fad0b13
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 1 deletion.
28 changes: 27 additions & 1 deletion src/PricingManager/Action/CartDiscount.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,32 @@ class CartDiscount implements DiscountInterface, CartActionInterface

protected float $percent = 0;

/**
* @deprecated CartDiscount will reduce the total cart price without affecting added price modifications.
*/
protected bool $onlyDiscountCart = false;

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

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

$amount = Decimal::create($this->amount);
if ($subTotal->sub($amount)->isNegative()) {

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));
} elseif (!$this->onlyDiscountCart) {
trigger_deprecation(
'pimcore/ecommerce-framework-bundle',
'1.2.2',
'Applying discounts not only to the cart is deprecated and will be removed in version 1.3. Cart discounts will reduce the total cart price without affecting added price modifications.'
);
}

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

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

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

return $this;
}

Expand All @@ -113,4 +129,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

0 comments on commit fad0b13

Please sign in to comment.