Webiant Logo Webiant Logo
  1. No results found.

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

TierPriceExtensions.cs

using Nop.Core.Domain.Catalog;
using Nop.Core.Domain.Stores;

namespace Nop.Services.Catalog;

/// 
/// Tier price extensions
/// 
public static class TierPriceExtensions
{
    /// 
    /// Filter tier prices by a store
    /// 
    /// Tier prices
    /// Store reference
    /// Filtered tier prices
    public static IEnumerable FilterByStore(this IEnumerable source, Store store)
    {
        ArgumentNullException.ThrowIfNull(source);

        ArgumentNullException.ThrowIfNull(store);

        return source.Where(tierPrice => tierPrice.StoreId == 0 || tierPrice.StoreId == store.Id);
    }

    /// 
    /// Filter tier prices by customer roles
    /// 
    /// Tier prices
    /// Customer role identifiers
    /// Filtered tier prices
    public static IEnumerable FilterByCustomerRole(this IEnumerable source, int[] customerRoleIds)
    {
        ArgumentNullException.ThrowIfNull(source);

        ArgumentNullException.ThrowIfNull(customerRoleIds);

        if (!customerRoleIds.Any())
            return source;

        return source.Where(tierPrice =>
            !tierPrice.CustomerRoleId.HasValue || tierPrice.CustomerRoleId == 0 || customerRoleIds.Contains(tierPrice.CustomerRoleId.Value));
    }

    /// 
    /// Remove duplicated quantities (leave only an tier price with minimum price)
    /// 
    /// Tier prices
    /// Filtered tier prices
    public static IEnumerable RemoveDuplicatedQuantities(this IEnumerable source)
    {
        ArgumentNullException.ThrowIfNull(source);

        var tierPrices = source.ToList();

        //get group of tier prices with the same quantity
        var tierPricesWithDuplicates = tierPrices.GroupBy(tierPrice => tierPrice.Quantity).Where(group => group.Count() > 1);

        //get tier prices with higher prices 
        var duplicatedPrices = tierPricesWithDuplicates.SelectMany(group =>
        {
            //find minimal price for quantity
            var minTierPrice = group.Aggregate((currentMinTierPrice, nextTierPrice) =>
                (currentMinTierPrice.Price < nextTierPrice.Price ? currentMinTierPrice : nextTierPrice));

            //and return all other with higher price
            return group.Where(tierPrice => tierPrice.Id != minTierPrice.Id);
        });

        //return tier prices without duplicates
        return tierPrices.Where(tierPrice => duplicatedPrices.All(duplicatedPrice => duplicatedPrice.Id != tierPrice.Id));
    }

    /// 
    /// Filter tier prices by date
    /// 
    /// Tier prices
    /// Date in UTC; pass null to filter by current date
    /// Filtered tier prices
    public static IEnumerable FilterByDate(this IEnumerable source, DateTime? date = null)
    {
        ArgumentNullException.ThrowIfNull(source);

        if (!date.HasValue)
            date = DateTime.UtcNow;

        return source.Where(tierPrice =>
            (!tierPrice.StartDateTimeUtc.HasValue || tierPrice.StartDateTimeUtc.Value < date) &&
            (!tierPrice.EndDateTimeUtc.HasValue || tierPrice.EndDateTimeUtc.Value > date));
    }
}