/// <summary> /// Saves the address. /// </summary> /// <param name="sender">The sender.</param> /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param> protected void SaveAddress(object sender, EventArgs e) { AddOrderAddress(addressType.Value); if (AddAddressToContact.Checked) { var orderAddress = _cartHelper.FindAddressByName(Name.Text); AddCustomerAddress(orderAddress); } //redirect after post Context.RedirectFast(Request.RawUrl + "#ShippingRegion" + shipmentId.Value); }
/// <summary> /// Sets the billing addresses. /// </summary> private void SetBillingAddresses() { OrderAddress address = null; if (Cart.OrderAddresses.ToArray().Length > 0) { string billingAddressId = Cart.OrderForms[0].BillingAddressId; address = CartHelper.FindAddressByName(billingAddressId); } else { var currentContact = CustomerContext.Current.CurrentContact; var defaultBillingAddress = currentContact == null ? null : currentContact.ContactAddresses.FirstOrDefault( a => currentContact.PreferredBillingAddressId.HasValue && currentContact.PreferredBillingAddressId.Value == a.PrimaryKeyId.Value); // Set default billing address to new order. if (defaultBillingAddress != null) { address = new OrderAddress(); CustomerAddress.CopyCustomerAddressToOrderAddress(defaultBillingAddress, address); Cart.OrderAddresses.Add(address); Cart.OrderForms[0].BillingAddressId = address.Name; } } BillingAddressInfo.OrderAddress = address; BillingAddressInfo.DataBind(); }
private Money CalculateShippingCostSubTotal(IEnumerable <Shipment> shipments, CartHelper cartHelper) { var retVal = new Money(0, cartHelper.Cart.BillingCurrency); foreach (Shipment shipment in shipments) { if (cartHelper.FindAddressByName(shipment.ShippingAddressId) == null) { continue; } var methods = ShippingManager.GetShippingMethods(CurrentPage.LanguageID); var shippingMethod = methods.ShippingMethod.FirstOrDefault(c => c.ShippingMethodId.Equals(shipment.ShippingMethodId)); if (shippingMethod == null) { continue; } var shipmentRateMoney = SampleStoreHelper.GetShippingCost(shippingMethod, shipment, cartHelper.Cart.BillingCurrency); retVal += shipmentRateMoney; } return(retVal); }
private bool IsValidSingleShippment() { // Check valid line item for Single Shipment if (Cart.OrderForms.Count > 0) { List <string> warehouseCodes = new List <string>(); var lineItems = Cart.OrderForms[0].LineItems; foreach (LineItem lineItem in lineItems) { if (!warehouseCodes.Contains(lineItem.WarehouseCode)) { warehouseCodes.Add(lineItem.WarehouseCode); } } // Cart only has one warehouse instore pickup if (warehouseCodes.Count == 1) { // Shipping method is "In store pickup", add address to Cart & Shipment IWarehouse warehouse = WarehouseHelper.GetWarehouse(warehouseCodes.FirstOrDefault()); if (!warehouse.IsFulfillmentCenter && warehouse.IsPickupLocation) { Shipment.WarehouseCode = warehouse.Code; if (CartHelper.FindAddressByName(warehouse.Name) == null) { var address = warehouse.ContactInformation.ToOrderAddress(); address.Name = warehouse.Name; Cart.OrderAddresses.Add(address); } Shipment.ShippingAddressId = warehouse.Name; Shipment.ShippingMethodName = ShippingManager.PickupShippingMethodName; var instorepickupShippingMethod = ShippingManager.GetShippingMethods("en").ShippingMethod.ToArray().Where(m => m.Name.Equals(ShippingManager.PickupShippingMethodName)).FirstOrDefault(); if (instorepickupShippingMethod != null) { Shipment.ShippingMethodId = instorepickupShippingMethod.ShippingMethodId; } Cart.AcceptChanges(); } } else { foreach (string warehouseCode in warehouseCodes) { IWarehouse warehouse = WarehouseHelper.GetWarehouse(warehouseCode); if (warehouse != null && warehouse.IsPickupLocation) { return(false); } } } } return(true); }
public override void DataBind() { if (CartHelper.IsEmpty) { return; } var billingCurrency = CartHelper.Cart.BillingCurrency; OrderSubTotalLineItems.Text = new Money(CartHelper.LineItems.ToArray().Sum(x => x.ExtendedPrice) + CartHelper.LineItems.ToArray().Sum(x => x.OrderLevelDiscountAmount), billingCurrency).ToString(); OrderDiscount.Text = new Money(CartHelper.Cart.OrderForms[0].Discounts.Cast <Discount>().ToArray().Sum(x => x.DiscountValue), billingCurrency).ToString(); OrderSubTotal.Text = new Money(CartHelper.LineItems.ToArray().Sum(x => x.ExtendedPrice), billingCurrency).ToString(); TaxTotal.Text = new Money(CartHelper.Cart.TaxTotal, billingCurrency).ToString(); var shipments = CartHelper.OrderForm.Shipments; // Check shipping information is full or not. bool canShip = shipments.Count > 0; if (shipments.ToArray().Any(s => CartHelper.FindAddressByName(s.ShippingAddressId) == null || s.ShippingMethodId.Equals(Guid.Empty))) { canShip = false; } // Showing the Total and calculate shipping cost/discount only when shipping information is full. Otherwise, only showing SubTotal. if (canShip) { string discountMessage = String.Empty; var shippingDiscountsTotal = CalculateShippingDiscounts(shipments, billingCurrency, out discountMessage); var shippingCostSubTotal = CalculateShippingCostSubTotal(shipments, CartHelper); shippingDiscount.Text = shippingDiscountsTotal.ToString(); ShippingDiscountsMessage.Text = discountMessage; shippingTotal.Text = (shippingCostSubTotal).ToString(); OrderTotal.Text = new Money(CartHelper.OrderForm.Total, billingCurrency).ToString(); } else { string zeroMoney = new Money(0, billingCurrency).ToString(); shippingDiscount.Text = zeroMoney; shippingTotal.Text = zeroMoney; OrderTotal.Text = new Money(CartHelper.OrderForm.SubTotal, billingCurrency).ToString(); } var orderDiscounts = ""; foreach (OrderFormDiscount dis in CartHelper.Cart.OrderForms[0].Discounts) { orderDiscounts += String.Format("<strong>{0}</strong><br />", dis.DisplayMessage); } OrderDiscountsMessage.Text = orderDiscounts; }
/// <summary> /// Create split shipment for the first time. /// </summary> private void CreateDataSplitShipment() { // Create shipment for each lineitem for the first time to demo showcase multishipment. if (Cart.OrderForms.Count > 0) { var lineItems = Cart.OrderForms[0].LineItems; if (Cart.OrderForms[0].Shipments.Count != lineItems.Count) { foreach (MetaObject shipment in Cart.OrderForms[0].Shipments) { shipment.Delete(); } foreach (LineItem lineItem in lineItems) { Shipment shipment = new Shipment(); shipment.CreatorId = SecurityContext.Current.CurrentUserId.ToString(); shipment.Created = DateTime.UtcNow; shipment.AddLineItemIndex(lineItems.IndexOf(lineItem), lineItem.Quantity); shipment.WarehouseCode = lineItem.WarehouseCode; // For Shipping method "In store pickup" IWarehouse warehouse = WarehouseHelper.GetWarehouse(lineItem.WarehouseCode); if (!warehouse.IsFulfillmentCenter && warehouse.IsPickupLocation) { if (CartHelper.FindAddressByName(warehouse.Name) == null) { var address = warehouse.ContactInformation.ToOrderAddress(); address.Name = warehouse.Name; Cart.OrderAddresses.Add(address); } shipment.ShippingAddressId = warehouse.Name; shipment.ShippingMethodName = ShippingManager.PickupShippingMethodName; var instorepickupShippingMethod = ShippingManager.GetShippingMethods("en").ShippingMethod.ToArray().Where(m => m.Name.Equals(ShippingManager.PickupShippingMethodName)).FirstOrDefault(); if (instorepickupShippingMethod != null) { shipment.ShippingMethodId = instorepickupShippingMethod.ShippingMethodId; } } Cart.OrderForms[0].Shipments.Add(shipment); } } Cart.AcceptChanges(); } }
/// <summary> /// Searches a cart's line items for a shipping address. Returns the first address found. /// </summary> /// <param name="cart">The cart being examined</param> /// <returns></returns> public static OrderAddress FindCartShippingAddress(Mediachase.Commerce.Orders.Cart cart) { CartHelper helper = new CartHelper(cart); foreach (LineItem lineItem in helper.LineItems) { if (!String.IsNullOrEmpty(lineItem.ShippingAddressId)) { OrderAddress address = helper.FindAddressByName(lineItem.ShippingAddressId); if (address != null) { return(address); } } } return(null); }
/// <summary> /// Sets the shipping addresses. /// </summary> private void SetShippingAddresses() { OrderAddress address = null; var currentContact = CustomerContext.Current.CurrentContact; if (Cart.OrderForms[0].Shipments.Count > 0 && !string.IsNullOrEmpty(CartHelper.Cart.OrderForms[0].Shipments[0].ShippingAddressId)) { string shippingAddressId = CartHelper.Cart.OrderForms[0].Shipments[0].ShippingAddressId; var cusomterAddress = currentContact == null ? null : currentContact.ContactAddresses.FirstOrDefault(ca => ca.Name.ToString().Equals(shippingAddressId)); if (cusomterAddress != null) { address = new OrderAddress(); CustomerAddress.CopyCustomerAddressToOrderAddress(cusomterAddress, address); } else { address = CartHelper.FindAddressByName(shippingAddressId); } } else { var defaultShippingAdd = currentContact == null ? null : currentContact.ContactAddresses.FirstOrDefault( a => currentContact.PreferredShippingAddressId.HasValue && currentContact.PreferredShippingAddressId.Value == a.PrimaryKeyId.Value); // Set default shipping address to new order. if (defaultShippingAdd != null) { address = new OrderAddress(); CustomerAddress.CopyCustomerAddressToOrderAddress(defaultShippingAdd, address); var shipment = Cart.OrderForms[0].Shipments[0]; shipment.ShippingAddressId = address.Name; Cart.OrderAddresses.Add(address); } } ShippingAddressInfo.OrderAddress = address; ShippingAddressInfo.DataBind(); }
protected void Page_Load(object sender, EventArgs e) { //Bind ShipmentId to button on AddressView control ShippingAddressInfo.ShipmentId = SplitShipment.ShipmentId.ToString(); if (!string.IsNullOrEmpty(SplitShipment.ShippingAddressId)) { // If Shipping method is hand off method -> Use Store to pickup var address = _cartHelper.FindAddressByName(SplitShipment.ShippingAddressId); if (address != null) { ShippingAddressInfo.OrderAddress = address; ShippingAddressInfo.DataBind(); if (ShippingManager.AddressIsHandoffLocation(address)) { ShippingAddressInfo.UseWarehouseAddress = true; var litMessage = shippingOptions.Controls[0].FindControl("litMessage") as Literal; if (litMessage != null) { litMessage.Text = ShippingManager.PickupShippingMethodName; } } else { SetShippingMethod(BindShippingMethods()); } } } else { SetShippingMethod(BindShippingMethods()); } //Lineitem data LineItemsID.ShipmentLineItems = Shipment.GetShipmentLineItems(this.SplitShipment); LineItemsID.BindData(); //Subtotal each line item OrderSubtotalID.SplitShipment = SplitShipment; OrderSubtotalID.DataBind(); }
private Money CalculateShippingCostSubTotal(IEnumerable<Shipment> shipments, CartHelper cartHelper) { var retVal = new Money(0, cartHelper.Cart.BillingCurrency); foreach (Shipment shipment in shipments) { if (cartHelper.FindAddressByName(shipment.ShippingAddressId) == null) continue; var methods = ShippingManager.GetShippingMethods(CurrentPage.LanguageID); var shippingMethod = methods.ShippingMethod.FirstOrDefault(c => c.ShippingMethodId.Equals(shipment.ShippingMethodId)); if (shippingMethod == null) continue; var shipmentRateMoney = SampleStoreHelper.GetShippingCost(shippingMethod, shipment, cartHelper.Cart.BillingCurrency); retVal += shipmentRateMoney; } return retVal; }
/// <summary> /// Get shipping address /// </summary> /// <param name="shipmentIndex">Shipment Index</param> /// <returns></returns> private OrderAddress GetShippingAddress(int shipmentIndex) { OrderAddress address = null; var currentContact = CustomerContext.Current.CurrentContact; if (Cart.OrderForms[0].Shipments.Count > 0 && !string.IsNullOrEmpty(CartHelper.Cart.OrderForms[0].Shipments[shipmentIndex].ShippingAddressId)) { string shippingAddressId = CartHelper.Cart.OrderForms[0].Shipments[shipmentIndex].ShippingAddressId; var cusomterAddress = currentContact == null ? null : currentContact.ContactAddresses.FirstOrDefault(ca => ca.Name.ToString().Equals(shippingAddressId)); if (cusomterAddress != null) { address = new OrderAddress(); CustomerAddress.CopyCustomerAddressToOrderAddress(cusomterAddress, address); } else { address = CartHelper.FindAddressByName(shippingAddressId); } } return(address); }
/// <summary> /// Gets all shipping methods. /// </summary> /// <param name="cart">The cart.</param> /// <param name="methodIds">The method ids.</param> /// <returns>shipping method model</returns> public static ShippingMethodModel[] GetShippingMethods(this CartHelper cart, List <string> methodIds = null) { var list = new List <ShippingMethodModel>(); methodIds = methodIds ?? new List <string>(); IEnumerable <ShippingMethod> shippingMethods; if (methodIds.Any()) { shippingMethods = cart.ShippingClient.GetAllShippingMethods().Where(so => methodIds.Contains(so.ShippingMethodId)); } else { var options = cart.ShippingClient.GetAllShippingOptions(); shippingMethods = options.SelectMany(o => o.ShippingMethods.Where(m => m.IsActive)); } foreach (var method in shippingMethods.Where(m => m.IsActive && (string.IsNullOrEmpty(m.Currency) || m.Currency.Equals(UserHelper.CustomerSession.Currency, StringComparison.OrdinalIgnoreCase)))) { var model = new ShippingMethodModel(method, cart.Cart); //Filter by shipping method jurisdiction var jurisdictionRelations = method.ShippingMethodJurisdictionGroups.SelectMany(g => g.JurisdictionGroup.JurisdictionRelations).ToArray(); if (jurisdictionRelations.Any()) { var address = cart.FindAddressByName("Shipping") ?? cart.FindAddressByName("Billing"); var jurisdictionFound = false; if (address != null) { jurisdictionFound = jurisdictionRelations.Any(j => j.Jurisdiction.CheckAllFieldsMatch( address.CountryCode, address.StateProvince, address.PostalCode, address.RegionName, "", "", address.City)); } if (!jurisdictionFound) { continue; } } methodIds.Add(method.ShippingMethodId); list.Add(model); } var rates = GetAllShippingRates(cart, methodIds.ToArray(), cart.LineItems); if (rates != null) { foreach (var sm in list) { sm.Rate = (from r in rates where r.Id == sm.Id select r).SingleOrDefault(); } } return(list.ToArray()); }
/// <summary> /// Binds the billing address. /// </summary> private void BindBillingAddress() { CustomerProfile ci = ProfileContext.Current.Profile; if (ci == null || ci.Account == null || ci.Account.Addresses == null || ci.Account.Addresses.Count == 0) { tblAddresses.Visible = false; OrderAddress address = CartHelper.FindAddressByName(CartHelper.OrderForm.BillingAddressId); if (address != null) { AddressNewModule1.AddressInfo = address; } else { if (CartHelper.Cart.OrderAddresses.Count > 0) { AddressNewModule1.AddressInfo = CartHelper.Cart.OrderAddresses[0]; } } rbBillingNewAddress.Checked = true; return; } //bool bSearch = CurrentOrderInfo.BillingAddress!=null; //bool bFound = false; AddressesList.Items.Clear(); if (ci.Account.Addresses.Count > 0) { AddressesList.DataSource = ci.Account.Addresses; AddressesList.DataBind(); AddressViewModule.AddressInfo = StoreHelper.ConvertToOrderAddress(ci.Account.Addresses[0]); AddressViewModule.DataBind(); CommonHelper.SelectListItem(AddressesList, Profile.PreferredBillingAddress); if (!rbBillingNewAddress.Checked) { rbBillingAddress.Checked = true; } } else { } /* * foreach (AddressInfo info in ci.Addresses) * { * string strAddress = MakeAddressString(info); * AddressesList.Items.Add(new ListItem(strAddress, info.AddressId)); * if(bSearch && (info.AddressId == CurrentOrderInfo.BillingAddress.AddressId)) * bFound = true; * } * */ /* * if (!bFound) * { * if (CurrentOrderInfo.BillingAddress != null) * { * AddressNewModule1.AddressInfo = CurrentOrderInfo.BillingAddress; * } * else * { * // bind shipping address * if (CurrentOrderInfo.Shipments != null && CurrentOrderInfo.Shipments.Length > 0 && CurrentOrderInfo.Shipments[0].Details.DeliveryAddress != null) * { * // need to set AddressId to 0 to avoid replacing corresponding address' fields if new address' fields will be changed * AddressInfo ai = CurrentOrderInfo.Shipments[0].Details.DeliveryAddress; * ai.AddressId = "0"; * AddressNewModule1.AddressInfo = ai; * } * } * * rbBillingNewAddress.Checked = true; * * // Bind view address * if (ci != null && ci.Addresses != null && ci.Addresses.Length != 0) * { * AddressViewModule.AddressInfo = ci.Addresses[0]; * AddressViewModule.DataBind(); * } * } * else * { * if (BillingAddress != null && !String.IsNullOrEmpty(BillingAddress.AddressId)) * { * CommonHelper.SelectListItem(AddressesList, BillingAddress.AddressId); * AddressViewModule.AddressInfo = BillingAddress; * AddressViewModule.DataBind(); * } * else * { * CommonHelper.SelectListItem(AddressesList, AddressesList.Items[0].Value); * AddressViewModule.AddressInfo = ci.Addresses[0]; * AddressViewModule.DataBind(); * } * rbBillingAddress.Checked = true; * } * */ }