FrittenKeeZ / laravel-vouchers

Voucher system for Laravel 9+
MIT License
55 stars 15 forks source link

Create vouchers with custom name #7

Closed 735l4 closed 3 years ago

735l4 commented 3 years ago

Is there a way to use custom naming to vouchers? I tried using with character but it's shuffling the input character and also tried voucher generate but it is not creating voucher and throwing Call to a member function create() on string

FrittenKeeZ commented 3 years ago

@735l4 yes, you can create a voucher using the model directly:

$voucher = Voucher::create(['code' => 'FREEPIZZA2021']);
735l4 commented 3 years ago

That's great didn't knew it was there would be helpful if you included this readme because it is very essential feature imo.

FrittenKeeZ commented 3 years ago

@735l4 sure thing, I'll add that to my list of tasks :)

735l4 commented 3 years ago

How do I add amount to it and also I'm confused on how redeem works too I checked the model and I don't see fillable for amount. I checked the docs and only the Facades docs are clear to me. Sorry if it's too obvious i'm just confused

FrittenKeeZ commented 3 years ago

@735l4 can you explain to me what you want to achieve, and how you want to use the vouchers? Then I'll try my best to explain it to you, or note what documentation I'm missing.

735l4 commented 3 years ago

I'm trying to make an food delivery app voucher campaign so I want to generate custom vouchers with start and expiry date and given amount so users should be able to redeem it within the given time frame. Also would be helpful if the vouchers could be associated to any given model like polymorphic relation

FrittenKeeZ commented 3 years ago

Okay, so you want multiple users to be able to redeem the same voucher? Should they be able to redeem the same voucher more than once? The amount you can store using the metadata property, and related entities can be added using Voucher::addEntities()

You would be able to do something like this:

$restaurant = Restaurant::whereName('PZ1')->first();
$voucher = Voucher::make([
    'code' => 'PZ1-MAY21-100',
    'starts_at' => Carbon::parse('2021-05-01 00:00:00'),
    'expires_at' => Carbon::parse('2021-05-31 23:59:59'),
    'metadata' => ['amount' => 100, 'campaign' => true, 'once' => true],
]);
$voucher->owner()->associate($restaurant)->save();
$voucher->addEntities(...Category::where('name', 'LIKE', '%pizza%')->get());

Then you can add event handling like so:

Voucher::shouldMarkRedeemed(function (Voucher $voucher) {
    return empty($voucher->metadata['campaign']);
});
Voucher::redeeming(function (Voucher $voucher) {
    $metadata = $voucher->metadata;
    if (($metadata['campaign'] ?? false) && ($metadata['once'] ?? false)) {
        $metadata['redeemers'] = $metadata['redeemers'] ?? [];
        if (in_array($voucher->redeemer->redeemer->getKey(), $metadata['redeemers'])) {
            return false;
        }
        $metadata['redeemers'][] = $voucher->redeemer->redeemer->getKey();
        $voucher->metadata = $metadata;
    }
});
735l4 commented 3 years ago

Thanks dude. yea I want multiple user to redeem the voucher but user should be able to redeem it only once

FrittenKeeZ commented 3 years ago

@735l4 I just updated my example to handle the use case, so if you can just test it out and provide feedback, that'll be great :)

735l4 commented 3 years ago

It's throwing undefined method exception. Call to undefined method FrittenKeeZ\Vouchers\Models\Voucher::owner()

Also i'm confused about the event handling. how do i use it

735l4 commented 3 years ago

It's throwing undefined method exception. Call to undefined method FrittenKeeZ\Vouchers\Models\Voucher::owner()

Also i'm confused about the event handling. how do i use it

I fixed it by upgrading the package but still I'm confused with event handling thing. how do i redeem the coupon from the controller.

FrittenKeeZ commented 3 years ago

@735l4 that's explained here The event handling should be added to your EventProvider