Version: nopCommerce 3.60
nopCommerce offers a scheduled task in the administration section where store admin can run this task to update the exchange rates.
Go to: Administration > System > Schedule Tasks
Here is the screenshot:
So, there is a catch in this scheduled task. If any store owner would like to update the exchange rates immediately by scheduled task's "Run Now" button, it can only be done once per hour because of the one hour condition specified in the code.
Let's take a closer look at the code!
Go to: src/Libraries/Nop.Services/Directory/UpdateExchangeRateTask.cs
Find this:
/// <
summary
>
/// Executes a task
/// </
summary
>
public void Execute()
{
if (!_currencySettings.AutoUpdateEnabled)
return;
long lastUpdateTimeTicks = _currencySettings.LastUpdateTime;
DateTime lastUpdateTime = DateTime.FromBinary(lastUpdateTimeTicks);
lastUpdateTime = DateTime.SpecifyKind(lastUpdateTime, DateTimeKind.Utc);
if (lastUpdateTime.AddHours(1) < DateTime.UtcNow)
{
//update rates each one hour
var exchangeRates = _currencyService.GetCurrencyLiveRates(_currencyService.GetCurrencyById(_currencySettings.PrimaryExchangeRateCurrencyId).CurrencyCode);
foreach (var exchageRate in exchangeRates)
{
var currency = _currencyService.GetCurrencyByCode(exchageRate.CurrencyCode);
if (currency != null)
{
currency.Rate = exchageRate.Rate;
currency.UpdatedOnUtc = DateTime.UtcNow;
_currencyService.UpdateCurrency(currency);
}
}
//save new update time value
_currencySettings.LastUpdateTime = DateTime.UtcNow.ToBinary();
_settingService.SaveSetting(_currencySettings);
}
}
This line of code is adding the 1 hour condition:
if (lastUpdateTime.AddHours(1) < DateTime.UtcNow)
Now, how to bypass it? Well, the solution is quite simple! We just need to comment out the time check IF condition and rest of the code will take care of everything.
You should comment out the condition like this:
long lastUpdateTimeTicks = _currencySettings.LastUpdateTime;
DateTime lastUpdateTime = DateTime.FromBinary(lastUpdateTimeTicks);
lastUpdateTime = DateTime.SpecifyKind(lastUpdateTime, DateTimeKind.Utc);
// Commenting out the one hour condition
//if (lastUpdateTime.AddHours(1) < DateTime.UtcNow)
{
//update rates each one hour
var exchangeRates = _currencyService.GetCurrencyLiveRates(_currencyService.GetCurrencyById(_currencySettings.PrimaryExchangeRateCurrencyId).CurrencyCode);
foreach (var exchageRate in exchangeRates)
{
var currency = _currencyService.GetCurrencyByCode(exchageRate.CurrencyCode);
if (currency != null)
{
currency.Rate = exchageRate.Rate;
currency.UpdatedOnUtc = DateTime.UtcNow;
_currencyService.UpdateCurrency(currency);
}
}
//save new update time value
_currencySettings.LastUpdateTime = DateTime.UtcNow.ToBinary();
_settingService.SaveSetting(_currencySettings);
}
A copy of this article is also available on Arvixe Blog.