Created
December 21, 2016 10:46
-
-
Save khurramkhang/b9563a37820f831eb8ba58e156c597d2 to your computer and use it in GitHub Desktop.
Custom Line Item Validator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Linq; | |
using EPiServer; | |
using EPiServer.Commerce.Catalog.ContentTypes; | |
using EPiServer.Commerce.Order; | |
using EPiServer.Commerce.Validation; | |
using Mediachase.Commerce; | |
using Mediachase.Commerce.Catalog; | |
using Mediachase.Commerce.Catalog.Dto; | |
namespace CommerceExtensions.LineItem | |
{ | |
public class AddonValidation : ILineItemValidator | |
{ | |
private readonly IContentLoader _contentLoader; | |
private readonly ReferenceConverter _referenceConverter; | |
private readonly ICatalogSystem _catalogSystem; | |
private readonly IValidateAddOn _validateAddOn; | |
public AddonValidation(IContentLoader contentLoader, ReferenceConverter referenceConverter, ICatalogSystem catalogSystem, IValidateAddOn validateAddOn) | |
{ | |
_contentLoader = contentLoader; | |
_referenceConverter = referenceConverter; | |
_catalogSystem = catalogSystem; | |
_validateAddOn = validateAddOn; | |
} | |
public bool Validate(ILineItem lineItem, IMarket market, Action<ILineItem, ValidationIssue> onValidationError) | |
{ | |
ParameterValidator.ThrowIfNull<ILineItem>(() => lineItem, lineItem); | |
ParameterValidator.ThrowIfNull<IMarket>(() => market, market); | |
EntryContentBase entryContent = lineItem.GetEntryContent(this._referenceConverter, this._contentLoader); | |
if (entryContent == null) | |
{ | |
onValidationError(lineItem, ValidationIssue.RemovedDueToCodeMissing); | |
return false; | |
} | |
// This item cannot be purchased alone as it is an AddOn | |
if (!this._validateAddOn.Validate(lineItem)) | |
{ | |
onValidationError(lineItem, ValidationIssue.RemovedDueToUnavailableItem); | |
return false; | |
} | |
if (!this.IsValidMarket(entryContent, market)) | |
{ | |
onValidationError(lineItem, ValidationIssue.RemovedDueToNotAvailableInMarket); | |
return false; | |
} | |
if (!this.IsValidMarket(entryContent, market)) | |
{ | |
onValidationError(lineItem, ValidationIssue.RemovedDueToNotAvailableInMarket); | |
return false; | |
} | |
DateTime utcNow = DateTime.UtcNow; | |
if (!this.IsValidCatalog(entryContent, utcNow)) | |
{ | |
onValidationError(lineItem, ValidationIssue.RemovedDueToUnavailableCatalog); | |
return false; | |
} | |
if (!this.IsValidEntry(entryContent, utcNow)) | |
{ | |
onValidationError(lineItem, ValidationIssue.RemovedDueToUnavailableItem); | |
return false; | |
} | |
return true; | |
} | |
protected virtual bool IsValidMarket(EntryContentBase content, IMarket market) | |
{ | |
return !content.MarketFilter.Any((string excludedMarket) => market.MarketId.Equals(new MarketId(excludedMarket))); | |
} | |
protected virtual bool IsValidCatalog(EntryContentBase entry, DateTime requestDate) | |
{ | |
CatalogDto.CatalogRow catalogRow = this._catalogSystem.GetCatalogDto(entry.CatalogId).Catalog.FirstOrDefault<CatalogDto.CatalogRow>(); | |
return catalogRow != null && catalogRow.IsActive && requestDate >= catalogRow.StartDate && requestDate <= catalogRow.EndDate; | |
} | |
protected virtual bool IsValidEntry(EntryContentBase entry, DateTime requestDate) | |
{ | |
DateTime t = (entry.StartPublish ?? DateTime.MinValue).ToUniversalTime(); | |
DateTime t2 = (entry.StopPublish ?? DateTime.MaxValue).ToUniversalTime(); | |
return !entry.IsPendingPublish && requestDate >= t && requestDate <= t2; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using EPiServer.Commerce.Order; | |
namespace CommerceExtensions.LineItem | |
{ | |
public interface IValidateAddOn | |
{ | |
bool Validate(ILineItem lineItem); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using EPiServer.Commerce.Order; | |
namespace CommerceExtensions.LineItem | |
{ | |
public class ValidateAddOn : IValidateAddOn | |
{ | |
public bool Validate(ILineItem lineItem) | |
{ | |
return true; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment