Contact Us

For marketers, Sitecore Experience Commerce (XC) serves as a great tool - it helps to build a personalized journey at every step. It is the only platform that can natively merge content, commerce, and data in one, enterprise-level commerce solution. It has impressive features, to say the least.
To begin with, it can craft and control personalized commerce experiences. Also, it can leverage Sitecore’s marketing automation, content management, data, and insights. It can also create storefronts, product pages, carts, checkouts, accounts, and a lot more things! Last but not the least (and significant), it also helps build relationships before, during, and after the transaction. You can test and optimize promotions, user journeys, personalization rules, and much more.

How does Sitecore Experience Commerce help with Promotions

Sitecore Experience has a Promotions dashboard that helps marketers to create and manage promotions for the shopping cart. In a nutshell, it enables business users to craft promotions based on pre-defined conditions in Sitecore Commerce.

Elements of Sitecore Commerce Promotions

Given below are elements of promotions:

  • Promotion book: It is a collection of promotions that be can apply to sellable items contained in multiple catalogs.
  • Promotions: Here, one or more benefits can be applied to the cart, to specific sellable items, and orders.
  • Qualifications: This is a rule that can be applied to a promotion. Also remember, multiple qualifications can be applied to a single promotion. The Commerce Engine makes use of pre-defined filters and rulers to assess which promotion applies.
  • Benefits: These are discounts that are applied to an order, as the result of a promotion, such as free shipping or a percentage of a discount. You can add multiple benefits to a promotion.
  • Public coupons: This is a code that can be used by any customer, and that can be used multiple times on separate orders.
  • Private coupons: This is a code that is assigned to a single customer.

There are mainly two components: Qualifications and Benefits, which help the user to create promotions or coupons that the end-user can utilize during the purchase journey. In some cases, one needs to create custom logic to fulfill business needs.

