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
namespace Sitecore.Commerce.Plugin.AdventureWorks.Pipelines.Blocks | |
{ | |
using System; | |
using System.Linq; | |
using System.Threading.Tasks; | |
using Sitecore.Commerce.Core; | |
using Sitecore.Commerce.Plugin.AdventureWorks.Components; | |
using Sitecore.Commerce.Plugin.Carts; | |
using Sitecore.Commerce.Plugin.Pricing; | |
using Sitecore.Framework.Pipelines; | |
/// <summary> | |
/// The calculate cart block. | |
/// </summary> | |
public class CalculateCartVIPBlock : PipelineBlock<Cart, Cart, CommercePipelineExecutionContext> | |
{ | |
/// <summary> | |
/// Initializes a new instance of the <see cref="CalculateCartVIPBlock"/> class. | |
/// </summary> | |
/// <param name="persistEntityPersistEntityPipeline"> | |
/// The persist entity persist entity pipeline. | |
/// </param> | |
/// <param name="findEntityPipeline"> | |
/// The find entity pipeline. | |
/// </param> | |
/// <param name="serviceProvider"> | |
/// The service provider. | |
/// </param> | |
public CalculateCartVIPBlock( | |
IPersistEntityPipeline persistEntityPersistEntityPipeline, | |
IFindEntityPipeline findEntityPipeline, | |
IServiceProvider serviceProvider) | |
{ | |
} | |
/// <summary> | |
/// The run. | |
/// </summary> | |
/// <param name="arg"> | |
/// The arg. | |
/// </param> | |
/// <param name="context"> | |
/// The context. | |
/// </param> | |
/// <returns> | |
/// The <see cref="Task"/>. | |
/// </returns> | |
public override Task<Cart> Run(Cart arg, CommercePipelineExecutionContext context) | |
{ | |
/* | |
* This block will be called anytime the cart is recalculated. We can now | |
* retrieve the flag on the cart and if its true do something like call a webservice that contains the VIP discount logic. | |
* You could also have another block on the calculate line if the discount was set at a line level | |
*/ | |
const string AdjustmentType = "VIP"; | |
const string AdjustementName = "VIP Discount"; | |
var vipDiscountApplied = arg.Adjustments.FirstOrDefault(x => x.AdjustmentType == AdjustmentType); | |
if (arg.GetComponent<CartVIPStatusComponent>().ApplyVipPricing == false) | |
{ | |
if (vipDiscountApplied != null) | |
{ | |
arg.Adjustments.Remove(vipDiscountApplied); | |
} | |
return Task.FromResult(arg); | |
} | |
// Do your custom discount calculation here | |
var amount = arg.Totals.SubTotal.Amount * ((decimal)0.20); | |
var adjustment = new Money(context.CommerceContext.CurrentCurrency(), amount * -1); | |
if (vipDiscountApplied != null) | |
{ | |
vipDiscountApplied.Adjustment = adjustment; | |
} | |
else | |
{ | |
// All the setup information should come from a policy or another system | |
var awarededAdjustment = new CartLevelAwardedAdjustment | |
{ | |
Name = AdjustementName, | |
DisplayName = AdjustementName, | |
Adjustment = adjustment, | |
AdjustmentType = AdjustmentType, | |
AwardingBlock = this.Name, | |
IncludeInGrandTotal = true, | |
IsTaxable = false | |
}; | |
arg.Adjustments.Add(awarededAdjustment); | |
} | |
return Task.FromResult(arg); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment