Open sanderha opened 7 years ago
@sanderha did you find any solution? If yes, can you share it?
@wernerkrauss If using the discounts module, to be able to show before/after price we have to calculate it for each item. Not sure which project I did that on.
On other shop projects I ended up creating a specific "BeforePrice" field on Product
, because that was much easier to understand for a client, as they almost never need the complex discount functionality of this module
EDIT: I found this code, not sure how good it is.
/**
* A bunch of this code is taken from \Shop\Discount\Calculator::calculate()
*
* @return int
*/
public function calcAfterDiscountPrice()
{
$classname = $this->owner->ClassName;
$total = 0;
// if its not a Product it'll be an OrderItem
$orderitem = $classname === 'Product' ? $this->owner->Item() : $this->owner;
$orderitem->setQuantity(1); // else it will take quantity from cart, we just want a basic discount per item for showing
$infoitem = new ItemPriceInfo($orderitem);
$discount = $this->getActiveDiscount();
if (!$discount) {
return $total;
}
// item discounts will update info items
$action = $discount->Type === "Percent" ?
new \ItemPercentDiscount([$infoitem], $discount) :
new \ItemFixedDiscount([$infoitem], $discount);
$action->perform();
// select best item-level discounts
$bestadjustment = $infoitem->getBestAdjustment();
if ($bestadjustment) {
$amount = $bestadjustment->getValue();
// prevent discounting more than original price
if ($amount > $infoitem->getOriginalTotal()) {
$amount = $infoitem->getOriginalTotal();
}
$total += $amount;
}
$total = $classname === 'Product' ? $this->owner->sellingPrice() - $total : $this->owner->UnitPrice() - $total;
return $total;
}
Hello
I've created a Discount and added some products to it, that should be affected by this discount. What is the smartest way to create Before/after prices in templates to reflect the discount the customer will receive?