Skip to content

Open-source eCommerce platform for C# and ASP.NET Core 10 with MongoDB. Build multi-store shops and multi-vendor marketplaces with B2B/B2C, REST API, plugins, Vue 3 and Docker.

1.8 KB
Raw Blame
@using System.Globalization
@using Grand.Business.Core.Extensions
@using Grand.Business.Core.Interfaces.Common.Directory
@inject ICurrencyService currencyService
@inject IContextAccessor contextAccessor

@{
    var availableCurrencies = (await currencyService.GetAllCurrencies(storeId: contextAccessor.StoreContext.CurrentStore.Id))
        .Select(x =>
        {
            //currency char
            var currencySymbol = !string.IsNullOrEmpty(x.DisplayLocale) ? new RegionInfo(x.DisplayLocale).CurrencySymbol : x.CurrencyCode;

            //model
            var currencyModel = new CurrencyModel
            {
                Id = x.Id,
                        Name = x.GetTranslation(y => y.Name, contextAccessor.WorkContext.WorkingLanguage.Id),
                CurrencyCode = x.CurrencyCode,
                CurrencySymbol = currencySymbol
            };
            return currencyModel;
        }).ToList();

    if (availableCurrencies.Count > 1)
    {
        var returnUrl = Context?.Request?.GetEncodedPathAndQuery();
        var currencies = availableCurrencies.Select(x => new
        {
            x.Name,
            Text = x.CurrencyCode,
            Value = Url.RouteUrl("ChangeCurrency", new { currencyCode = x.CurrencyCode, returnUrl }),
            Selected = x.Id.Equals(contextAccessor.WorkContext.WorkingCurrency.Id)
        });
        <div class="dropdown btn-group me-2">
        <button type="button" class="btn btn-secondary dropdown-toggle" data-bs-toggle="dropdown" aria-expanded="false">@currencies.FirstOrDefault(x => x.Selected)?.Text</button>
        <ul class="dropdown-menu">
            @foreach (var item in currencies)
            {
                var className = item.Selected ? "active" : "";
                <li><a class="dropdown-item @className" href="@item.Value">@item.Text</a></li>
            }
        </ul>
    </div>
    }
}