cashier-mollie icon indicating copy to clipboard operation
cashier-mollie copied to clipboard

VariableDiscountHandler

Open Paulsky opened this issue 5 years ago • 12 comments

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.

Paulsky avatar Feb 20 '20 08:02 Paulsky

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.

sandervanhooft avatar Feb 20 '20 10:02 sandervanhooft

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

Paulsky avatar Feb 20 '20 10:02 Paulsky

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

GertTimmerman avatar Mar 31 '20 08:03 GertTimmerman

@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

Paulsky avatar Mar 31 '20 08:03 Paulsky

@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.

GertTimmerman avatar Mar 31 '20 08:03 GertTimmerman

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.

Paulsky avatar Mar 31 '20 08:03 Paulsky

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

sandervanhooft avatar Jun 11 '20 13:06 sandervanhooft

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.

GertTimmerman avatar Jun 12 '20 12:06 GertTimmerman

Any status update on a variable discount handler?

Otto-munch avatar Oct 16 '20 08:10 Otto-munch

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.

Paulsky avatar Oct 20 '20 07:10 Paulsky

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.

salmanhijazi avatar Feb 01 '21 23:02 salmanhijazi

Also see https://github.com/laravel/cashier-mollie/pull/291 added by @GertTimmerman

jasperf avatar May 26 '21 01:05 jasperf