3

I'm working on creating a custom promotion. Our site sells theme park & hotel tickets online. The business scenario is - 20% off, if the visit date is 3 months from today.

The user can add both theme park & hotel tickets to the cart simultaneously.
The cart line items like customerid, externalcartid, product id, variant id and checkin/out dates are stored in a custom database as soon user adds the items to the cart. Let's assume, that the author has added items in the TargetItemId fields of "items" & "qualifications" tabs of pricing & promotions manager.

Here is my code for the condition:

[EntityIdentifier("Advance Month Booking")]
public class AdvanceMonthBookingCondition : ICartsCondition, ICondition, IMappableRuleEntity
{
    public IRuleValue<int> MonthsInAdvanceToVisitDate { get; set; }             

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

        try
        {
            if (commerceContext != null)
            {
                var promotionModel = (commerceContext.Objects.OfType<Promotion>()).FirstOrDefault(); //The result is null here.
                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))
                    {
                        int monthsInAdvance = 0;

                        if (int.TryParse(Convert.ToString(MonthsInAdvanceToVisitDate.Yield(context)), out monthsInAdvance) && monthsInAdvance >= 1)
                        {
                            //get details from custom database
                            DataTable dt = GetCartDates(commerceContext.CurrentShopperId(), cartID);

                            //loop through the datatable and check if the specified product is there in cart and is in eligible date range
                        }
                    }
                }
            }
        }
        catch (Exception ex)
        {
            throw ex;               
        }

        return isPromoApplicable;
    }
}

I would like to know, how I can access the promotion properties here. Mainly the TargetItemIds specified while adding the promotion in pricing & promotions manager. So, that I can check if the same are available in the line items and they have a valid date.

Also, there are two targetItemId fields - one in items tab and another in qualifications tab, where author can set IDs. Which is to be used when?

1 Answer 1

4
+50

If we take a look at Sitecore.Commerce.Plugin.Carts.CartItemQuantityCondition and Sitecore.Commerce.Plugin.Carts.CartItemSellPriceAction, then you can see that both of them inherit a base class called Sitecore.Commerce.Plugin.Carts.CartTargetItemId which provides them this functionality. This base class exposes a property which will contain what the user has specified in the Business Tools when creating the promotion

public IRuleValue<string> TargetItemId { get; set; }

However this will just give you the raw data, what you'll probably want to use is the Method provided by the base class as that will match this raw data to the context cart and give you the matching lines.

protected virtual IEnumerable<CartLineComponent> MatchingLines(IRuleExecutionContext context)

If you add the Sitecore.Commerce.Plugin.Carts nuget package then you can just inherit from the same base class to use the existing functionality in your conditions & benefits.

EDITED

As per the request in your comment, here is an example of how to inherit from this base class to gain access to the CartLines that match the targetIDs specificed by the user in the Business Tools

Condition:

using Sitecore.Commerce.Plugin.Carts;
using Sitecore.Framework.Rules;

namespace Feature.Rules.Engine.Conditions
{
    [EntityIdentifier("MySampleCartCondition")]
    public class SampleCartCondition : CartTargetItemId, ICondition
    {
        public bool Evaluate(IRuleExecutionContext context)
        {
            var matchingLines = MatchingLines(context);

            //Insert logic to evaulate whether the matching lines visit date is within 3 months from today
        }
    }
}

Benefit:

using Sitecore.Commerce.Plugin.Carts;
using Sitecore.Framework.Rules;

namespace Feature.Rules.Engine.Benefits
{
    [EntityIdentifier("MySampleBenefit")]
    public class SampleBenefit : CartTargetItemId, ICartLineAction
    {
        public void Execute(IRuleExecutionContext context)
        {
            var matchingLines = MatchingLines(context);

            //Insert logic to apply your 20% benefit to the matching cart lines
        }
    }
}
1
  • The plugin project I have already has a reference to Sitecore.Commerce.Plugin.Carts. I am a novice with commerce. Could you please share any code sample, how can I use it in my code to access those props. Thank you.
    – sukesh
    Commented Oct 1, 2018 at 10:23

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.