Exemplo n.º 1
0
        /// <summary>
        /// Creates a new cart for the supplied customer with the supplied name.
        /// This method does some additional things beyond ECF's cart creation code,
        /// so it's very importand that NWTD carts are created this way.
        /// </summary>
        /// <param name="Account"></param>
        /// <param name="CartName"></param>
        /// <returns></returns>
        public static Mediachase.Commerce.Orders.Cart CreateCart(Account Account, String CartName)
        {
            if (Mediachase.Commerce.Orders.Cart.LoadByCustomerAndName(Account.PrincipalId, CartName) != null)
            {
                throw new Exception("A cart for this customer with this name already exists");
            }
            //create the cart
            Mediachase.Commerce.Orders.Cart cartToAdd = Mediachase.Commerce.Orders.OrderContext.Current.GetCart(CartName, Account.PrincipalId);
            cartToAdd.CustomerName = Mediachase.Commerce.Profile.Account.LoadByPrincipalId(Account.PrincipalId).Name;
            cartToAdd.OrderForms.Add(new Mediachase.Commerce.Orders.OrderForm()
            {
                Name = CartName
            });                                                                                                   //We need to give it a name. So we can call the GetOrderForm mehod on the ECF's CartHelper class and actually return somethiung
            cartToAdd.Status = NWTD.Orders.Cart.CART_STATUS.OPEN.ToString();
            cartToAdd.AcceptChanges();

            return(cartToAdd);
        }
Exemplo n.º 2
0
        [WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.WrappedRequest)]         //the entire parameter must be serialized to a string (no individual raw POST values)
        public CartResponse Copy(string cartName, string newName, bool activate)
        {
            var response = new CartResponse();             //the response for the operation

            if (string.IsNullOrEmpty(newName))
            {
                newName = string.Format("Copy of {0}", cartName);
            }

            Guid userId = ProfileContext.Current.UserId;

            //make sure there isn't already a cart with the same name
            if (!CartNameIsValid(newName))
            {
                response.Message = "Invalid Wish List name! The Wish List name may only contain letters, numbers, spaces, and underscores";
                response.Status  = CartResponseStatus.ERROR;
            }
            else if (CartNameIsDuplicate(userId, newName))
            {
                response.Message = "Invalid Wish List name! There is already a Wish List with this name";
                response.Status  = CartResponseStatus.ERROR;
            }
            else
            {
                CartHelper originalCartHelper = new CartHelper(cartName, userId);

                //create the new cart
                Mediachase.Commerce.Orders.Cart cartToAdd = NWTD.Orders.Cart.CreateCart(ProfileContext.Current.Profile.Account, newName);

                //now, we'll need a CartHelper to start adding the lines
                CartHelper newCartHelper = new CartHelper(cartToAdd);

                //now add all the same line items to the new cart, copying any relevant metadata (gratis and quantity)
                foreach (LineItem lineItem in originalCartHelper.LineItems)
                {
                    //get the entry
                    Entry entry = CatalogContext.Current.GetCatalogEntry(lineItem.CatalogEntryId, new Mediachase.Commerce.Catalog.Managers.CatalogEntryResponseGroup(CatalogEntryResponseGroup.ResponseGroup.CatalogEntryFull));
                    if (entry != null)
                    {
                        newCartHelper.AddEntry(entry, lineItem.Quantity);
                    }

                    //get the item we just added and set its gratis
                    LineItem addedItem = newCartHelper.LineItems.Single(li => li.CatalogEntryId == entry.ID);
                    addedItem["Gratis"] = lineItem["Gratis"];
                    //addedItem.ShippingAddressId = lineItem.ShippingAddressId;
                    newCartHelper.RunWorkflow("CartValidate");
                    cartToAdd.AcceptChanges();
                }

                //save the changes
                cartToAdd.AcceptChanges();

                if (activate)
                {
                    NWTD.Profile.ActiveCart = cartToAdd.Name;
                    response.Message        = "Wish List succesfully copied and made active";
                }
                else
                {
                    response.Message = "Wish List succesfully copied";
                }
            }
            return(response);
        }