Let's look at how we can accomplish this:

  1. Add the New Project in the Sitecore Commerce Engine SDK with Name {SolutionName}.Foundation.Rules.
  2. 1 (2)

     

  3. Add the Sitecore.Commerce.Plugins.Orders, Sitecore.Commerce.Plugins.Rule and Sitecore.Commerce.Plugin.Carts references to the project for the Nuget Package Reference.
  4.  

  5. Create a folder 'Conditions in the Project' and place the below code:
  6. using Sitecore.Commerce.Core;

    using Sitecore.Commerce.Plugin.Carts;

    using Sitecore.Framework.Rules;

    namespace AltudoStorefront.Foundation.Rules.Engine.Conditions

    {

    [EntityIdentifier("CustomerHasSpecificDomainCondition")]

    public class CustomerHasSpecifcDomainCondition : ICartsCondition

    {

    public IRuleValue Domain { get; set; }

    public bool Evaluate(IRuleExecutionContext context)

    {

    Cart obj;

    CommerceContext commerceContext = context.Fact(null);

    if (commerceContext != null)

    {

    obj = commerceContext.GetObject();

    }

    else

    {

    obj = null;

    }

    Cart cart = obj;

    if (cart == null || string.IsNullOrEmpty(this.Domain.Yield(context)) || !cart.HasComponent())

    {

    return false;

    }

    if(cart.GetComponent().Email.Contains(this.Domain.Yield(context)))

    {

    return true;

    }

    else

    {

    return false;

    }

    }

    }

    }

  7. Now we have a custom condition, let’s create a custom benefit, which will apply to the promotion to fulfill the business requirement.
  8.  

  9. Create a Benefit folder in Project and Place the below code snippet:
  10.  

    using Sitecore.Commerce.Core;

    using Sitecore.Commerce.Plugin.Carts;

    using Sitecore.Commerce.Plugin.Pricing;

    using Sitecore.Framework.Rules;

    using System;

    using System.Collections.Generic;

    using System.Linq;

    namespace AltudoStorefront.Foundation.Rules.Engine.Benefits

    {

    [EntityIdentifier("PercentOffOnLimitedQuantityCartLineItem")]

    public class PercentOffOnLimitedQuantityCartLineItem : CartTargetItemId, ICartsAction

    {

    public IRuleValue PercentOff { get; set; }

    public IRuleValue LimitedQauntity { get; set; }

    public void Execute(IRuleExecutionContext context)

    {

    Cart cart;

    CartTotals cartTotal;

    CommerceContext commerceContext = context.Fact(null);

    CommerceContext commerceContext1 = commerceContext;

    if (commerceContext1 != null)

    {

    cart = commerceContext1.GetObjects().FirstOrDefault();

    }

    else

    {

    cart = null;

    }

    Cart cart1 = cart;

    CommerceContext commerceContext2 = commerceContext;

    if (commerceContext2 != null)

    {

    cartTotal = commerceContext2.GetObjects().FirstOrDefault();

    }

    else

    {

    cartTotal = null;

    }

    CartTotals amount = cartTotal;

    if (cart1 == null || !cart1.Lines.Any() || amount == null || !amount.Lines.Any<keyvaluepair<string,>>())</keyvaluepair<string,>

    {

    return;

    }

    List list = this.MatchingLines(context).ToList();

    if (!list.Any())

    {

    return;

    }

    list.ForEach((CartLineComponent line) => {

    object obj;

    if (!amount.Lines.ContainsKey(line.Id))

    {

    return;

    }

    var limitedQauntity = LimitedQauntity.Yield(context);

    if (line.Quantity < LimitedQauntity.Yield(context))

    {

    limitedQauntity = line.Quantity;

    }

    PropertiesModel propertiesModel = commerceContext.GetObject ();

    string discount = commerceContext.GetPolicy().Discount;

    decimal unitPrice = amount.Lines[line.Id].SubTotal.Amount / line.Quantity;

    decimal discountEligibleAmount = unitPrice * limitedQauntity;

    decimal num = Convert.ToDecimal((double)this.PercentOff.Yield(context) * 0.01) * discountEligibleAmount;

    if (commerceContext.GetPolicy().ShouldRoundPriceCalc)

    {

    num = decimal.Round(num, commerceContext.GetPolicy().RoundDigits, (commerceContext.GetPolicy().MidPointRoundUp ? MidpointRounding.AwayFromZero : MidpointRounding.ToEven));

    }

    decimal minusOne = num * decimal.MinusOne;

    line.Adjustments.Add(new CartLineLevelAwardedAdjustment()

    {

    Name = (propertiesModel != null ? propertiesModel.GetPropertyValue("PromotionText") : null) as string ?? discount,

    DisplayName = (propertiesModel != null ? propertiesModel.GetPropertyValue("PromotionCartText") : null) as string ?? discount,

    Adjustment = new Money(commerceContext.CurrentCurrency(), minusOne),

    AdjustmentType = discount,

    IsTaxable = false,

    AwardingBlock = "PercentOffLimitedQuantityBenefitAction"

    });

    amount.Lines[line.Id].SubTotal.Amount = amount.Lines[line.Id].SubTotal.Amount + minusOne;

    MessagesComponent component = line.GetComponent();

    string promotions = commerceContext.GetPolicy().Promotions;

    obj = (propertiesModel != null ? propertiesModel.GetPropertyValue("PromotionId") : null);

    if (obj == null)

    {

    obj = "PercentOffLimitedQuantityBenefitAction";

    }

    component.AddMessage(promotions, string.Format("PromotionApplied: {0}", obj));

    });

    }

    }

    }

  11. Now ‘Add Rule’ project reference to the Commerce Engine. Stop the IIS and deploy the New Dll to the Engine and restart the website.
  12.  

  13. Now it’s time to configure it in Sitecore. We need to configure both, Conditions and Benefits separately in Sitecore.
  14.  

  15. Go to the following path to configure the condition:
  16.  

    /sitecore/Commerce/Commerce Control Panel/Commerce Engine Settings/Commerce Terms/BusinessTools/Conditions

    Create an Item name same as the condition entity identifier
    i.e.,CustomerHasSpecificDomainCondition. Assign the value as shown below:

    2 (1) 

  17. Go to the following path to configure the action:
  18.  

    /sitecore/Commerce/Commerce Control Panel/Commerce Engine Settings/Commerce Terms/BusinessTools/Actions

  19. Create an Item name same as the benefit entity identifier
    i.e.,CustomerHasSpecificDomainCondition.Assign the value like below:
  20.  

3 (4) 

That’s it - your custom condition and benefits are ready for use.

 

The Bottomline

All in all, Promotions in Sitecore Experience are beneficial as a business tool and helps to create promotions with much ease to offer personalized commerce experiences to their users.

 
Need Help?