|
using System; |
|
using System.ComponentModel; |
|
using System.Web.UI.WebControls; |
|
using Telerik.Sitefinity; |
|
using Telerik.Sitefinity.Abstractions; |
|
using Telerik.Sitefinity.Data.Metadata; |
|
using Telerik.Sitefinity.Ecommerce.Orders.Model; |
|
using Telerik.Sitefinity.Modules.Ecommerce.Events; |
|
using Telerik.Sitefinity.Modules.Ecommerce.Orders; |
|
using Telerik.Sitefinity.Services; |
|
|
|
namespace SitefinityWebApp |
|
{ |
|
public class Global : System.Web.HttpApplication |
|
{ |
|
protected void Application_Start(object sender, EventArgs e) |
|
{ |
|
Bootstrapper.Initialized += new EventHandler<Telerik.Sitefinity.Data.ExecutedEventArgs>(Bootstrapper_Initialized); |
|
} |
|
|
|
private void Bootstrapper_Initialized(object sender, Telerik.Sitefinity.Data.ExecutedEventArgs e) |
|
{ |
|
if (e.CommandName == "Bootstrapped") |
|
{ |
|
EventHub.Subscribe<IEcommerceCheckoutPageChangingEvent>(OnEcommerceCheckoutPageChanging); |
|
|
|
this.CreateCustomOrderFields(); |
|
} |
|
} |
|
|
|
public void OnEcommerceCheckoutPageChanging(IEcommerceCheckoutPageChangingEvent evt) |
|
{ |
|
// This event could be raised after the shopping cart was destroyed so make sure you return when the ShoppingCartId is empty or null. |
|
if (evt.ShoppingCartId == Guid.Empty || evt.ShoppingCartId == null) |
|
{ |
|
return; |
|
} |
|
|
|
// Check that you are in the Shipping Information step. |
|
if (evt.CurrentStepIndex == 0) |
|
{ |
|
SaveCustomBillingAndShippingFields(evt); |
|
} |
|
|
|
// Shipping (current step index = 1) |
|
// Payment Information (current step index = 2) |
|
|
|
// Check that you are in the Preview step (current step index = 3) |
|
if (evt.CurrentStepIndex == 3) |
|
{ |
|
SaveCustomPreviewFields(evt); |
|
} |
|
|
|
} |
|
|
|
private void CreateCustomOrderFields() |
|
{ |
|
// You need an instance of the MetadataMananger in order to add the new meta field to the tables. |
|
MetadataManager metaManager = MetadataManager.GetManager(); |
|
|
|
// Check if the Order table has already been modified to contain meta fields |
|
if (metaManager.GetMetaType(typeof(Order)) == null) |
|
{ |
|
// Create the metatype for the order class. |
|
metaManager.CreateMetaType(typeof(Order)); |
|
|
|
// Save the changes |
|
metaManager.SaveChanges(); |
|
} |
|
|
|
// Add a new meta field to the Order table |
|
App.WorkWith() |
|
.DynamicData() |
|
.Type(typeof(Order)) |
|
.Field() |
|
.TryCreateNew("NewsLetter", typeof(string)) |
|
.SaveChanges(); |
|
|
|
// Add a new meta field to the Order table |
|
App.WorkWith() |
|
.DynamicData() |
|
.Type(typeof(Order)) |
|
.Field() |
|
.TryCreateNew("GiftMessage", typeof(string)) |
|
.SaveChanges(); |
|
|
|
// Check if the CartOrder table has already been modified to contain meta fields |
|
if (metaManager.GetMetaType(typeof(CartOrder)) == null) |
|
{ |
|
|
|
// Create the metatype for the CartOrder class. |
|
metaManager.CreateMetaType(typeof(CartOrder)); |
|
|
|
//Save the changes. |
|
metaManager.SaveChanges(); |
|
} |
|
|
|
// Add a new meta field to the CartOrder table |
|
App.WorkWith() |
|
.DynamicData() |
|
.Type(typeof(CartOrder)) |
|
.Field() |
|
.TryCreateNew("NewsLetter", typeof(string)) |
|
.SaveChanges(); |
|
|
|
// Add a new meta field to the CartOrder table |
|
App.WorkWith() |
|
.DynamicData() |
|
.Type(typeof(CartOrder)) |
|
.Field() |
|
.TryCreateNew("GiftMessage", typeof(string)) |
|
.SaveChanges(); |
|
} |
|
|
|
private void SaveCustomPreviewFields(IEcommerceCheckoutPageChangingEvent evt) |
|
{ |
|
// Find the custom control on the current page using the evt.Container object's GetControl method. |
|
TextBox giftMessageTextBox = evt.Container.GetControl<TextBox>("giftMessage", false); |
|
|
|
// Check to see that you have actually found the textbox control that you were looking for on the page |
|
if (giftMessageTextBox != null) |
|
{ |
|
// Get the value of the textbox. |
|
string giftMessage = giftMessageTextBox.Text; |
|
|
|
OrdersManager ordersManager = OrdersManager.GetManager(); |
|
|
|
// Get a copy of the shopping cart order based on the evt.ShoppingCartId. |
|
CartOrder cartOrder = ordersManager.GetCartOrder(evt.ShoppingCartId); |
|
|
|
if (cartOrder != null) |
|
{ |
|
// Get all properties of the cartOrder object. |
|
PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(cartOrder); |
|
|
|
// Get the meta property with the name used when creating the field in the CreateCustomOrderFields() method. |
|
PropertyDescriptor property = properties["GiftMessage"]; |
|
|
|
MetafieldPropertyDescriptor metaProperty = property as MetafieldPropertyDescriptor; |
|
|
|
// Safety check to make sure you have found the appropriately named property in the cartOrder object. |
|
if (metaProperty != null) |
|
{ |
|
// Set the meta property of the cartOrder object using the value from the checkbox control. |
|
metaProperty.SetValue(cartOrder, giftMessage); |
|
|
|
// Save the new value to the database. |
|
ordersManager.SaveChanges(); |
|
} |
|
} |
|
} |
|
} |
|
|
|
private void SaveCustomBillingAndShippingFields(IEcommerceCheckoutPageChangingEvent evt) |
|
{ |
|
// Find the custom control on the current page using the container object's GetControl method. |
|
CheckBox newsletterCheckbox = evt.Container.GetControl<CheckBox>("newsletter", false); |
|
|
|
// Check to see that you have actually found the checkbox control that you were looking for on the page. |
|
if (newsletterCheckbox != null) |
|
{ |
|
// Get the value of the checkbox control. |
|
bool newsLetter = newsletterCheckbox.Checked; |
|
|
|
OrdersManager ordersManager = OrdersManager.GetManager(); |
|
|
|
// Get an instance of the shopping cart order based on the evt.ShoppingCartId. |
|
CartOrder cartOrder = ordersManager.GetCartOrder(evt.ShoppingCartId); |
|
|
|
if (cartOrder != null) |
|
{ |
|
// Get all properties of the cartOrder object. |
|
PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(cartOrder); |
|
|
|
// Get the meta property with the name used when creating the field in the CreateCustomOrderFields() method. |
|
PropertyDescriptor property = properties["NewsLetter"]; |
|
|
|
MetafieldPropertyDescriptor metaProperty = property as MetafieldPropertyDescriptor; |
|
|
|
// Safety check to make sure you have found the appropriately named property in the cartOrder object. |
|
if (metaProperty != null) |
|
{ |
|
// Set the meta property of the cartOrder object using the value from the checkbox control. |
|
metaProperty.SetValue(cartOrder, newsLetter.ToString()); |
|
|
|
// Save the new value to the database. |
|
ordersManager.SaveChanges(); |
|
} |
|
} |
|
} |
|
} |
|
} |
|
} |