定制 responsiv/currency 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

responsiv/currency

Composer 安装命令:

composer require responsiv/currency

包简介

Currency plugin for October CMS

README 文档

README

Tools for dealing with currency display and conversions. You can configure currencies and converters via the Settings page.

  • Settings → Currencies
  • Settings → Exchange Rates

Get Started

A quick start guide for this plugin can be found at the following link:

Official Documentation

This plugin is partially documented in the official October CMS documentation.

Article Purpose
Currency Twig Filter Formatting Currency in Twig markup
Currency Form Widget Displaying currency as an input field
Currency List Column Displaying currency in a list column

Understanding Currency Definitions

There are multiple currency definition types that are important to operating the Currency plugin. Each definition type is described in more detail below.

Default Currency

The default currency is the global anchor — all prices are stored in this currency. It is used when there is no site context or when no other currency is configured. In the currency form widget, if the model does not implement the multisite feature, the value is stored in the default currency.

Note: The default currency is set by opening the Settings → Currencies page and checking the Default checkbox on a currency listed on this page.

Base Currency (Site Group)

The base currency can be set on a Site Group to override the stored currency for all sites in that group. This is an edge case for installations where different groups of sites store prices in different currencies (e.g. a US group stores in USD while a UK group stores in GBP).

When no base currency is set on the site group, the global default currency is used.

Note: The base currency is set by opening the Settings → Site Groups page and selecting a currency in the Base Currency dropdown.

Site Currency

The site currency defines what currency is displayed to users visiting that site. When set, the currency filter automatically converts from the base currency to the site currency using exchange rates.

The site currency is available in Twig as this.site.currency and this.site.currency_code.

{{ this.site.currency_code }}

For example, if a value is stored in the default currency as USD and the site has a currency of AUD, the site option handles conversion automatically.

{{ product.price|currency({ site: true }) }}

Note: The site currency is set by opening the Settings → Site Definitions page and selecting a currency in the Currency dropdown.

Currencyable Trait

The Currencyable trait allows models to store explicit per-currency price overrides in a sidecar table. It uses attribute interception (similar to the Translatable trait) so the model's $attributes always hold primary currency values. When a non-primary currency is active, reads return the explicit override if one exists, or fall back to exchange-rate conversion automatically.

Setup

Add the trait and define which attributes support currency overrides:

class Product extends Model
{
    use \Responsiv\Currency\Traits\Currencyable;

    public $currencyable = ['price', 'cost'];
}

How It Works

  1. Attribute interception: getAttribute() and setAttribute() are overridden. When the active currency differs from the primary, reads and writes are redirected to the sidecar cache instead of $attributes.
  2. Exchange-rate fallback: When no explicit override exists for a currency, getCurrencyOverride() automatically converts the base value using Currency::convert().
  3. Before save: syncCurrencyableAttributes() persists any dirty sidecar data and restores the original primary-currency values on the model so the main table is never written with override values.
  4. Clearing overrides: Setting a currency override to null or empty deletes the sidecar row, reverting the attribute to automatic exchange-rate conversion.

Storage

Overrides are stored in the responsiv_currency_attributes table:

Column Purpose
model_type Morph type (model class)
model_id Foreign key to the model
currency_code Currency code (e.g. EUR, GBP)
attribute Attribute name (e.g. price)
value Override value

Reading Overrides

// Get override for a specific currency (falls back to exchange-rate conversion)
$eurPrice = $product->getCurrencyOverride('price', 'EUR');

// Get override without fallback (returns null if no override)
$eurPrice = $product->getCurrencyOverride('price', 'EUR', false);

// Check if an explicit override exists (not exchange-rate converted)
if ($product->hasCurrencyOverride('price', 'EUR')) {
    // ...
}

// Get all stored currency values for an attribute
$allPrices = $product->getCurrencyOverrides('price');
// Returns: ['USD' => 1000, 'EUR' => 950, 'GBP' => 800]

// Get the primary-currency value (always reads from $attributes)
$basePrice = $product->getCurrencyableBaseValue('price');

Writing Overrides

// Set a single override
$product->setCurrencyOverride('price', 'EUR', 950);

// Set multiple currencies at once
$product->setCurrencyOverrides('price', [
    'EUR' => 950,
    'GBP' => 800,
]);

// Remove an override (reverts to exchange rate conversion)
$product->forgetCurrencyOverride('price', 'EUR');

// Remove all overrides for an attribute
$product->forgetCurrencyOverrides('price');

// Remove all overrides for a currency
$product->forgetAllCurrencyOverrides('EUR');

Context Switching

// Override the currency context for a model instance
$product->setCurrency('EUR');
echo $product->price; // Returns EUR override or exchange-rate converted value

// Get the active currency code
$code = $product->getCurrency();

Query Scopes

// Filter by currency override value
Product::whereCurrencyOverride('price', 'EUR', 950)->get();

// Eager load overrides for the active currency
Product::withCurrencyOverride()->get();

// Eager load overrides for a specific currency
Product::withCurrencyOverride('EUR')->get();

// Eager load all currency overrides
Product::withCurrencyOverrides()->get();

Currency Resolution

The trait resolves currencies through the CurrencyManager:

Method Returns Purpose
getCurrencyableDefault() Base currency code Site group's base currency, or global default
getCurrencyableContext() Active currency code Site's currency, or falls back to base
shouldConvertCurrency() bool true when active differs from base

Enabling / Disabling

The trait is enabled by default. Override isCurrencyableEnabled() in the model to disable it conditionally:

public function isCurrencyableEnabled()
{
    return false; // Disable currency overrides for this model
}

License

This plugin is an official extension of the October CMS platform and is free to use if you have a platform license. See EULA license for more details.

responsiv/currency 适用场景与选型建议

responsiv/currency 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 9.26k 次下载、GitHub Stars 达 18, 最近一次更新时间为 2016 年 08 月 18 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

围绕 responsiv/currency 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

  • Stars: 18
  • Watchers: 3
  • Forks: 23
  • 开发语言: PHP

其他信息

  • 授权协议: Unknown
  • 更新时间: 2016-08-18