Пример #1
0
        /// <summary>
        /// Called when [next button click].
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="T:System.Web.UI.WebControls.WizardNavigationEventArgs"/> instance containing the event data.</param>
        protected void OnNextButtonClick(object sender, WizardNavigationEventArgs e)
        {
            if (CheckoutWizard.ActiveStepIndex == CheckoutWizard.WizardSteps.IndexOf(this.ShippingAddressStep))
            {
                if (CheckoutShippingAddress.ValidateForm())
                {
                    OrderAddress address = CheckoutShippingAddress.ShippingAddress;

                    // set current shipping address as customer's preferred shipping address
                    if (!Profile.IsAnonymous)
                    {
                        //Profile.Addresses.Count == 0
                        CustomerAddress custAddress = StoreHelper.ConvertToCustomerAddress(address);
                        custAddress.Name = _PreferredShippingAddressString;
                        Profile.PreferredShippingAddress = custAddress.Name;
                        if (Profile.Account != null)
                        {
                            if (!StoreHelper.IsAddressInCollection(Profile.Account.Addresses, custAddress))
                            {
                                Profile.Account.Addresses.Add(custAddress);
                                Profile.Account.Addresses.AcceptChanges();
                            }
                        }
                    }

                    // If name is not set, set to the default shipping address name
                    if (String.IsNullOrEmpty(address.Name))
                    {
                        address.Name = _ShippingAddressString;
                    }

                    object shippingAddressId = Session[_SessionShippingAddressKey];

                    OrderAddress orderAddress = shippingAddressId != null?CartHelper.FindAddressByName(shippingAddressId.ToString()) : null;

                    if (orderAddress == null)
                    {
                        // Save address for the order
                        CartHelper.Cart.OrderAddresses.Add(address);
                        CartHelper.Cart.AcceptChanges();

                        // Save address id in the viewstate
                        Session[_SessionShippingAddressKey] = address.Name;

                        // Change address id for all line items
                        foreach (LineItem lineItem in CartHelper.LineItems)
                        {
                            if (String.IsNullOrEmpty(lineItem.ShippingAddressId))
                            {
                                lineItem.ShippingAddressId = address.Name.ToString();
                            }
                        }
                    }
                    else // simply modify the existing address
                    {
                        foreach (MetaField field in orderAddress.MetaClass.MetaFields)
                        {
                            // copy all address fields except ids and name
                            if (!field.Name.Equals("OrderGroupAddressId", StringComparison.InvariantCultureIgnoreCase) &&
                                !field.Name.Equals("OrderGroupId", StringComparison.InvariantCultureIgnoreCase) &&
                                !field.Name.Equals("Name", StringComparison.InvariantCultureIgnoreCase))
                            {
                                orderAddress[field.Name] = address[field.Name];
                            }
                        }
                    }

                    CartHelper.Cart.AcceptChanges();
                }
                else
                {
                    e.Cancel = true;
                    return;
                }
            }
            else if (CheckoutWizard.ActiveStepIndex == CheckoutWizard.WizardSteps.IndexOf(this.ShippingOptionsStep))
            {
                CartHelper.Cart.AcceptChanges();
            }
            else if (CheckoutWizard.ActiveStepIndex == CheckoutWizard.WizardSteps.IndexOf(this.PaymentStep))
            {
                if (CheckoutPayment.ValidateData())
                {
                    OrderAddress address = CheckoutPayment.BillingAddress;

                    // set current shipping address as customer's preferred billing address
                    if (!Profile.IsAnonymous)
                    {
                        CustomerAddress custAddress = StoreHelper.ConvertToCustomerAddress(address);
                        custAddress.Name = _PreferredBillingAddressString;
                        Profile.PreferredBillingAddress = custAddress.Name;
                        if (Profile.Account != null)
                        {
                            if (!StoreHelper.IsAddressInCollection(Profile.Account.Addresses, custAddress))
                            {
                                Profile.Account.Addresses.Add(custAddress);
                                Profile.Account.Addresses.AcceptChanges();
                            }
                        }
                    }

                    object billingAddressId = Session[_SessionBillingAddressKey];

                    OrderAddress orderAddress = billingAddressId != null?CartHelper.FindAddressByName(billingAddressId.ToString()) : null;

                    if (orderAddress == null)
                    {
                        // If name is not set, set to the default shipping address name
                        if (String.IsNullOrEmpty(address.Name))
                        {
                            address.Name = "Billing Address";
                        }

                        // Save address for the order
                        CartHelper.Cart.OrderAddresses.Add(address);
                        CartHelper.Cart.AcceptChanges();

                        // Save the address
                        CartHelper.OrderForm.BillingAddressId = address.Name;

                        // Save address id in the viewstate
                        Session[_SessionBillingAddressKey] = address.Name;
                    }
                    else // simply modify the existing address
                    {
                        foreach (MetaField field in orderAddress.MetaClass.MetaFields)
                        {
                            // copy all address fields except ids and name
                            if (!field.Name.Equals("OrderGroupAddressId", StringComparison.InvariantCultureIgnoreCase) &&
                                !field.Name.Equals("OrderGroupId", StringComparison.InvariantCultureIgnoreCase) &&
                                !field.Name.Equals("Name", StringComparison.InvariantCultureIgnoreCase))
                            {
                                orderAddress[field.Name] = address[field.Name];
                            }
                        }

                        address = orderAddress;
                    }

                    // set primary address
                    CartHelper.OrderForm.BillingAddressId = address.Name.ToString();

                    // Save email address
                    address.Email = CheckoutPayment.EmailText;
                    //if (String.IsNullOrEmpty(Profile.Email))
                    Profile.Email = CheckoutPayment.EmailText;

                    //if(String.IsNullOrEmpty(Profile.FirstName))
                    Profile.FirstName = address.FirstName;

                    //if(String.IsNullOrEmpty(Profile.LastName))
                    Profile.LastName = address.LastName;

                    //if (String.IsNullOrEmpty(Profile.FullName))
                    Profile.FullName = String.Format("{0} {1}", Profile.FirstName, Profile.LastName);

                    // Save profile
                    Profile.Save();

                    // Set customer info
                    CartHelper.Cart.CustomerId   = ProfileContext.Current.UserId;
                    CartHelper.Cart.CustomerName = String.Format("{0} {1}", address.FirstName, address.LastName);

                    // Remove all shipments before we run prepare workflow since it will recreate shipments there
                    foreach (OrderForm form in CartHelper.Cart.OrderForms)
                    {
                        foreach (Shipment shipment in form.Shipments)
                        {
                            shipment.Delete();
                        }

                        // Save items
                        CartHelper.Cart.AcceptChanges();
                    }

                    // Prepare cart for checkout
                    CartHelper.Cart.RunWorkflow("CartPrepare");

                    // Remove previous payments
                    foreach (Payment payment in CartHelper.OrderForm.Payments)
                    {
                        payment.Delete();
                    }

                    // Add Payments to the cart
                    Payment paymentInfo = CheckoutPayment.PaymentInfo;

                    // Check if any payment methods were selected
                    if (paymentInfo != null)
                    {
                        paymentInfo.PaymentMethodId   = CheckoutPayment.PaymentMethod.PaymentMethodId;
                        paymentInfo.PaymentMethodName = CheckoutPayment.PaymentMethod.Name;
                        paymentInfo.Amount            = CartHelper.OrderForm.Total;
                        CartHelper.OrderForm.Payments.Add(paymentInfo);
                    }

                    // Save changes
                    CartHelper.Cart.AcceptChanges();
                }
                else
                {
                    e.Cancel = true;
                    return;
                }
            }
        }