BLOG

Mukesh Kumar Author's Perspective
3 Minute read

Manage promotions with ease, using Sitecore Experience Commerce

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 so much more.

How does it 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 predefined conditions in Sitecore Commerce.

Elements of Promotions

Given below are elements of promotions:

  • Promotion book: It is a collection of promotions that can apply to sellable items that are 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 predefined 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 the 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. Add the Sitecore.Commerce.Plugins.Orders, Sitecore.Commerce.Plugins.Rule and Sitecore.Commerce.Plugin.Carts references to the project for the Nuget Package Reference.
  3. Create a folder 'Conditions in the Project' and place the below code:
  4. 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;

    }

    }

    }

    }

  5. Now we have a custom condition - next, let’s create a custom benefit, which will apply to the promotion to fulfill the business requirement.
  6. Create a Benefit folder in the Project and Place the below code snippet:
  7. 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));

    });

    }

    }

    }

  8. Now ‘Add Rule’ project reference to the Commerce Engine. Stop the IIS and deploy the New Dll to the Engine and restart the website.
  9. Now it’s time to configure it in Sitecore. We need to configure both Conditions and Benefits separately in Sitecore.
  10. Go to the following path to configure the condition:
  11. /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 like below:

  12. Go to the following path to configure the action:
  13. /sitecore/Commerce/Commerce Control Panel/Commerce Engine Settings/Commerce Terms/BusinessTools/Actions

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

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

The bottomline

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

Natesh G
Mukesh Kumar Associate Technical Lead


Talk to our Experts

Talk to us about how we bring together 1:1 personalisation, deep Martech Expertise, CX & Demand Gen Strategy, Engagement Analytics & Cross-Channel Orchestration to drive award winning experiences that convert

Get in touch for a complimentary consultation or a demo today.

Expert Workshops

Free workshops, expert advice & demos- to help your realize value with Sitecore

Register

Session Presentations

  • Sitecore + SFMC= Marketing Success
  • Transforming The Future Of eCommerce
Meet Us

Giveaways:

Participate in our event survey , meet us at our booth , get free giveaways & a chance to win an iPhone 11

Let’s go