programmatically adding a product to the shopping cart

Posted by Community Admin on 04-Aug-2018 04:32

programmatically adding a product to the shopping cart

All Replies

Posted by Community Admin on 11-Aug-2016 00:00

We have a requirement to add a product and add that product to the cart using code. I can add the product to the list (I can see the added product in sitefinity administration), however when I add the product to the cart, I can't see it in the cart. There are no errors or exception messages. I have attached the sample code, which I took from internet.

 

        private Guid CreateNewProduct(string QuoteRef, decimal QuotePremium)
       
            try
           
                CatalogManager manager = CatalogManager.GetManager();
                EcommerceManager ecommerceManager = EcommerceManager.GetManager();
                if (manager.GetProducts().Where(t => t.Title == QuoteRef).SingleOrDefault() != null)
                    return Guid.Empty;     // Product already exists
                ProductType productType = ecommerceManager.GetProductTypes().Where(t => t.Title == "InsuranceQuote").SingleOrDefault();
                if (productType == null)
                    return Guid.Empty;     // Product Type does not exist
                Product product = manager.CreateProduct(productType.ClrType);
                product.ClrType = productType.ClrType;
                product.Title = QuoteRef;
                product.AssociateBuyerWithRole = Guid.Empty;
                product.DateCreated = DateTime.Now;
                product.Description = QuoteRef;
                product.IsShippable = false;
                product.Price = QuotePremium;
                product.Sku = "";
                product.UrlName = Regex.Replace(QuoteRef.ToLower(), @"[^\w\-\!\$\'\(\)\=\@\d_]+", "-");
                product.Visible = true;
                product.Status = Telerik.Sitefinity.GenericContent.Model.ContentLifecycleStatus.Master;
                manager.Provider.RecompileItemUrls(product);
                manager.SaveChanges();
                var contextBag = new Dictionary<string, string>();
                contextBag.Add("ContentType", product.GetType().FullName);
                string workflowOperation = "Publish";
                WorkflowManager.MessageWorkflow(product.Id,
                    product.GetType(),
                   "OpenAccessDataProvider",
                    workflowOperation,
                    false,
                    contextBag);
                return product.Id;
           
            catch (Exception e)
           
                Log.Write(e.ToString());
                return Guid.Empty;
           
       
private void AddToCart(Guid ProductId)
       
            CatalogManager catalog = CatalogManager.GetManager();
            //Product productMaster = catalog.GetProduct(ProductId);
            //Product product = catalog.Lifecycle.GetLive(productMaster) as Product;
            Product product = catalog.GetProduct(ProductId);
            HttpCookie cookie = HttpContext.Current.Request.Cookies.Get("shoppingCartId");
            OrdersManager orderm = OrdersManager.GetManager();
            if (cookie == null)
           
                CartOrder cartItem = orderm.CreateCartOrder();
                var shoppingcartid = cartItem.Id;
                HttpCookie Cookie = new HttpCookie("shoppingCartId");
                DateTime now = DateTime.Now;
                Cookie.Value = shoppingcartid.ToString();
                Cookie.Expires = now.AddYears(1);
                HttpContext.Current.Response.Cookies.Add(Cookie);
                cartItem.Currency = "LKR";
                OptionsDetails oD = new OptionsDetails();
                orderm.AddToCart(cartItem, product, oD, 1);
                orderm.SaveChanges();
           
            else
           
                Guid guid = new Guid(cookie.Value.ToString());
                CartOrder cartItem = orderm.GetCartOrder(guid);
                OptionsDetails oD = new OptionsDetails();
                orderm.AddToCart(cartItem, product, oD, 1);
                orderm.SaveChanges();
           
       

This thread is closed