Skip to content

Instantly share code, notes, and snippets.

@websterian
Created November 20, 2017 18:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save websterian/e9e7a70b843781b06d57588c704dc85d to your computer and use it in GitHub Desktop.
Save websterian/e9e7a70b843781b06d57588c704dc85d to your computer and use it in GitHub Desktop.
All items with a specific tag set price
namespace Plugin.Pricing.Action
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using Sitecore.Commerce.Core;
using Sitecore.Commerce.Plugin.Carts;
using Sitecore.Commerce.Plugin.Pricing;
using Sitecore.Framework.Rules;
[EntityIdentifier("All items with a specific tag set price")]
public class CartAllItemsWithTagSpecifyAmountAction : ICartLineAction
{
public void Execute(IRuleExecutionContext context)
{
var commerceContext = context.Fact<CommerceContext>();
if (commerceContext == null)
{
return;
}
var cart = (commerceContext.Objects.OfType<Cart>()).FirstOrDefault();
var totals = (commerceContext.Objects.OfType<CartTotals>()).FirstOrDefault();
if ((cart != null) && cart.Lines.Any() && ((totals != null) && totals.Lines.Any()) == false)
{
return;
}
var source = new List<CartLineComponent>();
foreach (var cartLine in cart.Lines.Where(x => x.HasComponent<CartProductComponent>()))
{
var firstOrDefault = cartLine.GetComponent<CartProductComponent>().Tags.FirstOrDefault(t => t.Name == this.Tag.Yield(context));
if (!string.IsNullOrEmpty(firstOrDefault?.Name))
{
source.Add(cartLine);
}
}
if (!source.Any())
{
return;
}
var model = commerceContext.Objects.OfType<PropertiesModel>().FirstOrDefault();
if (model == null)
{
return;
}
foreach (var line in source)
{
if (!totals.Lines.ContainsKey(line.Id))
{
continue;
}
var discount = commerceContext.GetPolicy<KnownCartAdjustmentTypesPolicy>().Discount;
var d = this.Price.Yield(context);
if (commerceContext.GetPolicy<GlobalPricingPolicy>().ShouldRoundPriceCalc)
{
d = decimal.Round(d, commerceContext.GetPolicy<GlobalPricingPolicy>().RoundDigits, commerceContext.GetPolicy<GlobalPricingPolicy>().MidPointRoundUp ? MidpointRounding.AwayFromZero : MidpointRounding.ToEven);
}
decimal amount;
var currentAmount = totals.Lines[line.Id].SubTotal.Amount;
if (currentAmount <= d)
{
amount = d - currentAmount;
totals.Lines[line.Id].SubTotal.Amount += amount;
}
else
{
amount = currentAmount - d;
amount = amount * decimal.MinusOne;
totals.Lines[line.Id].SubTotal.Amount += amount;
}
var item = new CartLineLevelAwardedAdjustment
{
Name = (string) model.GetPropertyValue("PromotionText"),
DisplayName = (string) model.GetPropertyValue("PromotionCartText"),
Adjustment = new Money(commerceContext.CurrentCurrency(), amount),
AdjustmentType = discount,
IsTaxable = false,
AwardingBlock = "CartAllItemsWithTagSpecifyAmountAction"
};
line.Adjustments.Add(item);
line.GetComponent<MessagesComponent>().AddMessage(commerceContext.GetPolicy<KnownMessageCodePolicy>().Promotions, $"PromotionApplied: {model.GetPropertyValue("PromotionId")}");
}
}
public IRuleValue<decimal> Price { get; set; }
public IRuleValue<string> Tag { get; set; }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment