Skip to main content
Back to packages
234 GitHub stars

safemood/discountify

View on GitHub

Description

Laravel package for dynamic discounts with custom conditions.

Define Conditions

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' => 'promo_early_spring_sale_2024',
'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);
}
}

Set Items, Global Discount, and Tax Rate

$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 Total Amounts

// Calculate the total amount considering the set conditions and discounts
 
$total = Discountify::total();
 
// Calculate total amount with detailed breakdown
// (array contains total, subtotal, tax amount, total after discount, savings, tax rate, discount rate)
$total = Discountify::totalDetailed();
 
// 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) (before discounts)
 
$taxAmount = Discountify::taxAmount(19);
 
// Calculate tax amount with tax applied after discounts
 
$taxAmount = Discountify::calculateTaxAmount(19, true);
 
// Calculate the amount saved
$savings = Discountify::savings();

Recent Courses on Laravel Daily

Next.js Basics for Laravel Developers

11 lessons
58 min

Laravel 13 Starter Kit Teams and Customizations

10 lessons
33 min

Queues in Laravel 13

18 lessons
1 h 12 min read