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

Before & After prices in templates #51

Open
sanderha opened this issue Feb 3, 2017 · 2 comments
Open

Before & After prices in templates #51

sanderha opened this issue Feb 3, 2017 · 2 comments

Comments

@sanderha
Copy link

sanderha commented Feb 3, 2017

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?

@wernerkrauss
Copy link
Contributor

@sanderha did you find any solution? If yes, can you share it?

@sanderha
Copy link
Author

sanderha commented Nov 19, 2018

@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;
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants