Пример #1
0
 public GetPrice(int productId, int variantId, decimal originalPrice, ShoppingContext shoppingContext)
 {
     ProductId = productId;
     VariantId = variantId;
     OriginalPrice = originalPrice;
     FinalPrice = originalPrice;
     ShoppingContext = shoppingContext;
 }
Пример #2
0
        public Order CreateFromCart(ShoppingCart cart, ShoppingContext context)
        {
            Require.NotNull(cart, "cart");
            Require.NotNull(cart.Customer, "cart.Customer", "Cannot create order from guest cart. Login is required.");
            Require.NotNull(cart.ShippingAddress, "cart.ShippingAddress", "Shipping address is required.");
            Require.That(cart.Items.Count > 0, "cart.Items", "Cannot create order from an empty cart.");

            // Recalculate price
            var pricingContext = new ShoppingCartService(_instance).CalculatePrice(cart, context);

            var order = new Order();
            order.ShoppingCartId = cart.Id;
            order.CustomerId = cart.Customer.Id;
            order.Coupon = cart.CouponCode;

            foreach (var item in pricingContext.Items)
            {
                var cartItem = cart.Items.FirstOrDefault(i => i.Id == item.ItemId);

                var orderItem = OrderItem.CreateFrom(cartItem, item.UnitPrice);
                orderItem.UnitPrice = item.UnitPrice;
                orderItem.Discount = item.Discount;
                orderItem.Subtotal = item.Subtotal;
                orderItem.Total = item.Subtotal - item.Discount;

                order.OrderItems.Add(orderItem);
            }

            order.ShippingAddress = OrderAddress.CreateFrom(cart.ShippingAddress);

            if (cart.BillingAddress != null)
            {
                order.BillingAddress = OrderAddress.CreateFrom(cart.BillingAddress);
            }

            order.ShippingCost = pricingContext.ShippingCost;
            order.PaymentMethodCost = pricingContext.PaymentMethodCost;
            order.Tax = pricingContext.Tax;
            order.Discount = pricingContext.TotalDiscount;

            order.Subtotal = pricingContext.Subtotal;
            order.Total = pricingContext.Total;

            Create(order);

            return order;
        }
Пример #3
0
 public decimal GetFinalPrice(ShoppingContext context)
 {
     return PriceCalculationContext.GetFinalPrice(ProductId, Id, Price, context);
 }
Пример #4
0
        public PriceCalculationContext CalculatePrice(ShoppingCart cart, ShoppingContext shoppingContext)
        {
            Require.NotNull(cart, "cart");

            var context = PriceCalculationContext.CreateFrom(cart);
            if (shoppingContext != null)
            {
                context.Culture = shoppingContext.Culture;
            }

            new PriceCalculator().Calculate(context);

            Event.Raise(new CartPriceCalculated(cart, context), _instance);

            return context;
        }
Пример #5
0
 public virtual decimal GetFinalPrice(int variantId, ShoppingContext context)
 {
     var variant = Variants.FirstOrDefault(it => it.Id == variantId);
     return variant.GetFinalPrice(context);
 }
Пример #6
0
 public decimal GetFinalUnitPrice(int productId, int productPriceId, decimal originalPrice)
 {
     var customerId = Customer == null ? null : (int?)Customer.Id;
     var shoppingContext = new ShoppingContext(customerId, Culture);
     return PriceCalculationContext.GetFinalPrice(productId, productPriceId, originalPrice, shoppingContext);
 }
Пример #7
0
 /// <summary>
 /// Gets the final price of the specified product price in the current shopping context.
 /// </summary>
 public static decimal GetFinalPrice(int productId, int variantId, decimal originalPrice, ShoppingContext shoppingContext)
 {
     // TODO: Avoid using Current
     var @event = new GetPrice(productId, variantId, originalPrice, shoppingContext);
     Event.Raise(@event, CommerceInstance.Current);
     return @event.FinalPrice;
 }