PHPackages                             safemood/discountify - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. safemood/discountify

ActiveLibrary

safemood/discountify
====================

Laravel package for dynamic discounts with custom conditions.

1.5.1(1y ago)2306.7k↓30.8%19[1 PRs](https://github.com/Safemood/discountify/pulls)MITPHPPHP ^8.1|^8.2CI passing

Since Jan 14Pushed 2mo ago3 watchersCompare

[ Source](https://github.com/Safemood/discountify)[ Packagist](https://packagist.org/packages/safemood/discountify)[ Docs](https://github.com/safemood/discountify)[ GitHub Sponsors](https://github.com/Safemood)[ RSS](/packages/safemood-discountify/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (10)Dependencies (12)Versions (17)Used By (0)

Laravel Discountify for dynamic discounts with custom conditions.
=================================================================

[](#laravel-discountify-for-dynamic-discounts-with-custom-conditions)

[![Latest Version on Packagist](https://camo.githubusercontent.com/ed21b4f193020bde186386e3ddd2fc48fec1a7fb3fbb3fba541deff8c19242fd/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f736166656d6f6f642f646973636f756e746966792e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/safemood/discountify)[![GitHub Tests Action Status](https://camo.githubusercontent.com/fa57db5f9957a0de38df271a7fc6eed8365d3b51df4925bfc8153b679aaad4d1/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f736166656d6f6f642f646973636f756e746966792f72756e2d74657374732e796d6c3f6272616e63683d6d61696e266c6162656c3d7465737473267374796c653d666c61742d737175617265)](https://github.com/safemood/discountify/actions?query=workflow%3Arun-tests+branch%3Amain)[![GitHub Code Style Action Status](https://camo.githubusercontent.com/a6ebfa6cdb9be6b0c01814dc77ad742eba57577fe68f72a915bc65d78e9e1ad4/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f736166656d6f6f642f646973636f756e746966792f6669782d7068702d636f64652d7374796c652d6973737565732e796d6c3f6272616e63683d6d61696e266c6162656c3d636f64652532307374796c65267374796c653d666c61742d737175617265)](https://github.com/safemood/discountify/actions?query=workflow%3A%22Fix+PHP+code+style+issues%22+branch%3Amain)[![Total Downloads](https://camo.githubusercontent.com/aea94c92a0a4f5aad849f807deaf1d1acffd817a2bc3c434059786508604c99c/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f736166656d6f6f642f646973636f756e746966792e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/safemood/discountify)

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.

- [Installation](#installation)
- [Usage](#usage)
    - [Define Discounts Conditions](#define-conditions)
    - [Set Items, Global Discount, and Tax Rate](#set-items-global-discount-and-tax-rate)
    - [Calculate Total Amounts](#calculate-total-amounts)
    - [Dynamic Field Names](#dynamic-field-names)
    - [Class-Based Discounts](#class-based-discounts)
    - [Skip Discounts conditions](#skip-discounts-conditions)
    - [Event Tracking](#event-tracking)
    - [Coupon Based Discounts](#coupon-based-discounts)

Installation
------------

[](#installation)

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,
    'fire_events' => env('DISCOUNTIFY_FIRE_EVENTS', true),
    'state_file_path' => env('DISCOUNTIFY_STATE_FILE_PATH', storage_path('app/discountify/coupons.json')),
];
```

Usage
-----

[](#usage)

Define 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

[](#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-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();
```

### Dynamic Field Names

[](#dynamic-field-names)

```
// Set custom field names through configuration
return [
    'condition_namespace' => 'App\\Conditions',
    'condition_path' => app_path('Conditions'),
    'fields' => [
        'price' => 'amount',
        'quantity' => 'qty',
    ],
    '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);
```

### Class-Based Discounts

[](#class-based-discounts)

The classes in App\\Conditions will be auto-discovered by Discountify for seamless integration—no configuration is needed.

- Create Class-Based Conditions:

To create a class-based condition using the `discountify:condition` artisan command, you can run the following command:

```
php artisan discountify:condition OrderTotalDiscount
```

```
php artisan discountify:condition OrderTotalDiscount --discount=10 --slug OrderTotal
```

```
