Show item, total price and total quantity in cart bag icon
Question:
Except my cart page, another page can’t show item name, quantity and price on my web, this is my Project tree.
base.html
<div class="sinlge-bar shopping">
<a href="#" class="single-icon"><i class="ti-bag"></i>
<span class="total-count">{{cart.get_total_product}}</span></a>
<!-- Shopping Item -->
<div class="shopping-item">
<div class="dropdown-cart-header">
<span>{{cart.get_total_product}} Items</span>
<a href="{% url 'cart:cart_detail' %}">View Cart</a>
</div>
<ul class="shopping-list">
{% for item in cart %}
{% with product=item.product %}
<li>
<a href="#" class="remove" title="Remove this item"><i class="fa fa-remove"></i></a>
<a class="cart-img" href="{% url 'store:product' product.pk %}"><img src="{{product.image.url}}" alt="{{item.product.name}}" width="100" height="100"></a>
<h4><a href="{% url 'store:product' product.pk %}">{{item.product.name}}</a></h4>
<p class="quantity">
{{item.quantity}}x
<span class="amount">{{item.price|floatformat:0|intcomma}}</span>
</p>
</li>
{% endwith %}
{% endfor %}
</ul>
<div class="bottom">
<div class="total">
<span>Total</span>
<span class="total-amount">{{item.total_price|floatformat:0|intcomma}}</span>
</div>
<a href="{% url 'store:checkout' %}" class="btn animate">Checkout</a>
</div>
</div>
</div>
store/url.py
app_name = 'store'
urlpatterns = [
path ('',views.index, name='index'),
path ('index.html',views.index, name='index'),
path ('cart.html',views.cart_detail, name='cart'),
path ('search.html',views.search_form, name='search'),
path ('contact.html',views.contact, name='contact'),
path ('checkout.html',views.checkout, name='checkout'),#/<int:pk>/
path ('blog-single-sidebar.html',views.blog, name='blog'),
path ('shop-grid.html/<int:pk>/',views.shop, name='shop'),
path ('product.html/<int:pk>/',views.product_detail, name='product'),#/<int:pk>/
path ('filter_by_prices.html',views.filter_by_prices,name='filter_by_prices')
]
I try to add cart.py
to use function with code like this but not working
store/views.py
from cart.cart import Cart
def cart_detail(request):
cart = Cart(request)
return render(request, 'store:cart.html', {'cart': cart})
It looks like following, except cart page:
Answers:
Run the query like this:
def place_order(request, total=0, quantity=0,):
current_user = request.user
#if cartr count is less than or = 0 then redirect back to shop
cart_items = Cart_item.objects.filter(user=current_user)
cart_count = cart_items.count()
if cart_count <= 0 :
return redirect('store')
grand_total = 0
tax = 0
for cart_item in cart_items:
total += (cart_item.product.price * cart_item.quantity)
quantity += cart_item.quantity
tax = (2 * total)/100
context = {
'cart_items' :cart_items,
'total' :total,
'tax' :tax,
'grand_total' :grand_total,
}
and url pattern:
path('place_order/', views.place_order, name='place_order')
thanks you @Suhaib Illyas,
i fixed it, just need to add Cart = Cart(request)
and context {'cart': cart}
to another funtion in store.
Except my cart page, another page can’t show item name, quantity and price on my web, this is my Project tree.
base.html
<div class="sinlge-bar shopping">
<a href="#" class="single-icon"><i class="ti-bag"></i>
<span class="total-count">{{cart.get_total_product}}</span></a>
<!-- Shopping Item -->
<div class="shopping-item">
<div class="dropdown-cart-header">
<span>{{cart.get_total_product}} Items</span>
<a href="{% url 'cart:cart_detail' %}">View Cart</a>
</div>
<ul class="shopping-list">
{% for item in cart %}
{% with product=item.product %}
<li>
<a href="#" class="remove" title="Remove this item"><i class="fa fa-remove"></i></a>
<a class="cart-img" href="{% url 'store:product' product.pk %}"><img src="{{product.image.url}}" alt="{{item.product.name}}" width="100" height="100"></a>
<h4><a href="{% url 'store:product' product.pk %}">{{item.product.name}}</a></h4>
<p class="quantity">
{{item.quantity}}x
<span class="amount">{{item.price|floatformat:0|intcomma}}</span>
</p>
</li>
{% endwith %}
{% endfor %}
</ul>
<div class="bottom">
<div class="total">
<span>Total</span>
<span class="total-amount">{{item.total_price|floatformat:0|intcomma}}</span>
</div>
<a href="{% url 'store:checkout' %}" class="btn animate">Checkout</a>
</div>
</div>
</div>
store/url.py
app_name = 'store'
urlpatterns = [
path ('',views.index, name='index'),
path ('index.html',views.index, name='index'),
path ('cart.html',views.cart_detail, name='cart'),
path ('search.html',views.search_form, name='search'),
path ('contact.html',views.contact, name='contact'),
path ('checkout.html',views.checkout, name='checkout'),#/<int:pk>/
path ('blog-single-sidebar.html',views.blog, name='blog'),
path ('shop-grid.html/<int:pk>/',views.shop, name='shop'),
path ('product.html/<int:pk>/',views.product_detail, name='product'),#/<int:pk>/
path ('filter_by_prices.html',views.filter_by_prices,name='filter_by_prices')
]
I try to add cart.py
to use function with code like this but not working
store/views.py
from cart.cart import Cart
def cart_detail(request):
cart = Cart(request)
return render(request, 'store:cart.html', {'cart': cart})
It looks like following, except cart page:
Run the query like this:
def place_order(request, total=0, quantity=0,):
current_user = request.user
#if cartr count is less than or = 0 then redirect back to shop
cart_items = Cart_item.objects.filter(user=current_user)
cart_count = cart_items.count()
if cart_count <= 0 :
return redirect('store')
grand_total = 0
tax = 0
for cart_item in cart_items:
total += (cart_item.product.price * cart_item.quantity)
quantity += cart_item.quantity
tax = (2 * total)/100
context = {
'cart_items' :cart_items,
'total' :total,
'tax' :tax,
'grand_total' :grand_total,
}
and url pattern:
path('place_order/', views.place_order, name='place_order')
thanks you @Suhaib Illyas,
i fixed it, just need to add Cart = Cart(request)
and context {'cart': cart}
to another funtion in store.