php-junior/pricing-engine 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

php-junior/pricing-engine

最新稳定版本:v0.0.4

Composer 安装命令:

composer require php-junior/pricing-engine

包简介

A dynamic pricing rule engine for Laravel.

README 文档

README

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.

统计信息

  • 总下载量: 1
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-12-18