Skip to content

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

Return Price object instead of number

Artur Smęt6cdbc775fa2a
diff --git a/saleor/cart/__init__.py b/saleor/cart/__init__.py
index 3c6e7de671..5ac12fe82b 100755
--- a/saleor/cart/__init__.py
+++ b/saleor/cart/__init__.py
@@ -2,9 +2,11 @@ from __future__ import unicode_literals
from django.utils.translation import pgettext
from django.utils.encoding import python_2_unicode_compatible, smart_text
+from django.conf import settings
from satchless import cart
from satchless.item import ItemList
from saleor.product.models import Product
+from prices import Price
CART_SESSION_KEY = 'cart'
@@ -50,8 +52,7 @@ class Cart(cart.Cart):
'product_id': variant.product.pk,
'variant_id': variant.pk,
'unit_price_gross': str(variant_price.gross),
- 'unit_price_net': str(variant_price.net),
- }
+ 'unit_price_net': str(variant_price.net)}
return variant_data
def add(self, product, quantity=1, data=None, replace=False,
@@ -68,8 +69,11 @@ class Cart(cart.Cart):
class SessionCartLine(cart.CartLine):
+
def get_price_per_item(self, **kwargs):
- return self.data.get('unit_price')
+ gross = self.data.get('unit_price_gross')
+ net = self.data.get('unit_price_net')
+ return Price(net=net, gross=gross, currency=settings.DEFAULT_CURRENCY)
def for_storage(self):
return {
diff --git a/saleor/cart/tests.py b/saleor/cart/tests.py
index d83478dbc2..d38e37b592 100755
--- a/saleor/cart/tests.py
+++ b/saleor/cart/tests.py
@@ -32,15 +32,14 @@ class BigShipCartForm(AddToCartForm):
return self.product
stock_product = BigShip(name='BigShip',
- stock=10, price=Price(10, currency='USD'), weight=Decimal(123))
+ stock=10, price=Price(10, currency='USD'),
+ weight=Decimal(123))
stock_product.product = stock_product
digital_product = ShipPhoto(price=Price(10, currency='USD'))
digital_product.product = digital_product
class CartTest(TestCase):
- def setUp(self):
- self.session_cart = SessionCart()
def test_check_quantity(self):
"""
@@ -55,7 +54,7 @@ class CartTest(TestCase):
self.assertFalse(cart)
def test_add_adds_to_session_cart(self):
- cart = Cart(session_cart=self.session_cart)
+ cart = Cart(session_cart=SessionCart())
cart.add(stock_product, 10)
self.assertEqual(cart.session_cart.count(), 10)
self.assertTrue(cart.session_cart.modified)
@@ -166,3 +165,14 @@ class ReplaceCartLineFormSetTest(TestCase):
product_quantity = cart.get_line(stock_product).quantity
self.assertEqual(product_quantity, 5,
'%s is the bad quantity value' % (product_quantity,))
+
+
+class SessionCartTest(TestCase):
+
+ def test_sessioncart_get_price_per_item(self):
+ cart = Cart(SessionCart())
+ cart.add(stock_product, quantity=10)
+ cart_price = cart[0].get_price_per_item()
+ sessioncart_price = cart.session_cart[0].get_price_per_item()
+ self.assertTrue(isinstance(sessioncart_price, Price))
+ self.assertEqual(cart_price, sessioncart_price)
\ No newline at end of file