承接 whitecube/laravel-prices 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

whitecube/laravel-prices

Composer 安装命令:

composer require whitecube/laravel-prices

包简介

Manage acquisition, selling & renting prices for products and services in your Laravel Application

README 文档

README

Manage acquisition, selling & renting prices for products and services in your Laravel Application

Installation

composer require whitecube/laravel-prices

Configuration

You can publish the config file by running this command:

php artisan vendor:publish --tag=prices-config 

Once your configuration file is created in config/prices.php, you can edit the Price model to a custom Price model by changing:

return [
    'model' => \App\Models\CustomPriceModel::class,
];

Quick overview

This package lets you attach prices to anything you want, and keeps a history of the price changes overtime.

To achieve that, the package will migrate a prices table which is used to store every price change that occurs on your priceable items (such as products or services). When you access your item's price, the most recent result in that table will be returned.

This documentation will use a fake Product model as an example, however you can link these prices to anything you want.

Usage

Add the HasPrices trait to your Product model.

<?php

use Whitecube\LaravelPrices\HasPrices;

class Product extends Model
{
    use HasPrices;
}

This unlocks the whole functionality of this package for that model. Here are the things you can now do:

Setting a price

$product->price = new Price(amount: 50, currency: 'EUR');

You can pass additional arguments, such as the type (defaults to selling, more on this later) and the moment the price needs to come into effect.

use Whitecube\LaravelPrices\Models\Price;

$product->price = new Price(
    amount: 50, 
    currency: 'EUR', 
    type: 'selling', 
    activated_at: now()->addWeek()
);

or if you want to avoid the use statement

$product->setPrice(
    amount: 50, 
    currency: 'EUR', 
    type: 'selling', 
    activated_at: now()->addWeek()
);

It's important to note that this package uses whitecube/php-prices under the hood. This allows you to later do accurate calculations with your prices, without running into problems with floating point number precision.

This means it converts the price into "minor units" (aka cents) before storage in the database. The value you specify when creating a new price can be either in major or minor units. To define a price directly in minor units, use the minor argument instead of amount:

$product->price = new Price(
    minor: 5000, 
    currency: 'EUR', 
    type: 'selling', 
    activated_at: now()->addWeek()
);

Getting the current selling price

The quickest and easiest way, getting a Whitecube\Price\Price instance so you're ready to do accurate calculations with it.

$price = $product->price;

By default, this will return the latest price that has the selling type, but you can change that by overriding the getDefaultPriceType method on your class. It must return a string that corresponds to the price type you want to use by default for that priceable item class.

Accessing the relationship manually

The above example does a little magic via an accessor method on the trait to make the most common use case easier, but behind the scenes it's a simple query on a relation. You can query this relation yourself when necessary (and with the help of the current() scope, it will only return the currently active price).

$buying_price = $product->prices()->current()->where('type', 'buying')->first();

Do note that this returns an instance of the Whitecube\LaravelPrices\Models\Price model, not a Whitecube\Price\Price instance. To access that manually, call :

$buying_price->toObject();

How to handle one-off prices

The main way to use prices with this package is to use it as a chronological history table of all the prices for a specific item. This means you should define the activated_at attribute when creating the price so that the system can accurately make a decision on whether or not it should consider the price when you query it.

However, you may sometimes need to specify a special one-off price for an item, without it getting applied automatically every time. This can be done easily by leaving out or setting the activated_at attribute to null, and storing the price's ID where you need to have a reference to it. When you do so, these prices will never be returned when using time-based scopes (such as current() or effectiveAt()).

Available scopes

current()

Filters the query to only return the current price model.

$product->prices()->current()->first();

effectiveAt($date)

Filters the query to only return the price model that was active at the given time (accepts a Carbon instance)

$product->prices()->effectiveAt(now()->subWeek())->first();

oneOffs()

Filters the query to only return the one-off prices (activated_at === null)

$product->prices()->oneOffs()->get();

🔥 Sponsorships

If you are reliant on this package in your production applications, consider sponsoring us! It is the best way to help us keep doing what we love to do: making great open source software.

Contributing

Feel free to suggest changes, ask for new features or fix bugs yourself. We're sure there are still a lot of improvements that could be made, and we would be very happy to merge useful pull requests.

Thanks!

Made with ❤️ for open source

At Whitecube we use a lot of open source software as part of our daily work. So when we have an opportunity to give something back, we're super excited!

We hope you will enjoy this small contribution from us and would love to hear from you if you find it useful in your projects. Follow us on Twitter for more updates!

whitecube/laravel-prices 适用场景与选型建议

whitecube/laravel-prices 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 3.51k 次下载、GitHub Stars 达 13, 最近一次更新时间为 2022 年 01 月 17 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

围绕 whitecube/laravel-prices 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

  • Stars: 13
  • Watchers: 2
  • Forks: 3
  • 开发语言: PHP

其他信息

  • 授权协议: Unknown
  • 更新时间: 2022-01-17