Discountify is a Laravel package designed for managing dynamic discounts with custom conditions. It allows you to create flexible and powerful discounting strategies, easily defining conditions and applying percentage-based discounts to enhance your e-commerce application.
You can install the package via composer:
composer require safemood/discountify
You can publish the config file with:
php artisan vendor:publish --tag="discountify-config"
This is the contents of the published config file:
// config/discountify.php
return [
'condition_namespace' => 'App\\Conditions',
'condition_path' => app_path('Conditions'),
'fields' => [
'price' => 'price',
'quantity' => 'quantity',
],
'global_discount' => 0,
'global_tax_rate' => 0,
];
use Illuminate\Support\ServiceProvider;
use Safemood\Discountify\Facades\Condition;
use Carbon\Carbon;
class AppServiceProvider extends ServiceProvider
{
public function boot()
{
// If items are more than 2, apply a 10% discount.
Condition::define('more_than_2_products_10', fn (array $items) => count($items) > 2, 10)
// If the date is within a 7-day interval starting March 1, 2024, apply a 15% discount.
->add([
[
'slug' => 'more_than_3_products_15',
'condition' => fn ($items) => now()->between(
Carbon::createFromDate(2024, 3, 1),
Carbon::createFromDate(2024, 3, 15)->addDays(7)
),
'discount' => 15,
],
// If 'special' items are in the cart, apply a 10% discount.
[
'slug' => 'special_type_product_10',
'condition' => fn ($items) => in_array('special', array_column($items, 'type')),
'discount' => 10,
],
])
// If the user has a renewal, apply a 10% discount.
->defineIf('client_has_renewal_10', auth()->user()->hasRenewal(), 10);
}
}
$items = [
['id' => '1', 'quantity' => 2, 'price' => 50],
['id' => '2', 'quantity' => 1, 'price' => 100, 'type' => 'special'],
];
// Set the items in the cart
Discountify::setItems($items)
// Set a global discount for all items in the cart
->setGlobalDiscount(15)
// Set a global tax rate for all items in the cart
->setGlobalTaxRate(19);
// Calculate the total amount considering the set conditions and discounts
$total = Discountify::total();
// Calculate the total amount with the applied global discount
$totalWithDiscount = Discountify::totalWithDiscount();
// Calculate the total amount with taxes applied based on the set global tax rate
$totalWithTaxes = Discountify::tax();
// Calculate the total tax amount based on the given tax rate (19 in this case)
$taxAmount = Discountify::taxAmount(19);
// Set custom field names through configuration
return [
'condition_namespace' => 'App\\Conditions',
'condition_path' => app_path('Conditions'),
'fields' => [
'price' => 'price',
'quantity' => 'quantity',
],
'global_discount' => 0,
'global_tax_rate' => 0,
'fire_events' => env('DISCOUNTIFY_FIRE_EVENTS', true)
];
// Alternatively, set dynamic field names on the fly
$items = [
['id' => 'item1', 'qty' => 2, 'amount' => 20],
['id' => 'item2', 'qty' => 1, 'amount' => 20],
];
$discountify->setFields([
'price' => 'amount',
'quantity' => 'qty',
])->setItems($items);
$totalWithDiscount = $discountify->totalWithDiscount(50);
The classes in App\Conditions will be auto-discovered by Discountify for seamless integration—no configuration is needed.
// app/Conditions
<?php
namespace App\Conditions;
use Safemood\Discountify\Contracts\ConditionInterface;
class MoreThan1ProductsCondition implements ConditionInterface
{
public string $slug = 'more_than_1_products_10';
public int $discount = 10;
public function __invoke(array $items): bool
{
return count($items) > 1;
}
}
This will allows you to exclude specific conditions based on the "skip" field.
Using Condition::define:
Condition::define('condition2', fn ($items) => false, 20, true); // Will be skipped
- Using Condition::add:
Condition::add([
['slug' => 'condition1', 'condition' => fn ($items) => true, 'discount' => 10, 'skip' => false], // Won't be skipped
['slug' => 'condition2', 'condition' => fn ($items) => false, 'discount' => 20, 'skip' => true], // Will be skipped
['slug' => 'condition3', 'condition' => fn ($items) => true, 'discount' => 30], // Will not be skipped (default skip is false)
]);
- Using Class-Based Conditions:
To create a class-based condition using the discountify:condition
artisan command, you can run the following command:
Options:
--discount (-d): Specifies the discount value for the condition. Default value is 0.
--slug (-s): Specifies the slug for the condition. If not provided, the name of the condition will be used as the slug.
--force (-f): Creates the class even if the condition class already exists.
php artisan discountify:condition OrderTotalDiscount
php artisan discountify:condition OrderTotalDiscount --discount=10 --slug OrderTotal
<?php
namespace App\Conditions;
use Safemood\Discountify\Contracts\ConditionInterface;
class OrderTotalDiscount implements ConditionInterface
{
public bool $skip = true; // Set to true to skip the condition
public string $slug = 'order_total';
public int $discount = 10;
public function __invoke(array $items): bool
{
return count($items) > 5;
}
}
You can listen for the DiscountAppliedEvent
using Laravel's Event system.
Ensure the following configuration in the discountify.php file:
// config/discountify.php
'fire_events' => env('DISCOUNTIFY_FIRE_EVENTS', true) // Toggle event dispatching
// app/Providers/EventServiceProvider.php
use Illuminate\Support\Facades\Event;
use Safemood\Discountify\Events\DiscountAppliedEvent;
public function boot(): void
{
Event::listen(function (DiscountAppliedEvent $event) {
// Your event handling logic here
// dd($event);
});
}
Check the Laravel Events documentation for more details.
Coupon based discounts to easily apply and calculate discounts on a given coupon code (working on it !)
composer test
Please see CHANGELOG for more information on what has changed recently.
Please see CONTRIBUTING for details.
Please review our security policy on how to report security vulnerabilities.
The MIT License (MIT). Please see License File for more information.