Esempio n. 1
0
        /// <summary>
        /// Gets the discount price by evaluating the discount rules and taking into account segments customer belongs to.
        /// </summary>
        /// <param name="entry">The entry.</param>
        /// <param name="catalogName">Name of the catalog.</param>
        /// <returns></returns>
        public static Price GetDiscountPrice(Entry entry, string catalogName)
        {
            if (entry == null)
            {
                throw new NullReferenceException("entry can't be null");
            }

            decimal minQuantity = 1;

            // get min quantity attribute
            if (entry.ItemAttributes != null)
            {
                minQuantity = entry.ItemAttributes.MinQuantity;
            }

            // we can't pass qauntity of 0, so make it default to 1
            if (minQuantity <= 0)
            {
                minQuantity = 1;
            }

            // Get sale price for the current user
            Price price = StoreHelper.GetSalePrice(entry, minQuantity);

            // Create new promotion helper, which will initialize PromotionContext object for us and setup context dictionary
            PromotionHelper helper = new PromotionHelper();

            // Get current context
            Dictionary <string, object> context = MarketingContext.Current.MarketingProfileContext;

            // Create filter
            PromotionFilter filter = new PromotionFilter();

            filter.IgnoreConditions = false;
            filter.IgnorePolicy     = false;
            filter.IgnoreSegments   = false;
            filter.IncludeCoupons   = false;

            // Create new entry
            PromotionEntry promotEntry = new PromotionEntry(catalogName, String.Empty, entry.ID, price.Amount);

            // Populate entry parameters
            ((IPromotionEntryPopulate)MarketingContext.Current.PromotionEntryPopulateFunctionClassInfo.CreateInstance()).Populate(ref promotEntry, entry);

            PromotionEntriesSet sourceSet = new PromotionEntriesSet();

            sourceSet.Entries.Add(promotEntry);

            // Configure promotion context
            helper.PromotionContext.SourceEntriesSet = sourceSet;
            helper.PromotionContext.TargetEntriesSet = sourceSet;
            // Only target entries
            helper.PromotionContext.TargetGroup = PromotionGroup.GetPromotionGroup(PromotionGroup.PromotionGroupKey.Entry).Key;

            // Execute the promotions and filter out basic collection of promotions, we need to execute with cache disabled, so we get latest info from the database
            helper.Eval(filter);

            // Check the count, and get new price
            if (helper.PromotionContext.PromotionResult.PromotionRecords.Count > 0)
            {
                return(ObjectHelper.CreatePrice(price.Amount - GetDiscountPrice(helper.PromotionContext.PromotionResult), price.CurrencyCode));
            }
            else
            {
                return(price);
            }
        }