Exemple #1
0
        public ActionResult LoadShippingDescription(int productId)
        {
            var    isAffiliate          = false;
            string shippingDescriptions = "";

            if (productId > 0)
            {
                var productMapping = _productMappingRepository.TableNoTracking.FirstOrDefault(x => x.ProductId == productId);
                if (productMapping != null)
                {
                    ProductMappingSettings productMappingSettings = _settingService.LoadSetting <ProductMappingSettings>();
                    isAffiliate          = true;
                    shippingDescriptions = productMappingSettings.ShippingDescriptions;
                }
            }
            return(Json(new { IsAffiliate = isAffiliate, ShippingDescriptions = shippingDescriptions }));
        }
        /// <summary>
        /// Gets the final price
        /// </summary>
        /// <param name="product">Product</param>
        /// <param name="customer">The customer</param>
        /// <param name="overriddenProductPrice">Overridden product price. If specified, then it'll be used instead of a product price. For example, used with product attribute combinations</param>
        /// <param name="additionalCharge">Additional charge</param>
        /// <param name="includeDiscounts">A value indicating whether include discounts or not for final price computation</param>
        /// <param name="quantity">Shopping cart item quantity</param>
        /// <param name="rentalStartDate">Rental period start date (for rental products)</param>
        /// <param name="rentalEndDate">Rental period end date (for rental products)</param>
        /// <param name="discountAmount">Applied discount amount</param>
        /// <param name="appliedDiscounts">Applied discounts</param>
        /// <returns>Final price</returns>
        public virtual decimal GetFinalPrice(Product product,
                                             Customer customer,
                                             decimal?overriddenProductPrice,
                                             decimal additionalCharge,
                                             bool includeDiscounts,
                                             int quantity,
                                             DateTime?rentalStartDate,
                                             DateTime?rentalEndDate,
                                             out decimal discountAmount,
                                             out List <DiscountForCaching> appliedDiscounts)
        {
            if (product == null)
            {
                throw new ArgumentNullException("product");
            }

            discountAmount   = decimal.Zero;
            appliedDiscounts = new List <DiscountForCaching>();

            var cacheKey = string.Format(PriceCacheEventConsumer.PRODUCT_PRICE_MODEL_KEY,
                                         product.Id,
                                         overriddenProductPrice.HasValue ? overriddenProductPrice.Value.ToString(CultureInfo.InvariantCulture) : null,
                                         additionalCharge.ToString(CultureInfo.InvariantCulture),
                                         includeDiscounts,
                                         quantity,
                                         string.Join(",", customer.GetCustomerRoleIds()),
                                         _storeContext.CurrentStore.Id);
            var cacheTime = _catalogSettings.CacheProductPrices ? 60 : 0;

            //we do not cache price for rental products
            //otherwise, it can cause memory leaks (to store all possible date period combinations)
            if (product.IsRental)
            {
                cacheTime = 0;
            }
            var cachedPrice = _cacheManager.Get(cacheKey, cacheTime, () =>
            {
                var _productMappingService = EngineContext.Current.Resolve <IProductMappingService>();
                var result = new ProductPriceForCaching();
                //initial price
                decimal price = overriddenProductPrice.HasValue ? overriddenProductPrice.Value : product.Price;

                if (_productMappingService.GetProductMappingByProductId(product.Id) != null)
                {
                    var _settingService = EngineContext.Current.Resolve <ISettingService>();
                    ProductMappingSettings mappingSettings = _settingService.LoadSetting <ProductMappingSettings>();
                    price = price * (1M + mappingSettings.AdditionalCostPercent / 100);
                }

                //tier prices
                var tierPrice = product.GetPreferredTierPrice(customer, _storeContext.CurrentStore.Id, quantity);
                if (tierPrice != null)
                {
                    price = tierPrice.Price;
                }

                //additional charge
                price = price + additionalCharge;

                //rental products
                if (product.IsRental)
                {
                    if (rentalStartDate.HasValue && rentalEndDate.HasValue)
                    {
                        price = price * product.GetRentalPeriods(rentalStartDate.Value, rentalEndDate.Value);
                    }
                }

                if (includeDiscounts)
                {
                    //discount
                    List <DiscountForCaching> tmpAppliedDiscounts;
                    decimal tmpDiscountAmount = GetDiscountAmount(product, customer, price, out tmpAppliedDiscounts);
                    price = price - tmpDiscountAmount;

                    if (tmpAppliedDiscounts != null)
                    {
                        result.AppliedDiscounts      = tmpAppliedDiscounts;
                        result.AppliedDiscountAmount = tmpDiscountAmount;
                    }
                }

                if (price < decimal.Zero)
                {
                    price = decimal.Zero;
                }

                result.Price = price;
                return(result);
            });

            if (includeDiscounts)
            {
                if (cachedPrice.AppliedDiscounts.Any())
                {
                    appliedDiscounts.AddRange(cachedPrice.AppliedDiscounts);
                    discountAmount = cachedPrice.AppliedDiscountAmount;
                }
            }

            return(cachedPrice.Price);
        }