Skip to content

Saleor Core: the high performance, composable, headless commerce API.

Clarify product variable naming in cart

Krzysztof Wolski1556531c3859
diff --git a/saleor/cart/forms.py b/saleor/cart/forms.py
index 74a6722e3d..8611ef40e3 100644
--- a/saleor/cart/forms.py
+++ b/saleor/cart/forms.py
@@ -69,7 +69,7 @@ class AddToCartForm(forms.Form):
def save(self):
"""Adds the selected product variant and quantity to the cart"""
product_variant = self.get_variant(self.cleaned_data)
- return self.cart.add(product=product_variant,
+ return self.cart.add(variant=product_variant,
quantity=self.cleaned_data['quantity'])
def get_variant(self, cleaned_data):
@@ -82,15 +82,17 @@ class ReplaceCartLineForm(AddToCartForm):
Similar to AddToCartForm but its save method replaces the quantity.
"""
def __init__(self, *args, **kwargs):
+ self.variant = kwargs.pop('variant')
+ kwargs['product'] = self.variant.product
super(ReplaceCartLineForm, self).__init__(*args, **kwargs)
- self.cart_line = self.cart.get_line(self.product)
+ self.cart_line = self.cart.get_line(self.variant)
self.fields['quantity'].widget.attrs = {
- 'min': 1, 'max': self.product.get_stock_quantity()}
+ 'min': 1, 'max': self.variant.get_stock_quantity()}
def clean_quantity(self):
quantity = self.cleaned_data['quantity']
try:
- self.product.check_quantity(quantity)
+ self.variant.check_quantity(quantity)
except InsufficientStock as e:
msg = self.error_messages['insufficient-stock']
raise forms.ValidationError(msg % {
@@ -103,8 +105,7 @@ class ReplaceCartLineForm(AddToCartForm):
return super(AddToCartForm, self).clean()
def get_variant(self, cleaned_data):
- """In cart formsets product is already the variant we want"""
- return self.product
+ return self.variant
def save(self):
"""Replaces the selected product's quantity in cart"""
diff --git a/saleor/cart/models.py b/saleor/cart/models.py
index c6452cb4fd..cddf2aac58 100644
--- a/saleor/cart/models.py
+++ b/saleor/cart/models.py
@@ -155,20 +155,20 @@ class Cart(models.Model, ItemSet):
def clear(self):
self.delete()
- def create_line(self, product, quantity, data):
- line = self.lines.create(product=product, quantity=quantity, data=data)
+ def create_line(self, variant, quantity, data):
+ line = self.lines.create(variant=variant, quantity=quantity, data=data)
return line
- def get_line(self, product, data=None):
+ def get_line(self, variant, data=None):
try:
- return self.lines.get(product=product)
+ return self.lines.get(variant=variant)
except CartLine.DoesNotExist:
return None
- def add(self, product, quantity=1, data=None, replace=False,
+ def add(self, variant, quantity=1, data=None, replace=False,
check_quantity=True):
cart_line, created = self.lines.get_or_create(
- product=product, defaults={'quantity': 0, 'data': data})
+ variant=variant, defaults={'quantity': 0, 'data': data})
if replace:
new_quantity = quantity
else:
@@ -179,7 +179,7 @@ class Cart(models.Model, ItemSet):
quantity, new_quantity))
if check_quantity:
- product.check_quantity(new_quantity)
+ variant.check_quantity(new_quantity)
cart_line.quantity = new_quantity
@@ -199,7 +199,7 @@ class Cart(models.Model, ItemSet):
class CartLine(models.Model, ItemLine):
cart = models.ForeignKey(Cart, related_name='lines')
- product = models.ForeignKey(
+ variant = models.ForeignKey(
ProductVariant, related_name='+',
verbose_name=pgettext_lazy('Cart line', 'product'))
quantity = models.PositiveIntegerField(
@@ -209,17 +209,17 @@ class CartLine(models.Model, ItemLine):
class Meta:
- unique_together = ('cart', 'product', 'data')
+ unique_together = ('cart', 'variant', 'data')
def __str__(self):
- return smart_str(self.product)
+ return smart_str(self.variant)
# Satchless
def __eq__(self, other):
if not isinstance(other, CartLine):
return NotImplemented
- return (self.product == other.product and
+ return (self.variant == other.variant and
self.quantity == other.quantity and
self.data == other.data)
@@ -227,14 +227,14 @@ class CartLine(models.Model, ItemLine):
return not self == other # pragma: no cover
def __repr__(self):
- return 'CartLine(product=%r, quantity=%r, data=%r)' % (
- self.product, self.quantity, self.data)
+ return 'CartLine(variant=%r, quantity=%r, data=%r)' % (
+ self.variant, self.quantity, self.data)
def __getstate__(self):
- return self.product, self.quantity, self.data
+ return self.variant, self.quantity, self.data
def __setstate__(self, data):
- self.product, self.quantity, self.data = data
+ self.variant, self.quantity, self.data = data
def get_total(self, **kwargs):
amount = super(CartLine, self).get_total(**kwargs)
@@ -244,7 +244,7 @@ class CartLine(models.Model, ItemLine):
return self.quantity
def get_price_per_item(self, discounts=None, **kwargs):
- return self.product.get_price_per_item(discounts=discounts, **kwargs)
+ return self.variant.get_price_per_item(discounts=discounts, **kwargs)
def is_shipping_required(self):
- return self.product.is_shipping_required()
+ return self.variant.is_shipping_required()
diff --git a/saleor/cart/utils.py b/saleor/cart/utils.py
index bc24a66b58..640c850a00 100644
--- a/saleor/cart/utils.py
+++ b/saleor/cart/utils.py
@@ -5,33 +5,33 @@ from django.utils.translation import ugettext_lazy as _
from satchless.item import InsufficientStock
-def contains_unavailable_products(cart):
+def contains_unavailable_variants(cart):
try:
- [item.product.check_quantity(item.quantity) for item in cart]
+ [line.variant.check_quantity(line.quantity) for line in cart]
except InsufficientStock:
return True
return False
-def remove_unavailable_products(cart):
- for item in cart:
+def remove_unavailable_variants(cart):
+ for line in cart:
try:
- cart.add(item.product, quantity=item.quantity, replace=True)
+ cart.add(line.variant, quantity=line.quantity, replace=True)
except InsufficientStock as e:
quantity = e.item.get_stock_quantity()
- cart.add(item.product, quantity=quantity, replace=True)
+ cart.add(line.variant, quantity=quantity, replace=True)
def get_product_variants_and_prices(cart, product):
lines = (cart_line for cart_line in cart
- if cart_line.product.product_id == product.id)
+ if cart_line.variant.product_id == product.id)
for line in lines:
for i in range(line.quantity):
- yield line.product, line.get_price_per_item()
+ yield line.variant, line.get_price_per_item()
def get_category_variants_and_prices(cart, discounted_category):
- products = set((cart_line.product.product for cart_line in cart))
+ products = set((cart_line.variant.product for cart_line in cart))
discounted_products = []
for product in products:
for category in product.categories.all():
@@ -45,8 +45,8 @@ def get_category_variants_and_prices(cart, discounted_category):
def check_product_availability_and_warn(request, cart):
- if contains_unavailable_products(cart):
+ if contains_unavailable_variants(cart):
msg = _('Sorry. We don\'t have that many items in stock. '
'Quantity was set to maximum available for now.')
messages.warning(request, msg)
- remove_unavailable_products(cart)
+ remove_unavailable_variants(cart)
diff --git a/saleor/cart/views.py b/saleor/cart/views.py
index 858f73a6da..7854dbd4a6 100644
--- a/saleor/cart/views.py
+++ b/saleor/cart/views.py
@@ -17,17 +17,17 @@ from ..product.models import Product, ProductVariant
@get_or_empty_db_cart
def index(request, cart):
- cart_lines = cart.lines.select_related('product')
+ cart_lines = cart.lines.select_related('variant')
check_product_availability_and_warn(request, cart_lines)
discounts = request.discounts
cart_lines = []
for line in cart:
initial = {'quantity': line.get_quantity()}
- form = ReplaceCartLineForm(None, cart=cart, product=line.product,
+ form = ReplaceCartLineForm(None, cart=cart, variant=line.variant,
initial=initial, discounts=discounts)
cart_lines.append({
- 'product': line.product,
+ 'variant': line.variant,
'get_price_per_item': line.get_price_per_item(discounts),
'get_total': line.get_total(discounts=discounts),
'form': form})
@@ -63,17 +63,17 @@ def add_to_cart(request, cart, product_id):
def update(request, cart, product_id):
if not request.is_ajax():
return redirect('cart:index')
- product = get_object_or_404(ProductVariant, pk=product_id)
+ variant = get_object_or_404(ProductVariant, pk=product_id)
discounts = request.discounts
status = None
- form = ReplaceCartLineForm(request.POST, cart=cart, product=product,
+ form = ReplaceCartLineForm(request.POST, cart=cart, variant=variant,
discounts=discounts)
if form.is_valid():
form.save()
response = {'productId': product_id,
'subtotal': 0,
'total': 0}
- updated_line = cart.get_line(form.cart_line.product)
+ updated_line = cart.get_line(form.cart_line.variant)
if updated_line:
response['subtotal'] = currencyfmt(
updated_line.get_total(discounts=discounts).gross,
diff --git a/saleor/checkout/test_checkout_integration.py b/saleor/checkout/test_checkout_integration.py
index da79dd8635..97eba89494 100644
--- a/saleor/checkout/test_checkout_integration.py
+++ b/saleor/checkout/test_checkout_integration.py
@@ -45,7 +45,7 @@ def test_checkout_flow(product, variant, client):
cart_page = client.get(urls['cart'])
cart_lines = cart_page.context['cart_lines']
assert len(cart_lines) == cart.lines.count()
- assert cart_lines[0]['product'] == variant
+ assert cart_lines[0]['variant'] == variant
# Enter checkout
checkout_index = client.get(urls['checkout_index'])
# Checkout index redirects directly to shipping address step
diff --git a/templates/cart/index.html b/templates/cart/index.html
index 55488cc3f1..39d954f791 100644
--- a/templates/cart/index.html
+++ b/templates/cart/index.html
@@ -44,22 +44,22 @@
{% for line in cart_lines %}
<tr class="cart-item">
<td class="hidden-xs">
- <a href="{{ line.product.get_absolute_url }}">
- {% if line.product.product.images.first %}
- <img class="cart-item-image" itemprop="image" src="{{ line.product.product.images.first.image.crop.100x100 }}" alt="">
+ <a href="{{ line.variant.get_absolute_url }}">
+ {% if line.variant.product.images.first %}
+ <img class="cart-item-image" itemprop="image" src="{{ line.variant.product.images.first.image.crop.100x100 }}" alt="">
{% else %}
<img class="cart-item-image" src="{% static 'images/product-image-placeholder-small.png' %}" alt="">
{% endif %}
</a>
</td>
<td>
- <a href="{{ line.product.get_absolute_url }}">
- {{ line.product.product }} - {{ line.product.display_variant }}
+ <a href="{{ line.variant.get_absolute_url }}">
+ {{ line.variant.product }} - {{ line.variant.display_variant }}
</a>
</td>
<td>
<div class="cart-item-amount">
- <form role="form" action="{% url "cart:update-line" product_id=line.product.pk %}" method="post" class="form-cart">
+ <form role="form" action="{% url "cart:update-line" product_id=line.variant.pk %}" method="post" class="form-cart">
<div class="input-group cart-item-quantity js-hidden{% if form.quantity.errors %} has-error{% endif %}">
{{ line.form.quantity }}
<span class="input-group-btn">
@@ -80,7 +80,7 @@
</div>
</td>
<td class="text-right hidden-xs">{% gross line.get_price_per_item %}</td>
- <td class="text-right cart-item-subtotal" data-product-id="{{ line.product.pk }}">{% gross line.get_total %}</td>
+ <td class="text-right cart-item-subtotal" data-product-id="{{ line.variant.pk }}">{% gross line.get_total %}</td>
</tr>
{% endfor %}
</tbody>
diff --git a/templates/checkout/details.html b/templates/checkout/details.html
index c9564a4abe..1c5f769f59 100644
--- a/templates/checkout/details.html
+++ b/templates/checkout/details.html
@@ -68,9 +68,9 @@
{% for item, item_price_per_item, item_price_total in delivery %}
<tr>
<td class="noborder">
- {{ item.product.product }}{% if item.quantity != 1 %} <span class="badge">×{{ item.quantity }}</span>{% endif %}
+ {{ item.variant.product }}{% if item.quantity != 1 %} <span class="badge">×{{ item.quantity }}</span>{% endif %}
<br>
- <small>{{ item.product }}</small>
+ <small>{{ item.variant }}</small>
</td>
<td class="noborder text-right">{% net item_price_total %}</td>
</tr>