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

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

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

php-junior/pricing-engine

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.

php-junior/pricing-engine 适用场景与选型建议

php-junior/pricing-engine 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 2 次下载、GitHub Stars 达 9, 最近一次更新时间为 2025 年 12 月 18 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「ecommerce」 「laravel」 「shopping-cart」 「laravel-package」 「php-junior」 「pricing-rules」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 php-junior/pricing-engine 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 php-junior/pricing-engine 我们能提供哪些服务?
定制开发 / 二次开发

基于 php-junior/pricing-engine 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

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

GitHub 信息

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

其他信息

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