protected void AddToCartButton_Command(object sender, CommandEventArgs e)
        {
            try
            {
                int quantity = 1;

                IShoppingCartAdder shoppingCartAdder = new ShoppingCartAdder();
                string defaultCurrencyName = Config.Get<EcommerceConfig>().DefaultCurrency;

                OptionsDetails optionsDetails = new OptionsDetails();

                decimal price = 0;
                if (!decimal.TryParse(DonationAmountDropDown.Value.ToString(), out price))
                {
                    price = Convert.ToDecimal(OtherAmountControl.Value);
                }
                this.Product.Price = price;
                shoppingCartAdder.AddItemToShoppingCart(this, this.OrdersManager, this.Product, optionsDetails, quantity, defaultCurrencyName);
                this.AddedToCartMessage.ShowPositiveMessage("Donation added to cart");
            }
            catch (Exception ex)
            {
                this.AddedToCartMessage.ShowNegativeMessage("Failed to add donation to cart, try again");
            }
        }
예제 #2
0
        protected void AddToCartButton_Command(object sender, EventArgs e)
        {
            try
            {
                int quantity = 1;

                IShoppingCartAdder shoppingCartAdder   = new ShoppingCartAdder();
                string             defaultCurrencyName = Config.Get <EcommerceConfig>().DefaultCurrency;

                OptionsDetails optionsDetails = new OptionsDetails();

                decimal price = 0;
                if (!decimal.TryParse(DonationAmountDropDown.Value.ToString(), out price))
                {
                    price = Convert.ToDecimal(OtherAmountControl.Value);
                }

                this.Product.Price = price;
                shoppingCartAdder.AddItemToShoppingCart(this, this.OrdersManager, this.Product, optionsDetails, quantity, defaultCurrencyName);

                // Save the donation amount in custom field
                OrdersManager ordersManager = OrdersManager.GetManager();
                string        cookieKey     = EcommerceConstants.OrdersConstants.ShoppingCartIdCookieName;
                if (SystemManager.CurrentContext.IsMultisiteMode)
                {
                    cookieKey += SystemManager.CurrentContext.CurrentSite.Id;
                }

                HttpCookie shoppingCartCookie = HttpContext.Current.Request.Cookies[cookieKey];

                CartOrder cartOrder = null;
                if (shoppingCartCookie == null || !shoppingCartCookie.Value.IsGuid())
                {
                    // throw new ArgumentException("The shopping cart cookie does not exist or its value is not a valid string.");
                    cartOrder = ordersManager.CreateCartOrder();
                }
                else
                {
                    Guid cartOrderId = new Guid(shoppingCartCookie.Value);
                    cartOrder = ordersManager.GetCartOrder(cartOrderId);
                }

                if (cartOrder != null)
                {
                    PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(cartOrder);
                    PropertyDescriptor           property   = properties["DonationAmount"];

                    MetafieldPropertyDescriptor metaProperty = property as MetafieldPropertyDescriptor;

                    if (metaProperty != null)
                    {
                        metaProperty.SetValue(cartOrder, price);
                        ordersManager.SaveChanges();
                    }
                }

                this.AddedToCartMessage.ShowPositiveMessage("Donation added to cart");
            }
            catch (Exception ex)
            {
                this.AddedToCartMessage.ShowNegativeMessage("Failed to add donation to cart, try again");
            }
        }