Currencies

Currencies

How do I change the store currency?

In order to change the store currency, publish the store configuration file:

php artisan vendor:publish --tag aero::store-config

Locate the configurations section of the published config/aero/store.php file and update the currency_code value of the active store configuration.

Ensure the currency_codeexists in the currenciesdatabase table of the store.

What is a fascia currency and how do I use it?

A fascia currency can be set to render a different currency to the one specified in the store configuration.

Setting the fascia currency

The easiest way to check for and apply the fascia currency is through storemiddleware. The example snippet below details how to check for the presence of a currencycookie passed with the request.

<?php

namespace App\Http\Middleware;

use Aero\Common\Models\Currency;
use Aero\Store\Http\Middleware\EncryptCookies;

class SetFasciaCurrency
{
    public function handle($request, $next): void 
    {
        EncryptCookies::$exceptions[] = 'currency';

        if ($code = $request->cookie('currency')) {
            $currency = Currency::find($code);

            $request->store()->setFasciaCurrency($currency);
        }

        return $next($request);
    }
}

Ensure the currency code exists in the currenciesdatabase table of the store.

The middleware class can then be pushed to the storemiddleware group within the registermethod of a service provider:

<?php

namespace App\Providers;

use App\Http\Middleware\SetFasciaCurrency;
use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
{
    public function register(): void 
    {
        $this->app['router']->pushMiddlewareToGroup('store', SetFasciaCurrency::class);
    }
}

If the fascia currency needs to be set when running a console command:

if ($this->app->runningInConsole() && ($currency = env('CURRENCY_CODE'))) {
    $this->app->booted(function () use ($currency) {
        Store::setFasciaCurrency(Currency::find($currency));
    });
}

Additional considerations