Lunar provides currency-aware price formatting and display for storefront use.Documentation Index
Fetch the complete documentation index at: https://docs.lunarphp.com/llms.txt
Use this file to discover all available pages before exploring further.
Overview
When displaying prices on a storefront, it is important to show the correct format relative to the currency the customer is purchasing in. Every storefront is different. Lunar provides a default price formatter that suits most use cases, while also making it straightforward to swap in a custom implementation for stores with specific formatting requirements.The Price model
| Field | Type | Description |
|---|---|---|
id | bigIncrements | Primary key |
customer_group_id | foreignId nullable | Associated customer group for tiered pricing |
currency_id | foreignId | Associated currency |
priceable_type | string | Morph type for the parent model |
priceable_id | unsignedBigInteger | Morph ID for the parent model |
price | unsignedBigInteger | Price value stored in the smallest currency unit (e.g. cents) |
compare_price | unsignedBigInteger nullable | Comparison/original price for displaying discounts |
min_quantity | integer | Minimum quantity required for this price tier (default: 1) |
created_at | timestamp | |
updated_at | timestamp |
price and compare_price fields are automatically cast to Lunar\DataTypes\Price objects when accessed.
Relationships
| Relationship | Type | Related Model | Description |
|---|---|---|---|
priceable | MorphTo | Various | The model this price belongs to (e.g. ProductVariant) |
currency | BelongsTo | Lunar\Models\Currency | The currency for this price |
customerGroup | BelongsTo | Lunar\Models\CustomerGroup | Optional customer group for group-specific pricing |
Tax helper methods
TheLunar\Models\Price model provides methods for retrieving prices with or without tax applied, based on the lunar.pricing.stored_inclusive_of_tax configuration value.
The Price data type
TheLunar\DataTypes\Price class is used throughout Lunar whenever a price value needs formatting. It is not limited to the Lunar\Models\Price model. The following models also have attributes that return Lunar\DataTypes\Price instances:
Lunar\Models\Order
sub_totaldiscount_totalshipping_totaltax_totaltotal
Lunar\Models\OrderLine
unit_pricesub_totaldiscount_totaltax_totaltotal
Lunar\Models\Transaction
amount
Price formatting
The class responsible for price formatting is configured in theconfig/lunar/pricing.php file:
DefaultPriceFormatter
TheLunar\Pricing\DefaultPriceFormatter ships with Lunar and handles most use cases for formatting a price.
To demonstrate, start by creating a standard price model:
Raw value
Return the raw integer value as stored in the database:Decimal value
Return the decimal representation of the price. The decimal value accounts for the number of decimal places configured on the currency. For example, if the currency has 2 decimal places:unitDecimal method factors in the unit_quantity of the purchasable model. Consider the following:
unit_quantity to 10, Lunar is told that 10 individual units make up this product at this price point. This is useful for items where a single unit would cost less than the smallest currency denomination (e.g. 0.001 EUR).
unitDecimal method divides by the unit quantity, giving the per-unit cost of 0.01.
Formatted currency string
The formatted price uses the native PHP NumberFormatter. A locale and formatting style can be specified:Full method reference
Creating a custom formatter
A custom formatter must implementLunar\Pricing\PriceFormatterInterface and accept $value, $currency, and $unitQty as constructor parameters.
DefaultPriceFormatter.
Once implemented, register the custom formatter in config/lunar/pricing.php:
Model casting
For custom models that need price formatting, Lunar provides a cast class. The only requirement is that the column stores aninteger value.
The
Lunar\Base\Casts\Price cast resolves the currency from the model’s currency relationship. If no currency relationship exists, it falls back to the default currency.