private void AddEventToCart(Event currentEvent)
        {
            CatalogManager catalog = new CatalogManager();
            Product        product = catalog.GetProduct(ProductId);

            HttpCookie     cookie        = HttpContext.Current.Request.Cookies.Get("shoppingCartId");
            OrdersManager  orderm        = new OrdersManager();
            OptionsDetails optionDetails = new OptionsDetails();

            // Check if shopping cart cookie exists in the current request.
            if (cookie == null)                                               //if it does not exist...
            {
                CartOrder  cartItem       = orderm.CreateCartOrder();         //create a new cart order
                var        shoppingcartid = cartItem.Id;                      // that id is equal to the cookie value
                HttpCookie Cookie         = new HttpCookie("shoppingCartId"); //create a new shopping cart cookie
                DateTime   now            = DateTime.Now;                     // Set the cookie value.
                Cookie.Value   = shoppingcartid.ToString();                   // Set the cookie expiration date.
                Cookie.Expires = now.AddYears(1);                             // Add the cookie.
                HttpContext.Current.Response.Cookies.Add(Cookie);             //give cart item currency of USD because it cannot be null
                cartItem.Currency = "USD";                                    //add the product to the cart
                orderm.AddToCart(cartItem, product, optionDetails, 1);        //save all changes
                orderm.SaveChanges();
            }
            else //if the cookie does exist
            {
                Guid      guid     = new Guid(cookie.Value.ToString()); //get the cookie value as the guid
                CartOrder cartItem = orderm.GetCartOrder(guid);        //get the cart based on the cookie value
                orderm.AddToCart(cartItem, product, optionDetails, 1); // add the item to the cart
                orderm.SaveChanges();                                  //save changes
            }
        }
Exemple #2
0
        /// <summary>
        /// Handles the Command event of the CustomAddToCartButton control. Used to save custom order
        /// fields from checkout template inputs.
        /// </summary>
        ///
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="CommandEventArgs" /> instance containing the event data.</param>
        protected void CustomAddToCartButton_Command(object sender, CommandEventArgs e)
        {
            var cartId = this.GetShoppingCartId();
            var parent = Parent;

            OptionsDetails optionsDetails = GetOptionDetails();

            if (optionsDetails.IsProductVariationRequired && optionsDetails.ProductVariation == null)
            {
                AddedToCartMessage.ShowNegativeMessage(Res.Get <OrdersResources>().SelectFromAvailableOptions);
                return;
            }
            //FIND CONTROLS THAT MATCH CUSTOM ORDER FIELDS
            foreach (var field in OrderHelper.GetCustomFields())
            {
                //ITERATE THROUGH PRODUCT VIEW PAGE
                foreach (Control control in parent.Controls)
                {
                    //CHECK IF ID MATCHES FIELD NAME
                    if (control.ID == field)
                    {
                        string value = string.Empty;

                        //GET VALUE BASED ON TYPE IF APPLICABLE
                        if (control is ITextControl)
                        {
                            var temp = control as ITextControl;
                            value = temp.Text;
                        }
                        else if (control is ICheckBoxControl)
                        {
                            var temp = control as ICheckBoxControl;
                            value = temp.Checked.ToString();
                        }

                        //SAVE VALUE TO CUSTOM ORDER FIELD
                        OrderHelper.SaveCustomField(cartId, field, value);

                        //STOP SEARCHING SINCE CONTROL FOUND
                        break;
                    }
                }
            }

            //RESTORE REDIRECT PAGE ID
            AddToCartRedirectPageId = RedirectPageId;
            RedirectPageId          = null;

            //REDIRECT TO DESTINATION URL IF APPLICABLE
            if (AddToCartRedirectPageId.HasValue)
            {
                var    pageNode = PageManager.GetManager().GetPageNode(AddToCartRedirectPageId.Value);
                string pageNodeUrlForCurrentPageCulture = this.GetPageNodeUrlForCurrentPageCulture(pageNode);
                Page.Response.Redirect(pageNodeUrlForCurrentPageCulture);
            }
        }
        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");
            }
        }
Exemple #4
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");
            }
        }
        private void AddEventToCart(Event currentEvent)
        {
            CatalogManager catalog = new CatalogManager();
            Product product = catalog.GetProduct(ProductId);

            HttpCookie cookie = HttpContext.Current.Request.Cookies.Get("shoppingCartId");
            OrdersManager orderm = new OrdersManager();
            OptionsDetails optionDetails = new OptionsDetails();

            // Check if shopping cart cookie exists in the current request.
            if (cookie == null) //if it does not exist...
            {
                CartOrder cartItem = orderm.CreateCartOrder(); //create a new cart order
                var shoppingcartid = cartItem.Id;  // that id is equal to the cookie value
                HttpCookie Cookie = new HttpCookie("shoppingCartId");  //create a new shopping cart cookie
                DateTime now = DateTime.Now; // Set the cookie value.
                Cookie.Value = shoppingcartid.ToString(); // Set the cookie expiration date.
                Cookie.Expires = now.AddYears(1);// Add the cookie.
                HttpContext.Current.Response.Cookies.Add(Cookie);  //give cart item currency of USD because it cannot be null
                cartItem.Currency = "USD"; //add the product to the cart
                orderm.AddToCart(cartItem, product, optionDetails, 1); //save all changes
                orderm.SaveChanges();
            }
            else //if the cookie does exist
            {
                Guid guid = new Guid(cookie.Value.ToString()); //get the cookie value as the guid
                CartOrder cartItem = orderm.GetCartOrder(guid); //get the cart based on the cookie value
                orderm.AddToCart(cartItem, product, optionDetails, 1); // add the item to the cart
                orderm.SaveChanges(); //save changes
            }
        }