Merx

Currencies

Merx is very flexible when it comes to displaying different prices in different currencies. You can write your own rules (using PHP) to determine when and how each price is displayed.

Currencies are defined in the pricingRules option. Each pricing rule is defined by a key and an array with following items.

Options per pricing rule

Name Type Default Description
name string or function Key of the rule
currency string 'EUR' Three-letter ISO currency code
rule function true Function returning true or false. You can access the $kirby object by loading it as an argument.
taxIncluded bool true Whether price is including tax or not

Set up pricing rules

Change default currency

If you want to change the default currency (e.g. for a single-language website), you can set up a simple default pricing rule.

/site/config/config.php
<?php

return [
    'wagnerwagner.merx.pricingRules' => [
        'default' => [
            'currency' => 'GBP',
        ],
    ],
];

Currencies based on language

If you want to display prices based on the Kirby language, you can do so by using the $kirby->languageCode() method.

/site/config/config.php
<?php

return [
    'wagnerwagner.merx.pricingRules' => [
        'us' => [
            'name' => 'US dollar',
            'currency' => 'USD',
            'rule' => fn (?\Kirby\Cms\App $kirby): bool => $kirby->languageCode() === 'en',
            'taxIncluded' => false,
        ],
        'default' => [
            'name' => 'Euro',
            'currency' => 'EUR',
        ],
    ],
];

Pricing rule based on weekday

If you want to show a reduced price on Mondays, you can do this as follows.

/site/config/config.php
<?php

return [
    'wagnerwagner.merx.pricingRules' => [
        'reduced' => [
            'name' => 'Reduced',
            'currency' => 'EUR',
            'rule' => fn (): bool => date('N') === '1', // returns true on mondays
        ],
        'default' => [
            'name' => 'Default',
            'currency' => 'EUR',
        ],
    ],
];

When no rule function is defined, the rule is considered as true.

Merx checks the rules in the order you define them and uses the first one that returns true. The default rule is always checked last, so it’s the fallback wherever you put it.

Add prices for products

A product has one price per pricing rule. The price field of the product blueprint is a single number with one rule, and has a row per rule with several. Each input says whether its price includes tax, as set by the rule’s taxIncluded. The field stores the prices as a list.

/content/products/1_bike/product.en.txt
Title: Bike

----

Price:

-
  price: 2000
  pricingrule: default
-
  price: 2100
  pricingrule: us

A plain number is the price of the default rule, so a shop with one currency can write Price: 2000.

A product without a price for a rule has no price for the visitors that rule applies to. Merx does not fall back to the default price, which would show them a price in another currency.

In a product blueprint of your own, add the field with type: prices.

Showing the price

The price() method of a product page returns the price based on the first valid pricing rule. The price is formatted automatically with the correct currency symbol (see $price->toString()).

/site/templates/product.php
<?= $page->price() ?>

You can provide the key of the rule as the first argument to the price method, if you want to return the price of a specific rule.

/site/templates/product.php
<?= $page->price('default') ?> or <?= $page->price('us') ?>

Merx::pricingRule() returns the current pricing rule. You can use it to check if your rules work as expected.


Continue learning how to define and use tax rules.