Cart template don't handle empty carts correctly

Issue #1158 resolved
Rodrigo Machado
created an issue

The template satchmo/apps/satchmo_store/shop/templates/shop/cart.html always show the items table, even when the cart is empty.

If the line is changed from

{{{

!python

{% if cart.is_empty %} }}}

to

{{{

!python

{% if not cart.numItems %} }}}

the error message is show.

Comments (4)

  1. Tay Ray Chuan
    • changed status to open

    This is what I'm proposing:

    diff --git a/satchmo/apps/satchmo_store/shop/models.py b/satchmo/apps/satchmo_store/shop/models.py
    index 70f649d..ab92255 100644
    --- a/satchmo/apps/satchmo_store/shop/models.py
    +++ b/satchmo/apps/satchmo_store/shop/models.py
    @@ -321,7 +321,8 @@ class Cart(models.Model):
             carts even if they have no items. This is ok because the most likely
             scenario is moving data from one db to the next. See ticket #1015 for
             discussion.
    -        Use cart.is_empty if you want to know if qty >= 1
    +
    +        Use len(cart) if you want to know if there are items in the cart.
             """
             return True
    
    diff --git a/satchmo/apps/satchmo_store/shop/templates/shop/cart.html b/satchmo/apps/satchmo_store/shop/templates/shop/cart.html
    index 8c76fcf..19b6de4 100644
    --- a/satchmo/apps/satchmo_store/shop/templates/shop/cart.html
    +++ b/satchmo/apps/satchmo_store/shop/templates/shop/cart.html
    @@ -17,7 +17,7 @@
     {% if error_message %}
     <div class="error">{{ error_message }}</div>
     {% endif %}
    -{% if cart.is_empty %}
    +{% if not cart|length %}
         <h4>{% trans "Your cart is empty." %}</h4>
     {% else %}
         <h4>{% trans "Your Cart" %}</h4>
    

    This patch would make Cart.is_empty redundant, and I think it should be removed.

  2. Tay Ray Chuan

    This is what I'm proposing:

    diff --git a/satchmo/apps/satchmo_store/shop/models.py b/satchmo/apps/satchmo_store/shop/models.py
    index 70f649d..ab92255 100644
    --- a/satchmo/apps/satchmo_store/shop/models.py
    +++ b/satchmo/apps/satchmo_store/shop/models.py
    @@ -321,7 +321,8 @@ class Cart(models.Model):
             carts even if they have no items. This is ok because the most likely
             scenario is moving data from one db to the next. See ticket #1015 for
             discussion.
    -        Use cart.is_empty if you want to know if qty >= 1
    +
    +        Use len(cart) if you want to know if there are items in the cart.
             """
             return True
    
    diff --git a/satchmo/apps/satchmo_store/shop/templates/shop/cart.html b/satchmo/apps/satchmo_store/shop/templates/shop/cart.html
    index 8c76fcf..19b6de4 100644
    --- a/satchmo/apps/satchmo_store/shop/templates/shop/cart.html
    +++ b/satchmo/apps/satchmo_store/shop/templates/shop/cart.html
    @@ -17,7 +17,7 @@
     {% if error_message %}
     <div class="error">{{ error_message }}</div>
     {% endif %}
    -{% if cart.is_empty %}
    +{% if not cart|length %}
         <h4>{% trans "Your cart is empty." %}</h4>
     {% else %}
         <h4>{% trans "Your Cart" %}</h4>
    

    This patch would make Cart.is_empty redundant, and I think it should be removed.

  3. Tay Ray Chuan

    cart.html: use {% if not cart|length %}

    This references #1015, which introduces Cart.is_empty(). However, NullCarts don't implement this, so the cart template incorrectly displays the non-empty cart portion.

    Instead of defining is_empty() on NullCart, we instead use cart|length in the template.

    This fixes #1158.

    Note: the {% if not foo|length %} syntax works since django 1.0.

    56c341bb15e1

  4. Log in to comment