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?