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.
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
The configuration file config/pricing-engine.php allows you to customize:
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();
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
=!=>>=<<=innot_inafter (for dates)before (for dates)date_equalsdate_betweentime_betweenday_of_weeklocation_inlocation_not_incontainsnot_containsFor contains and not_contains, both the attribute value and condition value can be arrays or strings.
percentage_discountfixed_discountpercentage_markupfixed_markupYou 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
)
]
);
composer test
The MIT License (MIT). Please see License File for more information.
How can I help you explore Laravel packages today?