How to bypass UpdateExchangeRateTask one hour condition in nopCommerce

If you are a nopCommerce developer and using update currency exchange rates scheduled task in the administration section, you might be aware of the fact that there is a one hour condition in the code. For those who are not familiar, let's me give you an overview:

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:

nopcommerce exchange rate


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


nopcommerce exchange rates


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.

About Author

Written By Lavish Kumar

Based out of New York, USA, Lavish Kumar is a full stack web developer by profession and founder of Striving Programmers, a trusted community for developers that offers a wealth of articles and forums to assist individuals with improving their software development skills.

Leave your comment