public virtual IEnumerable <Provider <IShippingRateComputationMethod> > LoadActiveShippingRateComputationMethods(int storeId = 0, string systemName = null)
        {
            var allMethods = _providerManager.GetAllProviders <IShippingRateComputationMethod>(storeId);

            // Get active shipping rate computation methods
            var activeMethods = allMethods
                                .Where(p => p.Value.IsActive && _shippingSettings.ActiveShippingRateComputationMethodSystemNames
                                       .Contains(p.Metadata.SystemName, StringComparer.InvariantCultureIgnoreCase));

            if (activeMethods.Any())
            {
                return(activeMethods);
            }

            // Try get a fallback shipping rate computation method
            var fallbackMethod = allMethods.FirstOrDefault(x => x.IsShippingRateComputationMethodActive(_shippingSettings)) ?? allMethods.FirstOrDefault();

            if (fallbackMethod != null)
            {
                _shippingSettings.ActiveShippingRateComputationMethodSystemNames.Clear();
                _shippingSettings.ActiveShippingRateComputationMethodSystemNames.Add(fallbackMethod.Metadata.SystemName);
                _settingFactory.SaveSettingsAsync(_shippingSettings).Await();

                return(new Provider <IShippingRateComputationMethod>[] { fallbackMethod });
            }

            if (DataSettings.DatabaseIsInstalled())
            {
                throw new SmartException(T("Shipping.OneActiveMethodProviderRequired"));
            }

            return(activeMethods);
        }
Example #2
0
        protected override async Task <HookResult> OnDeletingAsync(BaseEntity entity, IHookedEntity entry, CancellationToken cancelToken)
        {
            if (entity is not Language language)
            {
                return(HookResult.Void);
            }

            // Update default admin area language (if required)
            var localizationSettings = _localizationSettings.Value;

            if (localizationSettings.DefaultAdminLanguageId == language.Id)
            {
                var allLanguages = await GetAllLanguagesAsync();

                foreach (var activeLanguage in allLanguages)
                {
                    if (activeLanguage.Id != language.Id)
                    {
                        localizationSettings.DefaultAdminLanguageId = activeLanguage.Id;
                        await _settingFactory.SaveSettingsAsync(localizationSettings);

                        break;
                    }
                }
            }

            return(HookResult.Ok);
        }