-
Notifications
You must be signed in to change notification settings - Fork 56
8.0 Coupon System.
Throughout this system you can do the following :
- Generate Coupon
- Validate Coupon
- Deactivate Coupon
- Activate Coupon.
- Regenerate Coupon.
- Regenerate Inactive Coupon.
- Purchase Coupons.
- Release Coupons.
- fetch Applied Coupons on products , variations etc..
First things first , the basics which is generating the coupon.
- Generating Coupon
You probably guessed, It's an alias of the create method that eloquent prevails.
Coupon::generate([
'amount' => 100,
'expires_at' => Carbon::now()->addWeeks(3)->format('Y-m-d H:i:s')
]);
If you checked on the migrations table, we actually need to still insert the code and the user_id , That's where the observer comes into play and inserts a random string consists of 32 character, and the authenticated user's id.
If you wish to let the user generates his coupon code, you can just pass that within the generate method, and the observer will only work if the code isn't supplied.
Coupon::generate([
'amount' => 100,
'expires_at' => Carbon::now()->addWeeks(3)->format('Y-m-d H:i:s'),
'code' => request('code') // or you set a hard-coded code or whatever..
]);
- Validate Coupon
This method checks against expiration date, active and amount.
$coupon = Coupon::find(1); // an expired coupon.
Coupon::validate($coupon); // throws an CouponExpiredException.
$coupon = Coupon::find(2); // coupon that's not expired yet but inactive.
Coupon::validate($coupon); // returns false.
$coupon = Coupon::find(3); // coupon that's out of amount.
Coupon::validate($coupon); // returns false.
// otherwise that, it will return true.
- Deactivate Coupon
This method retrieves the coupon and sets the active column to false
Coupon::deactivate($id); // if the id doesn't exist, an exception is thrown, unless that it returns boolean.
- Activate Coupon
You probably guessed it, It sets the active column to true.
Coupon::activate($id); // if the id doesn't exist, an exception is thrown, unless that it returns boolean.
- Regenerating Coupon
This method finds the coupon by id, then regenerates the coupon's code either by passing a selected code, or generating a random string for the associated coupon
Coupon::regenerate(1); // regenerates the coupon code for the coupon of id 1.
Coupon::regenerate(1, 'something-random'); // sets the coupon code for the coupon of id 1 with 'something-random'
Coupon::regenerate(10,'something-random'); // ModelNotFoundException is thrown.
- Regenerating and Validate the Coupon
This method regenerates the code, and validates the coupon if It's not valid.
If The Coupon is expired , the expiration date will be updated, but the coupon's status remains the same.
Let's start with regenerating the coupon code first.
Coupon::regenerate($id); // returns the updated coupon.
This will update the code of the coupon and set a random string .
If you wish to set a custom code for the coupon, you can pass it as a second argument
Coupon::regenerate($id,'some-random-code'); // returns the updated coupon.
Perhaps you might like to validate the coupon before updating the code, just set the third argument to true.
First things first , we have got two scenarios for validating the coupon
-
If The coupon isn't active or out of amount, it will activate the coupon, and set the coupon amount to 1 if only It's out of amount.
-
If the coupon is expired, it will update the expiration date, and the coupon's status ( amount and availability ) remains same.
Coupon::regenerate($id,'some-random-code-string',true); // if the coupon isn't expired,it will set the availability to true and amount to one only if the current amount of coupon is less than 1.
Coupon::regenerate($id,'some-thing-random',true); // if the coupon is already expired, it will update the expiration date and by default adds one week to the current date.
// You can add your own number of weeks by passing the forth argument.
Coupon::regenerate($id,'something-random',true,4); // add 4 weeks for the expired coupon from now.