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:

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);
            }

 Looking for quality nopCommerce Web Hosting? Look no further than Arvixe Web Hosting!

Tags: , , , , , | Posted under nopCommerce | RSS 2.0

Author Spotlight

Lavish Kumar

Founder, developer and owner of StrivingProgrammers.com - By profession, he is a software programmer / web developer, web designer & DBA. He is passionate about technology and electronic gadgets. He enjoys writing tech articles and developing web applications. He is a nopCommerce MVP and author of official nopCommerce Designer's Guide.

Leave a Reply

Your email address will not be published. Required fields are marked *