I have created a custom condition to apply a promotion. On a valid criteria, the promo is applied to the product in the cart-summary/shopping-cart page.

But when user clicks on continue and moves to the checkout page, it is the original amount of the product that is displayed there. The promo is removed.

When I try creating a promo with the out of box conditions, it works well.

What else needs to be done in case of a custom condition to make the promo persistent.

Here is the code for my custom condition:

namespace Sitecore.Feature.Commerce.Promotions.Engine.Conditions
    using Microsoft.ApplicationBlocks.Data;
    using Sitecore.Commerce.Core;
    using Sitecore.Commerce.Plugin.Carts;
    using Sitecore.Framework.Rules;
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.IO;
    using System.Web.Configuration;

    [EntityIdentifier("Future Event Dates")]
    public class FutureEventDatesCondition : ICartsCondition, ICondition, IMappableRuleEntity
        public IRuleValue<string> FutureEventStartDate { get; set; }
        public IRuleValue<string> FutureEventEndDate { get; set; }

        public bool Evaluate(IRuleExecutionContext context)
            CommerceContext commerceContext = context.Fact<CommerceContext>((string)null);
            bool isPromoApplicable = false;
            string cartID = string.Empty;
            string error = string.Empty;

                if (commerceContext != null)
                    List<Cart> lstCarts = commerceContext.GetObjects<Cart>();

                    if (lstCarts != null && lstCarts.Count > 0)
                        Cart cart = lstCarts[0];
                        cartID = cart != null ? cart.Id : string.Empty;

                        if (!string.IsNullOrEmpty(cartID))
                            DateTime futureEventStartDate = Convert.ToDateTime(FutureEventStartDate.Yield(context));
                            DateTime futureEventEndDate = Convert.ToDateTime(FutureEventEndDate.Yield(context));

                            DateTime checkinDate;
                            DateTime checkoutDate;

                            GetCartDates(commerceContext.CurrentCustomerId(), cartID, out checkinDate, out checkoutDate);
                            isPromoApplicable = (checkinDate != DateTime.MinValue && checkoutDate != DateTime.MinValue &&
                                futureEventStartDate <= checkinDate && checkinDate <= checkoutDate && checkoutDate <= futureEventEndDate);
            catch (Exception ex)
                throw ex

            return isPromoApplicable;

        private void GetCartDates(string userId, string externalCartId, out DateTime checkinDate, out DateTime checkoutDate)
            checkinDate = DateTime.MinValue;
            checkoutDate = DateTime.MinValue;

            if (!string.IsNullOrEmpty(userId) && !string.IsNullOrEmpty(externalCartId))
                string connectionstring = Convert.ToString(WebConfigurationManager.AppSettings["DBCon"]);
                string query = "SELECT * FROM tbl_CartDates (NOLOCK) WHERE userId = '" + userId + "' AND ExternalCartId = '" + externalCartId + "'";
                DataTable dt = SqlHelper.ExecuteDataset(connectionstring, CommandType.Text, query)?.Tables[0];

                if (dt != null && dt.Rows.Count > 0)
                    string startDate = Convert.ToString(dt.Rows[0]["checkinDate"]);
                    string endDate = Convert.ToString(dt.Rows[0]["checkoutDate"]);

                    checkinDate = !string.IsNullOrEmpty(startDate) ? Convert.ToDateTime(startDate) : DateTime.MinValue;
                    checkoutDate = !string.IsNullOrEmpty(endDate) ? Convert.ToDateTime(endDate) : DateTime.MinValue;
                    throw new Exception("CART DATES NOT FOUND");
                throw new Exception("USER ID OR EXTERNAL CART ID IS EMPTY");


1 Answer 1


I have managed to fix this issue. Here is the reason why the promo was not persistent in the checkout page.

The Evaluate method is triggered when the promo code is applied and also on the page load of the checkout page.

I have used commerceContext.CurrentCustomerId() to get the userid. This worked in the summary page but in the checkout, it returned an empty value and hence the issue.

I changed it to commerceContext.CurrentShopperId() instead and now it's working fine.

The line is now

GetCartDates(commerceContext.CurrentShopperId(), cartID, out checkinDate, out checkoutDate);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.