Webiant Logo Webiant Logo
  1. No results found.

    Try your search with a different keyword or use * as a wildcard.

FixedByWeightByTotalEventConsumer.cs

using Nop.Core.Domain.Shipping;
using Nop.Core.Events;
using Nop.Services.Configuration;
using Nop.Services.Events;

namespace Nop.Plugin.Shipping.FixedByWeightByTotal.Infrastructure.Cache;

/// 
/// Event consumer of the "Fixed or by weight" shipping plugin (used for removing unused settings)
/// 
public class FixedByWeightByTotalEventConsumer : IConsumer>
{
    #region Fields

    protected readonly ISettingService _settingService;

    #endregion

    #region Ctor

    public FixedByWeightByTotalEventConsumer(ISettingService settingService)
    {
        _settingService = settingService;
    }

    #endregion

    #region Methods

    /// 
    /// Handle shipping method deleted event
    /// 
    /// Event message
    /// A task that represents the asynchronous operation
    public async Task HandleEventAsync(EntityDeletedEvent eventMessage)
    {
        var shippingMethod = eventMessage?.Entity;
        if (shippingMethod == null)
            return;

        //delete saved fixed rate if exists
        var setting = await _settingService.GetSettingAsync(string.Format(FixedByWeightByTotalDefaults.FIXED_RATE_SETTINGS_KEY, shippingMethod.Id));
        if (setting != null)
            await _settingService.DeleteSettingAsync(setting);
    }

    #endregion
}