Shopping Cart Implementation in ASP.NET MVC

1

I'm trying to build a "simple" sales system, however, I do not have any knowledge in e-commerce. What I want is the following:

The user chooses the products he wants, assembles a cart, and completes the order. Then my question starts: the cart is half finished, I can add the items and the quantity desired, but I do not know how to do the calculation to display the total value.

Follow my Action :

public ActionResult AdicionarCarrinho(int id)
        {
            List<ItemDesign> carrinho = Session["Carrinho"] != null ? (List<ItemDesign>)Session["Carrinho"] : new List<ItemDesign>();

            Design d = db.Design.Find(id);

            if (d != null)
            {
                ItemDesign i = new ItemDesign();
                i.Design = d;
                i.Qtd = 1;
                i.IdDesign = d.IdDesign;

                if (carrinho.Where(x => x.IdDesign == d.IdDesign).FirstOrDefault() != null)
                {
                    carrinho.Where(x => x.IdDesign == d.IdDesign).FirstOrDefault().Qtd += 1;
                }

                else
                {
                    carrinho.Add(i);


                }
                Session["Carrinho"] = carrinho;

            }

            return RedirectToAction("Carrinho");

        }



public ActionResult Carrinho()
    {
        List<ItemDesign> carrinho = Session["Carrinho"] != null ? (List<ItemDesign>)Session["Carrinho"] : new List<ItemDesign>();

        return View(carrinho);
    }

My first question is: How can I do to calculate total value according to cart items?

Second question: What to do after finishing the cart? What I was trying to do was more or less the following:

The user ends the order, and opens a payment screen (ticket only). He would type his data (name and cpf, I do not know) and the system would generate a "pseudo-ticket" with the buyer's data and the items he requested. It does not have to be a real ticket, because I do not really want to implement it.

Here are my tables:

Design:
IdDesign
Servico
Preco

ItemDesign:
Id
IdDesign
IdPedido

Pedido:
IdPedido
DtPedido
StatusPedido
IdUsuario
DtPagamento
ValorTotal

I accept suggestion of any tutorial / book on E-commerce.

Edit: View of Cart

@model List<AllFiction.Models.ItemDesign>     


@{
    ViewBag.Title = "Carrinho";
}

<h2>Carrinho</h2>


    @foreach (var item in Model)

    {

    <li>
        @item.IdDesign - @item.Design.Servico

        <input type="text" value="@item.Qtd" readonly="readonly"/>
        <input type="submit" value="alterar" />
        @Html.ActionLink("Remover", "Remover", "Shop", new {id=item.IdDesign}, null)
    </li>   


    }
@Html.ActionLink("Retornar", "Servicos", "Shop")
    
asked by anonymous 18.11.2014 / 06:53

1 answer

2

I'm assuming your Models looks like this:

public class Design 
{
    [Key]
    public int DesignId { get; set; }
    public String Servico { get; set; }
    public Decimal Preco { get; set; }

    public virtual ICollection<ItemDesign> ItensDesign { get; set; }
}

public class ItemDesign 
{
    [Key]
    public int ItemDesignId { get; set; }
    public int DesignId { get; set; }
    public int PedidoId { get; set; }

    public virtual Design Design { get; set; }
    public virtual Pedido Pedido { get; set; }
}

public class Pedido 
{
    [Key]
    public int PedidoId { get; set; }
    public int UsuarioId { get; set; }
    public DateTime DtPedido { get; set; }
    public StatusPedido StatusPedido { get; set; } // StatusPedido seria um Enum

    public DateTime DtPagamento { get; set; }
    public Decimal ValorTotal { get; set; }

    public virtual Usuario Usuario { get; set; }
    public virtual ICollection<ItemDesign> ItensDesign { get; set; }
}

There is not much secret. You've done everything right so far. I'm just going to change some things to make the calculation easier:

    public ActionResult AdicionarCarrinho(int id)
    {
        // Ao invés de colocar uma lista de ítens de Design, vamos colocar
        // Um objeto da entidade Pedido, que já possui List<ItemDesign>.
        // List<ItemDesign> carrinho = Session["Carrinho"] != null ? (List<ItemDesign>)Session["Carrinho"] : new List<ItemDesign>();
        Pedido carrinho = Session["Carrinho"] != null ? (Pedido)Session["Carrinho"] : new Pedido();

        var design = db.Design.Find(id);

        if (design != null)
        {
            var itemDesign = new ItemDesign();
            itemDesign.Design = design;
            itemDesign.Qtd = 1;
            // Esta linha não precisa. O EF é espertinho e preenche pra você.
            // itemDesign.IdDesign = design.IdDesign;

            if (carrinho.ItensDesign.FirstOrDefault(x => x.IdDesign == design.IdDesign) != null)
            {
                carrinho.ItensDesign.FirstOrDefault(x => x.IdDesign == design.IdDesign).Qtd += 1;
            }

            else
            {
                carrinho.ItensDesign.Add(itemDesign);
            }

            // Aqui podemos fazer o cálculo do valor

            carrinho.ValorTotal = carrinho.ItensDesign.Select(i => i.Design).Sum(d => d.Preco);

            Session["Carrinho"] = carrinho;
        }

        return RedirectToAction("Carrinho");
    }

    public ActionResult Carrinho()
    {
        Pedido carrinho = Session["Carrinho"] != null ? (Pedido)Session["Carrinho"] : new Pedido();

        return View(carrinho);
    }

View would look like this:

@model Pedido

@{
    ViewBag.Title = "Carrinho";
}

<h2>Carrinho</h2>

    @foreach (var item in Model.ItensDesign)
    {
        <li>
            @item.IdDesign - @item.Design.Servico

            <input type="text" value="@item.Qtd" readonly="readonly"/>
            <input type="submit" value="alterar" />
            @Html.ActionLink("Remover", "Remover", "Shop", new {id=item.IdDesign}, null)
        </li>
    }

@Html.ActionLink("Retornar", "Servicos", "Shop")
    
18.11.2014 / 19:21