46.Django(장고) - ecommerce 프로젝트 - 쇼핑몰 - 장바구니 가격 계산

JungSik Heo·2024년 12월 9일

1. templates\cart\cart_summary.html

아래의 코드 추가

  {% endfor %}
  <h3 class="text-center">Total: {{ totals }}</h3>
  <br />
  <br />
  <br />
  {% else %} There's Nothing in your cart {% endif %}

cart\views.py

def cart_summary(request):
    (... 생략 ...)
    totals = cart.cart_total()
    
    return render(request, "cart/cart_summary.html",{"cart_products": cart_products,"quantities": quantities,"totals": totals})

2. cart\cart.py

    def cart_total(self):
        product_ids = self.cart.keys()
        products = Product.objects.filter(id__in=product_ids)

        quantities = self.cart

        total = 0

        for key, value in quantities.items():
            #Convert key string into so we 
            key = int(key)

            for product in products:
                if product.id  == key:
                    if product.is_sale:
                        total = total + (product.sale_price * value)
                    else:
                        total = total + (product.price * value)

        return total   

아래와 같이 나오는지 확인

profile
쿵스보이(얼짱뮤지션)

0개의 댓글