More in this section
Forums / Developing with Sitefinity / programmatically adding a product to the shopping cart

programmatically adding a product to the shopping cart

The forums are in read-only mode. In case that you want to directly contact the Progress Sitefinity team use the support center. In our Google Plus group you can find more than one thousand Sitefinity developers discussing different topics. For the Stack Overflow threads don’t forget to use the “Sitefinity” tag.
1 posts, 0 answered
  1. Nuwan
    Nuwan avatar
    1 posts
    16 Mar 2016
    11 Aug 2016
    Link to this post

    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)
                    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;
                    var contextBag = new Dictionary<string, string>();
                    contextBag.Add("ContentType", product.GetType().FullName);
                    string workflowOperation = "Publish";
                    return product.Id;
                catch (Exception e)
                    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);
                    cartItem.Currency = "LKR";
                    OptionsDetails oD = new OptionsDetails();
                    orderm.AddToCart(cartItem, product, oD, 1);
                    Guid guid = new Guid(cookie.Value.ToString());
                    CartOrder cartItem = orderm.GetCartOrder(guid);
                    OptionsDetails oD = new OptionsDetails();
                    orderm.AddToCart(cartItem, product, oD, 1);

1 posts, 0 answered