+1-888-365-2779
Try Now
More in this section

Forums / Ecommerce / Sitefinity 7.0 custom field for Order payment not being saved

Sitefinity 7.0 custom field for Order payment not being saved

1 posts, 0 answered
  1. Praneeth
    Praneeth avatar
    26 posts
    Registered:
    22 Jun 2012
    04 Sep 2014
    Link to this post

    Hi,

     I am referring this link  to create custom fields. I have modified the code as I want to save custom fields for payment step. I have attached the Global.ascx file for the same.

     The issue what I am facing is the custom field i.e. SapNumber is getting saving in database for CartPayment table only. Even though the order has been completed it does not appear in the OrderPayment table. Whereas the custom field Notes gets saved both in CartOrder and Order table. Only payment is giving issues :-(

     Please let me know what I am missing here.

     P.S : Not able to attach code file only image allowed so pasting the code here:

     using System;

    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Linq;
    using System.Web;
    using System.Web.Security;
    using System.Web.SessionState;
    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;
    using Telerik.Sitefinity.Model;

    namespace CoreStore.Website
    {
        public class Global : System.Web.HttpApplication
        {
            //http://www.sitefinity.com/documentation/documentationarticles/developers-guide/sitefinity-essentials/modules/ecommerce/checkout/adding-custom-fields-to-the-checkout-widget/modifying-the-checkout-widgets-templates/modifying-the-shipping-and-billing-information-template
            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)
                //{
                    
                //}

                // Shipping (current step index = 1)
                // Payment Information (current step index = 2)
                if (evt.CurrentStepIndex == 2)
                {
                    SaveCustomPaymentFields(evt);
                }

                // 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("Notes", typeof(string))
                    .SaveChanges(true);

                // 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("Notes", typeof(string))
                    .SaveChanges(true);


                CreatCustomPaymentField(metaManager);
            }

            private void CreatCustomPaymentField(MetadataManager metaManager)
            {
                // Check if the CartOrder table has already been modified to contain meta fields
                if (metaManager.GetMetaType(typeof(OrderPayment)) == null)
                {

                    // Create the metatype for the CartOrder class.
                    metaManager.CreateMetaType(typeof(OrderPayment));

                    //Save the changes.
                    metaManager.SaveChanges();
                }

                // Add a new meta field to the CartOrder table
                App.WorkWith()
                    .DynamicData()
                    .Type(typeof(OrderPayment))
                    .Field()
                    .TryCreateNew("SapNumber", typeof(string))
                    .SaveChanges(true);

               
                 
                if (metaManager.GetMetaType(typeof(CartPayment)) == null)
                {

                    // Create the metatype for the CartOrder class.
                    metaManager.CreateMetaType(typeof(CartPayment));

                    //Save the changes.
                    metaManager.SaveChanges();
                }

                // Add a new meta field to the CartOrder table
                App.WorkWith()
                    .DynamicData()
                    .Type(typeof(CartPayment))
                    .Field()
                    .TryCreateNew("SapNumber", typeof(string))
                    .SaveChanges(true);
            }



            private void SaveCustomPaymentFields(IEcommerceCheckoutPageChangingEvent evt)
            {
                // Find the custom control on the current page using the evt.Container object's GetControl method.
                TextBox txtSapNumber = evt.Container.GetControl<TextBox>("sapnumber", false);

                // Check to see that you have actually found the textbox control that you were looking for on the page
                if (txtSapNumber != null)
                {
                    // Get the value of the textbox.
                    string sapnumber = txtSapNumber.Text;

                    OrdersManager ordersManager = OrdersManager.GetManager();

                    // Get a copy of the shopping cart order based on the evt.ShoppingCartId.
                    CartOrder cartOrder = ordersManager.GetCartOrder(evt.ShoppingCartId);

                    CartPayment cartPayment = cartOrder.Payments[0];

                    if (cartOrder != null)
                    {
                        // Get all properties of the cartOrder object.
                        PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(cartPayment);

                        // Get the meta property with the name used when creating the field in the CreateCustomOrderFields() method.
                        PropertyDescriptor property = properties["SapNumber"];

                        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(cartPayment, sapnumber);

                            // Save the new value to the database.
                            ordersManager.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>("notes", 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 notes = 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["Notes"];

                        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, notes);

                            // Save the new value to the database.
                            ordersManager.SaveChanges();
                        }
                    }
                }
            }
         }
    }

     

    Thanks

    Praneeth

1 posts, 0 answered