Aula 47 - Loja Online - Django - Remover itens do carrinho

Se gostarem do conteúdo dêem um joinha 👍 na página do Código Fluente no Facebook

Esse é o link do código fluente no Pinterest

Meus links de afiliados:

Hostinger

Digital Ocean

One.com

Melhore seu NETWORKING

https://digitalinnovation.one/

Participe de comunidades de desenvolvedores:

Fiquem a vontade para me adicionar ao linkedin.

E também para me seguir no https://github.com/toticavalcanti.

Código final da aula:

https://github.com/toticavalcanti

Toti:

https://www.youtube.com/channel/UCUEtjLuDpcOvR3mIUr-viOA

Backing track / Play-along:

https://www.youtube.com/channel/UCT3TryVMqTqYBjf5g5WAHfA

Código Fluente

https://www.youtube.com/channel/UCgn-O-88XBAwdG9gUWkkb0w

Putz!

https://www.youtube.com/channel/UCZXop2-CECwyFYmHbhnAkAw

Aula 47 - Loja Online - Django - Remover itens do carrinho

Nessa aula vamos implementar o remover do carrinho. Iremos mexer em dois arquivos, o e_commerce/carts/templates/carts/home.html e o e_commerce/products/templates/products/snippets/update-cart.html.

Vamos fazer as modificações destacadas em azul nos códigos abaixo.

e_commerce/carts/templates/carts/home.html


{% extends "base.html" %}
{% block content %}
  <h1>Cart</h1>
  {% if cart.products.exists %}
  <table class="table">
    <thead>
      <tr>
        <th>#</th>
        <th>Nome</th>
        <th>Preço/th>
      </tr>
    </thead>
    <tbody>
      {% for product in cart.products.all %}
      <tr>
        <th scope="row">{{ forloop.counter }}</th>
        <td>
          <a href='{{ product.get_absolute_url }}'>{{ product.title }}</a>
          {% include 'products/snippets/update-cart.html' with product=product cart=cart in_cart=True %}
        </td>
        <td>{{ product.price }}</td>
      </tr>
      {% endfor %}
      <tr>
        <td colspan="2"></td>
        <td><b>Subtotal</b> {{ cart.subtotal }}</td>
      </tr>
      <tr>
        <td colspan="2"></td>
        <td><b>Total</b> {{ cart.total }}</td>
      </tr>
    </tbody>
  </table>
  {% else %}
    <p class='lead'>Carrinho vazio</p>
  {% endif %}
{% endblock %}
Agora modifique o update-cart.html

e_commerce/products/templates/products/snippets/update-cart.html


<form method='POST' action='{% url "cart:update" %}' class="form"> {% csrf_token %}
  <input type='hidden' name='product_id' value='{{ product.id }}' />
  {% if in_cart %}
    <button type='submit' class='btn btn-link btn-sm' style="padding:0px;cursor: pointer;">Excluir</button>
  {% else %}
    {% if product in cart.products.all %}
      No carrinho <button type='submit' class='btn btn-link'>Excluir</button>
    {% else %}
      <button type='submit' class='btn btn-success'>Adicionar</button>
    {% endif %}
  {% endif %}
</form>

Agora acesse http://127.0.0.1:8000/products/

Adicione alguns produtos e depois remova do carrinho para testar.

Por essa aula é só, nos vemos na próxima. :)

Voltar para página principal do blog

Código final da aula:

https://github.com/toticavalcanti

Outros canais

Toti:

https://www.youtube.com/channel/UCUEtjLuDpcOvR3mIUr-viOA

Backing track / Play-along:

https://www.youtube.com/channel/UCT3TryVMqTqYBjf5g5WAHfA

Código Fluente

https://www.youtube.com/channel/UCgn-O-88XBAwdG9gUWkkb0w

Putz!

https://www.youtube.com/channel/UCZXop2-CECwyFYmHbhnAkAw

Se gostarem do conteúdo dêem um joinha 👍 na página do Código Fluente no Facebook

Esse é o link do código fluente no Pinterest

Meus links de afiliados:

Hostinger

Digital Ocean

One.com

É isso, ficamos por aqui \o/ e até a próxima 😉