Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Pricing Engine Laravel Package

php-junior/pricing-engine

View on GitHub
Deep Wiki
Context7

Pricing Engine

Latest Version on Packagist Total Downloads

A dynamic pricing rule engine for Laravel. This package allows you to define flexible pricing rules based on various conditions and apply actions like discounts or markups.

Installation

You can install the package via composer:

composer require php-junior/pricing-engine

You can publish the config file with:

php artisan vendor:publish --provider="PhpJunior\PricingEngine\Providers\PricingEngineServiceProvider"

You can run the migrations with:

php artisan migrate

Configuration

The configuration file config/pricing-engine.php allows you to customize:

  • Model classes
  • Table names
  • Priority order (highest first or lowest first)
  • Save Usage Records
  • Available operators
  • Available actions

Usage

Creating Pricing Rules

You can create pricing rules using the PricingEngine facade or service.

use PhpJunior\PricingEngine\Facades\PricingEngine;
use PhpJunior\PricingEngine\Data\ConditionData;
use PhpJunior\PricingEngine\Data\ActionData;

PricingEngine::make()->savePricingRule(
    name: 'VIP Discount',
    priority: 1,
    conditions: [
        new ConditionData(
            attribute: 'customer_group',
            operator: '=',
            value: 'vip'
        ),
        new ConditionData(
            attribute: 'price_total',
            operator: '>',
            value: '100'
        )
    ],
    actions: [
        new ActionData(
            type: 'percentage_discount',
            value: 10
        )
    ]
);

Attributes in conditions can be any key.

To update an existing rule, provide the rulr ID in them make method:

PricingEngine::make(id: 1)->savePricingRule( ... );

To delete a rule, use the deletePricingRule method:

PricingEngine::make(id: 1)->deletePricingRule();

Fetch all pricing rules:

$rules = PricingEngine::make()->getAllPricingRules();

Calculating Price

To calculate the price based on the defined rules, use the calculatePrice method. You need to provide the base price and a context array containing attributes used in your conditions.

use PhpJunior\PricingEngine\Facades\PricingEngine;

$result = PricingEngine::calculatePrice(
    basePrice: 200,
    context: [
        'customer_group' => 'vip',
        'price_total' => 200
    ]
);

// $result will contain:
// [
//     'original_price' => 200,
//     'final_price' => 180,
//     'applied_rules' => [ ... ]
// ]

Location-based pricing example:

use PhpJunior\PricingEngine\Facades\PricingEngine;
use PhpJunior\PricingEngine\Data\ConditionData;
use PhpJunior\PricingEngine\Data\ActionData;

PricingEngine::make()->savePricingRule(
    name: 'Location Based Discount',
    priority: 1,
    conditions: [
        new ConditionData(
            attribute: 'location',
            operator: 'location_in',
            value: ['US', 'CA']
        )
    ],
    actions: [
        new ActionData(
            type: 'fixed_discount',
            value: 20
        )
    ]
);

$result = PricingEngine::calculatePrice(
    basePrice: 150,
    context: [
        'location' => 'ip_address_here'
    ]
);

// $result will contain:
// [
//     'original_price' => 150,
//     'final_price' => 130,
//     'applied_rules' => [ ... ]
// ]

for location-based conditions, used this package stevebauman/location to resolve IP addresses to locations. get more info here

Available Operators

  • =
  • !=
  • >
  • >=
  • <
  • <=
  • in
  • not_in
  • after (for dates)
  • before (for dates)
  • date_equals
  • date_between
  • time_between
  • day_of_week
  • location_in
  • location_not_in
  • contains
  • not_contains

For contains and not_contains, both the attribute value and condition value can be arrays or strings.

Available Actions

  • percentage_discount
  • fixed_discount
  • percentage_markup
  • fixed_markup

Adding Custom Operators and Actions

You can create custom operators and actions by implementing the respective interfaces and registering them in the configuration file.

use PhpJunior\PricingEngine\Operators\OperatorInterface;

class CustomOperator implements OperatorInterface
{
    public function evaluate($attributeValue, $conditionValue): bool
    {
        // Custom logic here
    }
}

use PhpJunior\PricingEngine\Actions\ActionInterface;
class CustomAction implements ActionInterface
{
    public function execute(...$parameters): mixed
    {
        // Custom logic here
        // $parameters[0] - base price
        // $parameters[1] - action value
        // $parameters[2] - context array
    }
}

Register your custom classes in config/pricing-engine.php:

'operators' => [
    'custom_operator' => App\Operators\CustomOperator::class,
],
'actions' => [
    'custom_action' => App\Actions\CustomAction::class,
],

Then you can use them in your pricing rules.


use PhpJunior\PricingEngine\Facades\PricingEngine;
use PhpJunior\PricingEngine\Data\ConditionData;
use PhpJunior\PricingEngine\Data\ActionData;

PricingEngine::make()->savePricingRule(
    name: 'Custom Rule',
    priority: 2,
    conditions: [
        new ConditionData(
            attribute: 'custom_attribute',
            operator: 'custom_operator',
            value: 'some_value'
        )
    ],
    actions: [
        new ActionData(
            type: 'custom_action',
            value: 15
        )
    ]
);

Testing

composer test

Credit

  • Steve Bauman for his location package which is used for location-based pricing rules.

License

The MIT License (MIT). Please see License File for more information.

Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
jayeshmepani/jpl-moshier-ephemeris-php
elnasnato/laraliveui
labrodev/rest-sdk
sampaui/sampaui
babelqueue/php-sdk
facebook/capi-param-builder-php
babelqueue/symfony
hamzi/corewatch
minionfactory/raw-hydrator
hexters/coinpayment
rjcodes/rjcms
act-training/laravel-permissions-manager
alimarchal/laravel-chart-of-accounts
babenkoivan/elastic-scout-driver
mkwebdesign/filament-watchdog-v5
renatomarinho/laravel-page-speed
zedmagdy/filament-business-hours
renatovdemoura/blade-elements-ui
devgeek/beacon-admin
benjamin-rqt/data-watcher-bundle