Skip to content
This repository has been archived by the owner on Feb 2, 2022. It is now read-only.

VariableDiscountHandler #155

Open
Paulsky opened this issue Feb 20, 2020 · 12 comments
Open

VariableDiscountHandler #155

Paulsky opened this issue Feb 20, 2020 · 12 comments

Comments

@Paulsky
Copy link

Paulsky commented Feb 20, 2020

Does someone already written a VariableDiscountHandler? I mean, instead of coupons with fixed discount, coupons with percentages as discount.

Would you accept a PR if someone writes a VariableDiscountHandler @sandervanhooft ? I think this is a common use case. If someone uses coupons, the options will most of the time be there; fixed or variable discount.

@sandervanhooft
Copy link
Collaborator

Would you accept a PR if someone writes a VariableDiscountHandler @sandervanhooft ?

Sure! Don't forget to include tests. FYI I'm in survival mode this week, hope to address most issues next week.

@Paulsky
Copy link
Author

Paulsky commented Feb 20, 2020

Thanks @sandervanhooft ! I will try to create a PR for this! I wish you all the best with your housing unit!! 🙏

@GertTimmerman
Copy link

@Paulsky Any progress on this? I need a percentage discount handler for myself, but I don't want to reinvent the wheel ;)

@Paulsky
Copy link
Author

Paulsky commented Mar 31, 2020

@GertTimmerman I haven't started to work on this to be honest. But I'm still interested and willing to develop this. However, I don't know when I can start developing.... Do you have time and resources to work on this? Please let me know if you get started

@GertTimmerman
Copy link

GertTimmerman commented Mar 31, 2020

@Paulsky I am able to take a look at it, but I can't promise anything. I will let you know (in this thread) if I have made some progress. It's not my top prio to have this.

@Paulsky
Copy link
Author

Paulsky commented Mar 31, 2020

Alright! Let's keep each other posted. I will get in touch if I am able to start working on this. Maybe we can help each other out.

@sandervanhooft
Copy link
Collaborator

Hi @GertTimmerman and @Paulsky , how are you doing on this? Need any help?

@GertTimmerman
Copy link

Hi @GertTimmerman and @Paulsky , how are you doing on this? Need any help?

I have created a PercentageDiscountHandler, But I have not been able to test everything because you have to do tests with specific mollie orders, etc.

@Otto-munch
Copy link

Any status update on a variable discount handler?

@Paulsky
Copy link
Author

Paulsky commented Oct 20, 2020

I'm sorry @Otto-munch , I still haven't got the change to work on this. Maybe you can help @GertTimmerman out? I do remember that writing test for this was a bit difficult.

@salmanhijazi
Copy link

Here's how I created a PercentageDiscountHandler for myself. Just extended the FixedDiscountHandler and changed the calculation logic.

<?php

namespace App\Libraries\Cashier\Coupon;

use Laravel\Cashier\Order\OrderItem;
use Laravel\Cashier\Order\OrderItemCollection;
use Laravel\Cashier\Coupon\FixedDiscountHandler as CoreFixedDiscountHandler;
use Money\Money;

class PercentageDiscountHandler extends CoreFixedDiscountHandler
{
    /**
     * @param \Money\Money $base The amount the discount is applied to.
     * @return \Money\Money
     */
    protected function unitPrice(Money $base)
    {
        $discount = $base->multiply( $this->context('discount') * 0.01 );

        if($this->context('allow_surplus', false) && $discount->greaterThan($base)) {
            return $base->negative();
        }

        return $discount->negative();
    }
}

I'm loading coupons from the database. So I added this to my Coupon model to make it work.

/**
     * Builds a Cashier coupon from the current model.
     *
     * @returns \Laravel\Cashier\Coupon\Coupon
     */
    public function buildCashierCoupon(): CashierCoupon
    {
        $context = [
            'description' => $this->campaign->title,
            'discount' => $this->campaign->discount,
        ];

        return (new CashierCoupon(
            $this->name,
            ($this->campaign->discount_type == 'percentage' ? new PercentageDiscountHandler : new FixedDiscountHandler),
            $context))
        ->withTimes($this->campaign->times);
    }

I'm grouping coupons by campaigns and have a discount_type assigned to the campaign.

@jasperf
Copy link

jasperf commented May 26, 2021

Also see #291 added by @GertTimmerman

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

No branches or pull requests

6 participants